文章目录
- 1 简介
- 2 相关需求与实现
- 2.1 导出带单元格合并的excel文件
- 2.2 导出增加自动换行和固定列宽的excel文件
1 简介
-
之前整理过使用Excelize导出原始excel文件与增加数据校验的excel导出。【go】Excelize处理excel表 带数据校验的文件导出
-
本文整理使用Excelize导出带单元格合并的excel文件与导出增加自动换行和固定列宽的excel文件
2 相关需求与实现
2.1 导出带单元格合并的excel文件
- 需求:入参是一个map,其键是每个sheet页的名字,其值是一个二维切片。在每个sheet页中依次写入切片中的数据。返回文件名和错误信息。
合并单元格:前三列,如果此格和上行格相同,则合并 - 代码:
// 增加合并单元格的文件导出
func WriteExcelMerge(datas map[string][][]string) (string, error) {file := excelize.NewFile()for sheetName, data := range datas {index, _ := file.NewSheet(sheetName)for i, row := range data {for j, val := range row {// 列行数字索引转excel坐标索引cellName, _ := excelize.CoordinatesToCellName(j+1, i+1)// 设置,写入file.SetCellValue(sheetName, cellName, val)}}// 创建表格file.SetActiveSheet(index)}for sheet, _ := range datas {// 获取表格中的所有行rows, err := file.GetRows(sheet)if err != nil {return "", err}for i := 1; i < len(rows); i++ {currRow := rows[i]prevRow := rows[i-1]// 判断前3列是否相等if currRow[0] == prevRow[0] &&currRow[1] == prevRow[1] &&currRow[2] == prevRow[2] {// 合并相邻单元格for j := 1; j <= 3; j++ {cellName1, _ := excelize.CoordinatesToCellName(j, i) // 列,行cellName2, _ := excelize.CoordinatesToCellName(j, i+1)err := file.MergeCell(sheet, cellName1, cellName2)if err != nil {return "", err}}}}}filename := "aaa" + ".xlsx"// 创建目录_, err := os.ReadDir("aaa/")if err != nil {// 不存在就创建err = os.MkdirAll("aaa/", fs.ModePerm)if err != nil {return "", err}}file.DeleteSheet("Sheet1")err = file.SaveAs("aaa/" + filename)if err != nil {return "", err}return filename, nil
}func main() {datas := make(map[string][][]string)datas["xxx"] = [][]string{{"a", "b", "c", "D1", "E1", "G1", "H1", "I1"}, {"a", "b", "c", "D1"}, {"a", "b", "c", "D1"}}datas["yyy"] = [][]string{{"X1", "Y1", "Z1"}, {"X2", "Y2", "Z2"}, {"X3", "Y3", "Z3"}}fileName, err := WriteExcelMerge(datas)if err != nil {fmt.Println("Write excel error: ", err)return}fmt.Println("Write excel success, file name is: ", fileName)
}
- 输出文件:
合并:
未合并:
2.2 导出增加自动换行和固定列宽的excel文件
- 需求:同上
其中,第一列设置自动换行+固定列宽20;第二列固定列宽20 - 代码:
func WriteExcelAuto(datas map[string][][]string, widths map[string]float64, wrapTexts []string) (string, error) {file := excelize.NewFile()// 自动换行styleID, err := file.NewStyle(&excelize.Style{Alignment: &excelize.Alignment{WrapText: true,},})if err != nil {return "", err}for sheetName, data := range datas {index, _ := file.NewSheet(sheetName)for i, row := range data {for j, val := range row {// 列行数字索引转excel坐标索引cellName, _ := excelize.CoordinatesToCellName(j+1, i+1)// 设置,写入file.SetCellValue(sheetName, cellName, val)}}// 设置自动换行for _, v := range wrapTexts {file.SetColStyle(sheetName, v, styleID)}// 设置列宽for k, v := range widths {file.SetColWidth(sheetName, k, k, v)}// 创建表格file.SetActiveSheet(index)}filename := "aaa" + ".xlsx"// 创建目录_, err = os.ReadDir("aaa/")if err != nil {// 不存在就创建err = os.MkdirAll("aaa/", fs.ModePerm)if err != nil {return "", err}}file.DeleteSheet("Sheet1")err = file.SaveAs("aaa/" + filename)if err != nil {return "", err}return filename, nil
}func main() {datas := make(map[string][][]string)datas["xxx"] = [][]string{{"AAAAAAAAAAAAAAAAAAAAA", "B1", "C1", "d1", "E1", "G1", "H1", "I1"}, {"A2", "B2", "C2"}, {"A3", "B3", "C3"}}datas["yyy"] = [][]string{{"X1", "Y1", "Z1"}, {"X2", "Y2", "Z2"}, {"X3", "Y3", "Z3"}}widths := map[string]float64{"A": 20,"B": 20,}wraps := []string{"A",}fileName, err := WriteExcelAuto(datas, widths, wraps)if err != nil {fmt.Println("Write excel error: ", err)return}fmt.Println("Write excel success, file name is: ", fileName)
}
- 输出文件: