ai-week/tools/excel.go

45 lines
946 B
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package tools
import (
"fmt"
"github.com/xuri/excelize/v2"
"log"
"strconv"
)
//创建excel文件覆盖写即可
func CreateNewExcel(data map[string][]string) { // 示例的 map
// 创建一个新的 Excel 文件
f := excelize.NewFile()
// 设置工作表的名称
index, err := f.NewSheet("Sheet1")
if err != nil {
return
}
// 在第一行,第一列写入标题
f.SetCellValue("Sheet1", "A1", "工单id")
f.SetCellValue("Sheet1", "B1", "工单回复")
//遍历 map将键和值写入 Excel 表格
row := 2
for key, value := range data {
fmt.Println(row, key, value)
fmt.Println("A"+strconv.Itoa(row), "B"+strconv.Itoa(row))
f.SetCellValue("Sheet1", "A"+strconv.Itoa(row), key)
f.SetCellValue("Sheet1", "B"+strconv.Itoa(row), value)
row++
}
// 设置工作表为默认激活状态
f.SetActiveSheet(index)
// 保存 Excel 文件
if err := f.SaveAs("周报.xlsx"); err != nil {
log.Fatal(err)
}
}