使用excelize导出文件,
以下是利用excelize设置表头和表数据相关
package utils
import (
"github.com/360EntSecGroup-Skylar/excelize"
"strconv"
)
var maxCharCount = 26
func ExportExcel(sheetName string, headers []string, rows [][]interface{
}) (*excelize.File, error) {
f := excelize.NewFile()
sheetIndex := f.NewSheet(sheetName)
maxColumnRowNameLen := 1 + len(strconv.Itoa(len(rows)))
columnCount := len(headers)
if columnCount > maxCharCount {
maxColumnRowNameLen += 1
} else if columnCount > maxCharCount*maxCharCount {
maxColumnRowNameLen += 2
}
columnNames := make([][]byte, 0, columnCount)
for i, header := range headers {
columnName := getColumnName(i, maxColumnRowNameLen)
columnNames = append(columnNames, columnName)
curColumnName := getColumnRowName(columnName, 1)
f.SetCellValue(sheetName, curColumnName, header)
}
for rowIndex, row := range rows {
for columnIndex, columnName := range columnNames {
f.SetCellValue(sheetName, getColumnRowName(columnName, rowIndex+2), row[columnIndex])
}
}
f.SetActiveSheet(sheetIndex)
return f, nil
}
func getColumnName(column