qidongsheng 0619

This commit is contained in:
qidongsheng
2020-06-19 14:11:37 +08:00
parent baf505b677
commit c88b737478
9 changed files with 292 additions and 30 deletions

View File

@@ -3,12 +3,11 @@ package excel
import (
"bytes"
"fmt"
"io"
"reflect"
"git.rosy.net.cn/baseapi/utils"
"git.rosy.net.cn/jx-callback/globals"
"github.com/360EntSecGroup-Skylar/excelize"
"io"
"reflect"
)
type Obj2ExcelSheetConfig struct {
@@ -20,7 +19,8 @@ type Obj2ExcelSheetConfig struct {
func Obj2Excel(sheetList []*Obj2ExcelSheetConfig) []byte {
globals.SugarLogger.Debug("Obj2Excel")
excelFile := excelize.NewFile()
for sheetIndex, sheetConfig := range sheetList {
//for sheetIndex, sheetConfig := range sheetList {
for _, sheetConfig := range sheetList {
obj := sheetConfig.Data
typeInfo := reflect.TypeOf(obj)
@@ -39,12 +39,14 @@ func Obj2Excel(sheetList []*Obj2ExcelSheetConfig) []byte {
}
valueInfo := reflect.Indirect(reflect.ValueOf(obj))
if sheetIndex == 0 {
sheetName := excelFile.GetSheetName(1)
excelFile.SetSheetName(sheetName, sheetConfig.Title)
} else {
excelFile.NewSheet(sheetConfig.Title)
}
/*因爲默認是Sheet1所以直接賦值就叫sheet1然後再改名字就行了不然會默認多一頁空白頁為sheet1*/
//if sheetIndex == 0 {
index := excelFile.NewSheet("Sheet1")
// sheetName := excelFile.GetSheetName(1)
excelFile.SetSheetName("Sheet1", sheetConfig.Title)
//} else {
// excelFile.NewSheet(sheetConfig.Title)
//}
isMemberStruct := typeInfo.Kind() == reflect.Struct
colWithList := make([]float64, len(sheetConfig.CaptionList))
if isMemberStruct {
@@ -63,9 +65,9 @@ func Obj2Excel(sheetList []*Obj2ExcelSheetConfig) []byte {
for i := 0; i < valueInfo.Len(); i++ {
for col, index := range indexSlice {
str := fmt.Sprint(reflect.Indirect(valueInfo.Index(i)).FieldByIndex(index).Interface())
// if colWith := float64(len(str)); colWith > colWithList[col] {
// colWithList[col] = colWith
// }
//// if colWith := float64(len(str)); colWith > colWithList[col] {
//// colWithList[col] = colWith
//// }
excelFile.SetCellStr(sheetConfig.Title, genAxis(i+1, col), str)
}
}
@@ -104,6 +106,7 @@ func Obj2Excel(sheetList []*Obj2ExcelSheetConfig) []byte {
// colName, _ := excelize.ColumnNumberToName(col + 1)
// excelFile.SetColWidth(sheetConfig.Title, colName, colName, colWithList[col])
// }
excelFile.SetActiveSheet(index)
}
buf := &bytes.Buffer{}
excelFile.Write(buf)