- ExportMTWaybills
- Obj2Excel
This commit is contained in:
80
business/jxutils/excel/excel.go
Normal file
80
business/jxutils/excel/excel.go
Normal file
@@ -0,0 +1,80 @@
|
||||
package excel
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"reflect"
|
||||
|
||||
"git.rosy.net.cn/jx-callback/business/jxutils"
|
||||
"git.rosy.net.cn/jx-callback/globals"
|
||||
"github.com/360EntSecGroup-Skylar/excelize"
|
||||
"github.com/fatih/structs"
|
||||
)
|
||||
|
||||
type Obj2ExcelSheetConfig struct {
|
||||
Title string
|
||||
Data interface{}
|
||||
CaptionList []string
|
||||
}
|
||||
|
||||
func Obj2Excel(sheetList []*Obj2ExcelSheetConfig) []byte {
|
||||
globals.SugarLogger.Debug("Obj2Excel")
|
||||
excelFile := excelize.NewFile()
|
||||
for sheetIndex, sheetConfig := range sheetList {
|
||||
obj := sheetConfig.Data
|
||||
|
||||
typeInfo := reflect.TypeOf(obj)
|
||||
if typeInfo.Kind() == reflect.Ptr {
|
||||
typeInfo = typeInfo.Elem()
|
||||
}
|
||||
if typeInfo.Kind() != reflect.Slice {
|
||||
panic("obj must be slice type")
|
||||
}
|
||||
typeInfo = typeInfo.Elem()
|
||||
if typeInfo.Kind() == reflect.Ptr {
|
||||
typeInfo = typeInfo.Elem()
|
||||
}
|
||||
if typeInfo.Kind() != reflect.Struct && typeInfo.Kind() != reflect.Map {
|
||||
panic("obj must be slice of map or struct")
|
||||
}
|
||||
|
||||
valueInfo := reflect.Indirect(reflect.ValueOf(obj))
|
||||
if sheetIndex == 0 {
|
||||
sheetName := excelFile.GetSheetName(1)
|
||||
excelFile.SetSheetName(sheetName, sheetConfig.Title)
|
||||
} 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 = jxutils.FlatMap(structs.Map(valueInfo.Index(i).Interface()))
|
||||
} else {
|
||||
mapData = valueInfo.Index(i).Interface().(map[string]interface{})
|
||||
}
|
||||
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]))
|
||||
}
|
||||
}
|
||||
}
|
||||
buf := &bytes.Buffer{}
|
||||
excelFile.Write(buf)
|
||||
return buf.Bytes()
|
||||
}
|
||||
|
||||
func getStructMemberNames(typeInfo reflect.Type) (nameList []string) {
|
||||
nameList = make([]string, typeInfo.NumField())
|
||||
for i := 0; i < typeInfo.NumField(); i++ {
|
||||
nameList[i] = typeInfo.Field(i).Name
|
||||
globals.SugarLogger.Debug(typeInfo.Field(i).Tag)
|
||||
}
|
||||
return nameList
|
||||
}
|
||||
|
||||
func genAxis(row, col int) string {
|
||||
return fmt.Sprintf("%c%d", col+65, row+1)
|
||||
}
|
||||
26
business/jxutils/excel/excel_test.go
Normal file
26
business/jxutils/excel/excel_test.go
Normal file
@@ -0,0 +1,26 @@
|
||||
package excel
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestObj2Excel(t *testing.T) {
|
||||
// kk := make([]*model.SkuName, 1)
|
||||
// kk[0] = &model.SkuName{
|
||||
// Name: "haha",
|
||||
// }
|
||||
|
||||
kk := []map[string]interface{}{
|
||||
map[string]interface{}{
|
||||
"key1": 1,
|
||||
"key2": 2,
|
||||
"key3": 3,
|
||||
},
|
||||
}
|
||||
cc := &Obj2ExcelSheetConfig{
|
||||
Title: "Title",
|
||||
Data: kk,
|
||||
CaptionList: []string{"key1", "key2", "key3"},
|
||||
}
|
||||
Obj2Excel([]*Obj2ExcelSheetConfig{cc})
|
||||
}
|
||||
Reference in New Issue
Block a user