- 优化excel.Obj2Excel性能,不使用Struct2FlatMap
This commit is contained in:
@@ -45,19 +45,44 @@ func Obj2Excel(sheetList []*Obj2ExcelSheetConfig) []byte {
|
||||
} else {
|
||||
excelFile.NewSheet(sheetConfig.Title)
|
||||
}
|
||||
for index, name := range sheetConfig.CaptionList {
|
||||
excelFile.SetCellStr(sheetConfig.Title, genAxis(0, index), name)
|
||||
}
|
||||
for i := 0; i < valueInfo.Len(); i++ {
|
||||
var mapData map[string]interface{}
|
||||
if typeInfo.Kind() == reflect.Struct {
|
||||
mapData = utils.FlatMap(utils.Struct2MapByJson(valueInfo.Index(i).Interface()))
|
||||
} else {
|
||||
mapData = valueInfo.Index(i).Interface().(map[string]interface{})
|
||||
isMemberStruct := typeInfo.Kind() == reflect.Struct
|
||||
if isMemberStruct {
|
||||
var indexSlice [][]int
|
||||
name2IndexMap := utils.GetStructNameIndex(typeInfo, "json")
|
||||
for col, name := range sheetConfig.CaptionList {
|
||||
if _, ok := name2IndexMap[name]; !ok {
|
||||
panic(fmt.Sprintf("col:%s不能找到相应的数据", name))
|
||||
}
|
||||
indexSlice = append(indexSlice, name2IndexMap[name])
|
||||
excelFile.SetCellStr(sheetConfig.Title, genAxis(0, col), name)
|
||||
}
|
||||
for index, name := range sheetConfig.CaptionList {
|
||||
// globals.SugarLogger.Debug(sheetConfig.Title, " ", genAxis(i+1, index), " ", fmt.Sprintf("%v", mapData[name]))
|
||||
excelFile.SetCellStr(sheetConfig.Title, genAxis(i+1, index), fmt.Sprintf("%v", mapData[name]))
|
||||
for i := 0; i < valueInfo.Len(); i++ {
|
||||
for col, index := range indexSlice {
|
||||
excelFile.SetCellStr(sheetConfig.Title, genAxis(i+1, col), fmt.Sprint(reflect.Indirect(valueInfo.Index(i)).FieldByIndex(index).Interface()))
|
||||
}
|
||||
}
|
||||
} else {
|
||||
var name2IndexMap map[string]int
|
||||
if valueInfo.Len() > 0 {
|
||||
oneData := valueInfo.Index(0).Interface().(map[string]interface{})
|
||||
name2IndexMap = make(map[string]int)
|
||||
for k := range oneData {
|
||||
name2IndexMap[k] = 1
|
||||
}
|
||||
}
|
||||
for col, name := range sheetConfig.CaptionList {
|
||||
if name2IndexMap != nil {
|
||||
if _, ok := name2IndexMap[name]; !ok {
|
||||
panic(fmt.Sprintf("col:%s不能找到相应的数据", name))
|
||||
}
|
||||
}
|
||||
excelFile.SetCellStr(sheetConfig.Title, genAxis(0, col), name)
|
||||
}
|
||||
for i := 0; i < valueInfo.Len(); i++ {
|
||||
mapData := valueInfo.Index(i).Interface().(map[string]interface{})
|
||||
for col, name := range sheetConfig.CaptionList {
|
||||
excelFile.SetCellStr(sheetConfig.Title, genAxis(i+1, col), fmt.Sprint(mapData[name]))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user