当前位置:网站首页>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. 设置样式需注意,哪些样式可以作用于什么范围,样式不生效多数是因为给对象(如单元格)设置了不可设置到单元格的样式,例如行高。
边栏推荐
- 无效的目标发行版:17 的解决办法
- Basic theoretical knowledge of software testing - concept
- [redis] five common data types
- 项目时区问题解决
- 软件测试相关试题知识点
- Redis五种基本数据结构
- 面试突击68:为什么 TCP 需要 3 次握手?
- Tabbar of customized wechat applet on uni app
- How small programs help the new model of smart home ecology
- MySQL 5.7 takes the first item of the group
猜你喜欢

Uni app wechat applet search keywords are displayed in red

消息队列学习 -- 概念

After ten years of testing, I want to say to my friends who are still confused: one thing is to do a good job in personal planning

解决小程序报错getLocation:fail the api need to be declared in the requiredPrivateInfos field in app.json

Database read-write separation and database and table segmentation

swiperjs自定义宽度

系统安全测试要怎么做,详细来说说

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

How many holes have you stepped on in BigDecimal?

函数栈帧详解
随机推荐
Debezium系列之:记录从库服务器挂掉后binlog文件无法恢复,任务切换到主库并保证数据不丢失的方法
How small programs help the new model of smart home ecology
OD-Paper【3】:Faster R-CNN: Towards Real-Time Object Detection with Region Proposal Networks
Plato farm is expected to further expand its ecosystem through elephant swap
Prometheus operation and maintenance tool promtool (III) debug function
Redis installation and operation (Linux)
[untitled]
线程和进程
LeetCode->二分法(三)
bp 插件临时代码记录
Why do people like to rank things
Which securities firm is safer to open an account and buy REITs funds?
文章摘要智能提取【基于BERT技术】
JS utils fragmented
[redis] five common data types
Ubuntu基于docker的mysql主从数据库配置
OD-Paper【3】:Faster R-CNN: Towards Real-Time Object Detection with Region Proposal Networks
QT Chinese garbled constant newline ultimate solution
人们为什么热衷于给事物排序
Getlocation:fail the API need to be declared in the requiredprivateinfo field in app.json