Go excel 导出千万数据
使用库
github.com/xuri/excelize/v2
代码
func testExcel() io.Reader { file := excelize.NewFile() //设置表名 file.NewSheet("Sheet1")
//创建流式写入 writer, err := file.NewStreamWriter("sheet1")
//修改列宽 writer.SetColWidth(1, 15, 12) //设置表头 writer.SetRow("A1", []interface{}{"测试列名1", "测试列名2", "测试列名3", "测试列名4", "测试列名5", "测试列名6", "测试列名7", "测试列名8", "测试列名9", "测试列名10", "测试列名11", "测试列名12", "测试列名13", "测试列名14", "测试列名15"}) if err != nil { return } for i := 1; i <= 10000000; i++ { //索引转单元格坐标 cell, _ := excelize.CoordinatesToCellName(1, i+1) //添加的数据 writer.SetRow(cell, []interface{}{"测试数据1", "测试数据2", "测试数据3", "测试数据4", "测试数据5", "测试数据6", "测试数据7", "测试数据8", "测试数据9", "测试数据10", "测试数据11", "测试数据12", "测试数据13", "测试数据14", "测试数据15"}) } //结束流式写入 writer.Flush() file.SaveAs("测试001.xlsx") }
第二种方法 【不推荐慢】
func testExcel() { file := excelize.NewFile() //设置表名 file.NewSheet("Sheet1")
//修改列宽 file.SetColWidth("Sheet1", "A", "O", 12)
//设置表头 file.SetSheetRow("Sheet1", "A1", &[]interface{}{"测试列名1", "测试列名2", "测试列名3", "测试列名4", "测试列名5", "测试列名6", "测试列名7", "测试列名8", "测试列名9", "测试列名10", "测试列名11", "测试列名12", "测试列名13", "测试列名14", "测试列名15"})
for i := 1; i <= 1000000; i++ { //索引转单元格坐标 cell, _ := excelize.CoordinatesToCellName(1, i+1) //添加的数据 file.SetSheetRow("Sheet1", cell, &[]interface{}{"测试数据1", "测试数据2", "测试数据3", "测试数据4", "测试数据5", "测试数据6", "测试数据7", "测试数据8", "测试数据9", "测试数据10", "测试数据11", "测试数据12", "测试数据13", "测试数据14", "测试数据15"})
} //保存文件 file.SaveAs("测试001.xlsx") }
版权声明:
作者:超级管理员
链接:
https://apecloud.ltd/article/detail.html?id=go-excel-export
来源:猿码云个人技术站
文章版权归作者所有,未经允许请勿转载。
THE END
二维码
打赏
共有0条评论