当前位置:网站首页>go实现导出excel表格
go实现导出excel表格
2022-07-26 23:59:00 【liliane】
一、概述
使用go实现导出excel表格代码比较固定,因此,可以抽取为工具函数,便于业务层复用。下面介绍简单一个封装函数,支持设置表头、数据内容,及简单样式。
二、实现步骤
1. 导入依赖包
import "github.com/xuri/excelize/v2"这里选择主流的excel工具库。
2. 创建实例
函数签名:
func WriteToExcel(headers []string, data [][]interface{}, options ExcelOptions)创建文件、工作表:
file := excelize.NewFile()
sheetIndex := file.NewSheet(sheetName)
file.SetActiveSheet(sheetIndex) // 默认sheetSetActiveSheet 用来设置默认的工作表。
3. 设置表头
// 设置表头
for i, header := range headers {
tempColAxis := firstColAxis + int32(i)
writeCell(file, sheetName, tempColAxis, firstRowAxis, header)
}设置单元格内容封装在子函数writeCell。
4. 填充数据
// 设置内容
for i, line := range data {
tempRowAxis := firstRowAxis + i + 1
for j, item := range line {
tempColAxis := firstColAxis + int32(j)
writeCell(file, sheetName, tempColAxis, tempRowAxis, item)
}
}
// 设置单元格内容
func writeCell(file *excelize.File, sheetName string, collAxis int32, rowAxis int,
value interface{}) {
err := file.SetCellValue(sheetName, getCellIndex(collAxis, rowAxis), value)
if err != nil {
log.LogError("excel set cell value error:%v", err)
}
}
// 获取单元格下标
func getCellIndex(colAxis int32, rowAxis int) string {
return fmt.Sprintf("%c%d", colAxis, rowAxis)
}SetCellValue:填写的单元格下标,先列下标,再行下标。例如,第一行第一列:A1,第二行第三列:C2。
5. 设置样式
// 设置样式
func setStyle(file *excelize.File, sheetName string, maxCol int32, options ExcelOptions) {
err := file.SetColWidth(sheetName, string(firstColAxis), string(maxCol), options.ColWidth) // 设置列宽
if err != nil {
log.LogError("excel SetColWidth error:%v", err)
}
err = file.SetRowHeight(sheetName, firstRowAxis, options.RowHeight) // 设置行高
if err != nil {
log.LogError("excel SetRowHeight error:%v", err)
}
style := &excelize.Style{
Font: &excelize.Font{
Bold: true,
},
}
styleId, err := file.NewStyle(style)
if err != nil {
log.LogError("excel NewStyle error:%v", err)
}
err = file.SetCellStyle(sheetName, getCellIndex(firstColAxis, firstRowAxis), getCellIndex(maxCol, maxRow),
styleId)
if err != nil {
log.LogError("excel SetCellStyle error:%v", err)
}
}SetColWidth:设置列宽,需指定起止列。
SetRowHeight:设置行高,这里单设置表头的行高。
NewStyle:定义样式。
SetCellStyle:对指定区域,设置单元格样式。
6. 写到输出流
file.Write(httpWriter) 此处设置到http输出流,写本地文件则可以输出到文件,比较不常用。
三、注意点
1. 通过http导出,需注意设置header。
Content-Disposition: fmt.Sprintf(`attachment; filename="%s"`, fileName)
Content-Type: application/vnd.ms-excel2. 设置样式需注意,哪些样式可以作用于什么范围,样式不生效多数是因为给对象(如单元格)设置了不可设置到单元格的样式,例如行高。
边栏推荐
- 「软件测试」包装简历从这几点出发,直接提升通过率
- After working in Tencent testing post for 5 years, I was ruthlessly dismissed in July, trying to wake up my brother who was still paddling
- Okaleido tiger logged into binance NFT on July 27, and has achieved good results in the first round
- 项目时区问题解决
- Static keyword
- Go language slow start -- go operator
- Hcip OSPF interface network interface type experiment
- Redis五种基本数据结构
- Pyqt5 use pyqtgraph to draw dynamic scatter chart
- [Li Kou] 1859. Sort sentences
猜你喜欢

Dynamically set the height of applet swiper

Kubernetes Dashboard 部署应用以及访问

白盒测试案例设计(我爷爷都能看懂)

After working in Tencent testing post for 5 years, I was ruthlessly dismissed in July, trying to wake up my brother who was still paddling

Talk about the metrics of automated testing

What is a process?

【Redis】五种常用的数据类型

iNFTnews | GGAC联合中国航天ASES 独家出品《中国2065典藏版》

Pyqt5 use pyqtgraph to draw dynamic scatter chart
![[redis] quick start](/img/42/09f08b4f78bc2ddd4d15693d62cb4b.png)
[redis] quick start
随机推荐
QT Chinese garbled constant newline ultimate solution
It has been established for 3 years, and now goose factory has an annual income of millions +. As some suggestions of software testing predecessors
F8 catch traffic, F9 catch rabbits, f10turttle
Quick sort
[NISACTF 2022]上
[redis] five common data types
【无标题】
Redis五种基本数据结构
Kubernetes Dashboard 部署应用以及访问
Leetcode- > binary search clock in
Concept of data asset management
Witness that the "decoding 2022 strong star of China's network security" is about to set sail
The XML format of labelimg annotation is converted to yolov5
Sort icons with swiper
Information collection port scanning tool nmap instructions
Favicon网页收藏图标在线制作PHP网站源码/ICO图片在线生成/支持多种图片格式转换
Talk about connection pools and threads
【Redis】快速入门
MySQL master-slave database configuration based on docker for Ubuntu
聊聊连接池和线程