当前位置:网站首页>Go to export excel form
Go to export excel form
2022-07-27 02:59:00 【liliane】
One 、 summary
Use go Implement export excel The table code is relatively fixed , therefore , It can be extracted as a tool function , It is convenient for business layer reuse . Here is a simple encapsulation function , Support setting header 、 The data content , And simple style .
Two 、 Implementation steps
1. Import dependency package
import "github.com/xuri/excelize/v2"Choose the mainstream here excel Tool library .
2. Create examples
Function signature :
func WriteToExcel(headers []string, data [][]interface{}, options ExcelOptions)create a file 、 Worksheet :
file := excelize.NewFile()
sheetIndex := file.NewSheet(sheetName)
file.SetActiveSheet(sheetIndex) // Default sheetSetActiveSheet Used to set the default worksheet .
3. Set the header
// Set the header
for i, header := range headers {
tempColAxis := firstColAxis + int32(i)
writeCell(file, sheetName, tempColAxis, firstRowAxis, header)
}Set cell contents to be encapsulated in sub functions writeCell.
4. Fill in the data
// Set contents
for i, line := range data {
tempRowAxis := firstRowAxis + i + 1
for j, item := range line {
tempColAxis := firstColAxis + int32(j)
writeCell(file, sheetName, tempColAxis, tempRowAxis, item)
}
}
// Set cell contents
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)
}
}
// Get cell subscript
func getCellIndex(colAxis int32, rowAxis int) string {
return fmt.Sprintf("%c%d", colAxis, rowAxis)
}SetCellValue: The subscript of the filled cell , List subscripts first , Re subscript . for example , First row, first column :A1, The second row, the third column :C2.
5. Set the style
// Set the style
func setStyle(file *excelize.File, sheetName string, maxCol int32, options ExcelOptions) {
err := file.SetColWidth(sheetName, string(firstColAxis), string(maxCol), options.ColWidth) // Set column width
if err != nil {
log.LogError("excel SetColWidth error:%v", err)
}
err = file.SetRowHeight(sheetName, firstRowAxis, options.RowHeight) // Set row height
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: Set column width , You need to specify the start and end columns .
SetRowHeight: Set row height , Here, set the row height of the header .
NewStyle: Definition of style .
SetCellStyle: For the specified area , Set cell style .
6. Write output stream
file.Write(httpWriter) Set here to http Output stream , If you write a local file, you can output it to a file , It's not commonly used .
3、 ... and 、 Be careful
1. adopt http export , Pay attention to the setting header.
Content-Disposition: fmt.Sprintf(`attachment; filename="%s"`, fileName)
Content-Type: application/vnd.ms-excel2. Pay attention to setting style , Which styles can be used in what range , Styles don't work mostly because they are given to objects ( Such as cell ) Set a style that cannot be set to cells , For example, row height .
边栏推荐
- Information collection port scanning tool nmap instructions
- static关键字
- How to do the system security test? Let's talk about it in detail
- 文章摘要智能提取【基于BERT技术】
- 无效的目标发行版:17 的解决办法
- ansible系列之:不收集主机信息 gather_facts: False
- Okaleido tiger is about to log in to binance NFT in the second round, which has aroused heated discussion in the community
- 通达信开户安全么
- Basic theoretical knowledge of software testing - concept
- 测试人需要的数据库知识:MySQL常用语法
猜你喜欢

JMeter interface test, quickly complete a single interface request

Play a parallel multithreaded mcu-mc3172

Database read-write separation and database and table segmentation

OD-Paper【3】:Faster R-CNN: Towards Real-Time Object Detection with Region Proposal Networks

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

iNFTnews | “流量+体验”白衬e数字时装节引领数字时装新变迁

什么是进程?

Kubernetes dashboard deployment application and access

Okaleido Tiger 7.27日登录Binance NFT,首轮已获不俗成绩

ArduinoUNO驱动RGB模块全彩效果示例
随机推荐
Greenplum【部署 08】数据库小版本升级流程及问题处理 Error: open-source-greenplum-db-6 conflicts with
Tabbar of customized wechat applet on uni app
动态设置小程序swiper的高度
Interview shock 68: why does TCP need three handshakes?
js utils 零碎
Arduino UNO +74hc164 water lamp example
What is a process?
聊聊连接池和线程
ansible系列之:不收集主机信息 gather_facts: False
Functions of libraries and Archives
pyqt5使用pyqtgraph画动态散点图
Favicon web page collection icon online production PHP website source code /ico image online generation / support multiple image format conversion
机器学习【Matplotlib】
素因子分解--C(gcc)--PTA
对象创建的流程分析
测试人需要的数据库知识:MySQL常用语法
bp 插件临时代码记录
系统安全测试要怎么做,详细来说说
Static keyword
次轮Okaleido Tiger即将登录Binance NFT,引发社区热议