编程式导入导出

This commit is contained in:
xiangheng
2024-08-12 19:15:47 +08:00
parent bc059cd05f
commit c1b3b91e32
4 changed files with 101 additions and 144 deletions

View File

@@ -8,9 +8,11 @@ import (
)
type Col struct {
Name string
Key string
Width int
Name string
Key string
Width int
Replace map[string]any
Decode func(value any) any
}
// GetExcelColumnName2 根据列数生成 Excel 列名
@@ -106,42 +108,21 @@ func normalBuildDataRow2(e *Excel, sheet, endColName string, startDataRow int, l
list := lists[i]
for j := 0; j < len(cols); j++ {
col := cols[j]
val := list[col.Key]
// switch val.(type) {
replace := col.Replace
val := list[col.Key]
for replaceKey, v := range replace {
if replaceKey == fmt.Sprintf("%v", val) {
val = fmt.Sprintf("%v", v)
break
}
}
// default:
// v, _ := json.Marshal(list[col.Key])
// rowData = append(rowData, v)
// }
rowData = append(rowData, val)
}
// // 替换
// if dataCol.Replace != "" {
// split := strings.Split(dataCol.Replace, ",")
// for j := range split {
// s := strings.Split(split[j], "_") // 根据下划线进行分割格式需要替换的内容_替换后的内容
// value := fieldData.String()
// if strings.Contains(fieldData.Type().String(), "int") {
// value = strconv.Itoa(int(fieldData.Int()))
// } else if fieldData.Type().String() == "bool" {
// value = strconv.FormatBool(fieldData.Bool())
// } else if strings.Contains(fieldData.Type().String(), "float") {
// value = strconv.FormatFloat(fieldData.Float(), 'f', -1, 64)
// }
// if s[0] == value {
// dataCol.Value = s[1]
// }
// }
// } else {
// dataCol.Value = fieldData
// }
// if err != nil {
// return
// }
// exportRow = append(exportRow, dataCol)
// }
if startDataRow%2 == 0 {
_ = e.F.SetCellStyle(sheet, startCol, endCol, e.ContentStyle2)
} else {