当前位置:网站首页>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 .
边栏推荐
- CS224W fall 1.2 Applications of Graph ML
- LabVIEW中编程更改进程的优先级
- 次轮Okaleido Tiger即将登录Binance NFT,引发社区热议
- Blog competition dare to try BAC for beginners
- Dynamically set the height of applet swiper
- Why do people like to rank things
- 想要彻底搞的性能优化,得先从底层逻辑开始了解~
- Favicon web page collection icon online production PHP website source code /ico image online generation / support multiple image format conversion
- 机器学习【Matplotlib】
- Heisei thousand words (Heisei, September 10, 2012) (Shidu Mingzuo) the universe is far away, the Milky way is permanent, the sun and moon are running disorderly, the earth is open, the seasons shift,
猜你喜欢
随机推荐
Kubernetes Dashboard 部署应用以及访问
What is a process?
typora详细教程
聊聊连接池和线程
Favicon网页收藏图标在线制作PHP网站源码/ICO图片在线生成/支持多种图片格式转换
[redis] five common data types
F8 catch traffic, F9 catch rabbits, f10turttle
time模块: 时间戳、结构化时间、格式化时间的获取与相互转化
Talk about connection pools and threads
Greenplum【部署 08】数据库小版本升级流程及问题处理 Error: open-source-greenplum-db-6 conflicts with
Database read-write separation and database and table segmentation
idea中常用的快捷键
中断、信号、系统调用
Arduino UNO +74hc164 water lamp example
转:俞敏洪:阻碍你成长的,是你自己
C语言程序的编译(预处理)下
Non global function of lua function
Cookie addition, deletion, modification and query methods
Information collection port scanning tool nmap instructions
Comprehensive summary of shell analysis log file commands









