go excelize 批量写入数据到Excel
func CreateXlS(data [][]string, fileName string, headerNameArray []string) {
f := excelize.NewFile()
sheetName := "sheet1"
sheetWords := []string{
"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U",
"V", "W", "X", "Y", "Z",
}
for k, v := range headerNameArray {
f.SetCellValue(sheetName, sheetWords[k]+"1", v)
}
//设置列的宽度
f.SetColWidth("Sheet1", "A", sheetWords[len(headerNameArray)-1], 18)
headStyleID, _ := f.NewStyle(`{
"font":{
"color":"#333333",
"bold":false,
"family":"arial"
},
"alignment":{
"vertical":"center",
"horizontal":"center"
}
}`)
//设置表头的样式
f.SetCellStyle(sheetName, "A1", sheetWords[len(headerNameArray)-1]+"1", headStyleID)
line := 1
// 循环写入数据
for _, v := range data {
line++
for kk, _ := range headerNameArray {
f.SetCellValue(sheetName, sheetWords[kk]+strconv.Itoa(line), v[kk])
}
}
// 保存文件
if err := f.SaveAs(fileName + ".xlsx"); err != nil {
fmt.Println(err)
}
}