当前位置:网站首页>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 .
边栏推荐
- static关键字
- [redis] five common data types
- Okaleido tiger is about to log in to binance NFT in the second round, which has aroused heated discussion in the community
- Cuteone: a onedrive multi network disk mounting program / with member / synchronization and other functions
- 多线程的具体使用
- Interview shock 68: why does TCP need three handshakes?
- Rust Web(一)—— 自建TCP Server
- Okaleido Tiger 7.27日登录Binance NFT,首轮已获不俗成绩
- C语言程序的编译上
- Invalid target distribution: solution for 17
猜你喜欢
随机推荐
Debezium系列之:基于debezium offset拉取历史数据,确保数据没有丢失
次轮Okaleido Tiger即将登录Binance NFT,引发社区热议
Cuteone: a onedrive multi network disk mounting program / with member / synchronization and other functions
Sort icons with swiper
OD-Paper【3】:Faster R-CNN: Towards Real-Time Object Detection with Region Proposal Networks
Debezium series: the binlog file cannot be recovered after the record is hung from the library server, and the task is switched to the main library to ensure that the data is not lost
Okaleido tiger is about to log in to binance NFT in the second round, which has aroused heated discussion in the community
白盒测试案例设计(我爷爷都能看懂)
Prometheus operation and maintenance tool promtool (III) debug function
Web3.0 world knowledge system sharing - what is Web3.0
Comprehensive summary of shell analysis log file commands
解决小程序报错getLocation:fail the api need to be declared in the requiredPrivateInfos field in app.json
【RYU】安装RYU常见问题及解决办法
Dynamically set the height of applet swiper
Shell 分析日志文件命令全面总结
Pyqt5 use pyqtgraph to draw dynamic scatter chart
Kubernetes Dashboard 部署应用以及访问
转:俞敏洪:阻碍你成长的,是你自己
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









![[nisactf 2022] upper](/img/61/05291ba7a63fe13882e49ab026df14.png)