go语言操作excel

网友投稿 732 2022-05-30

Go语言操作excel

简介

本文讲解如何使用go语言操作excel,使用到的库是github.com/360EntSecGroup-Skylar/excelize/v2,本文内容包含

创建项目

创建excel文件

读excel文件

chart操作

创建bar chart

创建col chart

创建line chart

创建pie chart

chart 相关参数

创建项目

# 新建一个 excelDemo 文件夹 mkdir excelDemo cd excelDemo go mod init excelDemo # 创建main.go入口程序 touch main.go go get github.com/360EntSecGroup-Skylar/excelize/v2

创建excel文件

# 创建新建excel 文件dmeo的文件夹 mkdir create cd create touch main.go

go语言操作excel

编写main.go代码,如下:

package main import ( "fmt" "github.com/360EntSecGroup-Skylar/excelize/v2" ) func main() { fmt.Println("新建excel的例子") f := excelize.NewFile() index := f.NewSheet("新的sheet") // 单个配置cell的值. f.SetCellValue("新的sheet", "A2", "Hello world.") f.SetCellValue("新的sheet", "B2", 100) f.SetCellValue("新的sheet", "C2", 200) f.SetCellValue("新的sheet", "D1", "我来试试中文") // 借用 map类型批量的写数据 categories := map[string]string{ "B1": "年龄", "A2": "张三", "A3": "李四", "A4": "王五"} for k, v := range categories { f.SetCellValue("新的sheet", k, v) } // Set active sheet of the workbook. f.SetActiveSheet(index) // Save spreadsheet by the given path. if err := f.SaveAs("Book1.xlsx"); err != nil { fmt.Println(err) } }

执行demo

go run main..go

执行之后,会创建一个Book1.xlsxexcel文件,该文件内容如下 :

回到项目根目录cd ../

读excel文件

# 创建新建excel 文件dmeo的文件夹 mkdir read cd read touch main.go

编写main.go代码,如下:

package main import ( "fmt" "github.com/360EntSecGroup-Skylar/excelize/v2" ) func main() { fmt.Println("读excel的例子") f, err := excelize.OpenFile("read.xlsx") if err != nil { fmt.Println(err) return } fmt.Println("读文件成功") // 从指定sheet以及指定一个cell里面获取数据 cell, err := f.GetCellValue("Sheet1", "B2") if err != nil { fmt.Println(err) return } fmt.Println(cell) // 获取某个sheet的的所有行 rows, err := f.GetRows("Sheet1") if err != nil { fmt.Println(err) return } for _, row := range rows { for _, colCell := range row { fmt.Print(colCell, "\t") } //读完一行换行 fmt.Println() } }

执行demo

go run main.go

如果文件内容如下

日志打印结果如下:

chart操作

接下来几章会介绍如何在excel中利用数据显示图形,本文会介绍一些基本的图形,如果读者有更多需求,可参考Excelize的详细文档

创建bar chart

代码

package main import ( "fmt" "github.com/360EntSecGroup-Skylar/excelize/v2" ) func main() { fmt.Println("写图的例子") categories := map[string]string{ "A2": "年龄", "B1": "张三", "C1": "李四", "D1": "王五"} values := map[string]int{ "B2": 5, "C2": 6, "D2": 5} f := excelize.NewFile() for k, v := range categories { f.SetCellValue("Sheet1", k, v) } for k, v := range values { f.SetCellValue("Sheet1", k, v) } // 支持的chart类型 https://github.com/360EntSecGroup-Skylar/excelize/blob/master/chart.go if err := f.AddChart("Sheet1", "E1", `{ "type": "bar", "series": [ { "name": "Sheet1!$A$2", "categories": "Sheet1!$B$1:$D$1", "values": "Sheet1!$B$2:$D$2" } ], "title": { "name": "Bar Chart" } }`); err != nil { fmt.Println(err) return } // Save spreadsheet by the given path. if err := f.SaveAs("图例子bar.xlsx"); err != nil { fmt.Println(err) } }

执行效果 go run main.go

创建col chart

代码

package main import ( "fmt" "github.com/360EntSecGroup-Skylar/excelize/v2" ) func main() { fmt.Println("写图的例子") categories := map[string]string{ "A2": "年龄", "B1": "张三", "C1": "李四", "D1": "王五"} values := map[string]int{ "B2": 5, "C2": 6, "D2": 5} f := excelize.NewFile() for k, v := range categories { f.SetCellValue("Sheet1", k, v) } for k, v := range values { f.SetCellValue("Sheet1", k, v) } // 支持的chart类型 https://github.com/360EntSecGroup-Skylar/excelize/blob/master/chart.go if err := f.AddChart("Sheet1", "E1", `{ "type": "bar", "series": [ { "name": "Sheet1!$A$2", "categories": "Sheet1!$B$1:$D$1", "values": "Sheet1!$B$2:$D$2" } ], "title": { "name": "Bar Chart" } }`); err != nil { fmt.Println(err) return } // Save spreadsheet by the given path. if err := f.SaveAs("图例子bar.xlsx"); err != nil { fmt.Println(err) } }

执行效果

创建line chart

代码

package main import ( "fmt" "github.com/360EntSecGroup-Skylar/excelize/v2" ) func main() { fmt.Println("写图的例子") categories := map[string]string{ "B1": "2018","C1": "2019","D1": "2020","E1": "2020", "A2": "张三", "A3": "李四", "A4": "王五"} values := map[string]int{ "B2": 107, "C2": 115, "D2": 120,"E2": 136, "B3": 110, "C3": 124, "D3": 140,"E3": 150, "B4": 115, "C4": 126, "D4": 132,"E4": 140, } f := excelize.NewFile() for k, v := range categories { f.SetCellValue("Sheet1", k, v) } for k, v := range values { f.SetCellValue("Sheet1", k, v) } // 支持的chart类型 https://github.com/360EntSecGroup-Skylar/excelize/blob/master/chart.go if err := f.AddChart("Sheet1", "F1", `{ "type": "line", "series": [ { "name": "Sheet1!$A$2", "categories": "Sheet1!$B$1:$E$1", "values": "Sheet1!$B$2:$E$2" }, { "name": "Sheet1!$A$3", "categories": "Sheet1!$B$1:$E$1", "values": "Sheet1!$B$3:$E$3" }, { "name": "Sheet1!$A$4", "categories": "Sheet1!$B$1:$E$1", "values": "Sheet1!$B$4:$E$4" } ], "title": { "name": "Line Chart" }, "plotarea": { "show_bubble_size": true, "show_cat_name": false, "show_leader_lines": false, "show_percent": true, "show_series_name": false, "show_val": true } }`); err != nil { fmt.Println(err) return } // Save spreadsheet by the given path. if err := f.SaveAs("图例子line.xlsx"); err != nil { fmt.Println(err) } }

执行效果:

创建pie chart

代码

package main import ( "fmt" "github.com/360EntSecGroup-Skylar/excelize/v2" ) func main() { fmt.Println("写图的例子") categories := map[string]string{"A2": "营业额", "B1": "张三", "C1": "李四", "D1": "王五"} values := map[string]float32{"B2": 100, "C2": 130.5, "D2": 150} f := excelize.NewFile() for k, v := range categories { f.SetCellValue("Sheet1", k, v) } for k, v := range values { f.SetCellValue("Sheet1", k, v) } if err := f.AddChart("Sheet1", "E1", ` { "type": "pie", "series": [ { "name": "Sheet1!$A$2", "categories": "Sheet1!$B$1:$D$1", "values": "Sheet1!$B$2:$D$2" } ], "format": { "x_scale": 1.0, "y_scale": 1.0, "x_offset": 15, "y_offset": 10, "print_obj": true, "lock_aspect_ratio": false, "locked": false }, "legend": { "position": "bottom", "show_legend_key": false }, "title": { "name": "营业额" }, "plotarea": { "show_bubble_size": true, "show_cat_name": false, "show_leader_lines": false, "show_percent": true, "show_series_name": false, "show_val": true }, "show_blanks_as": "gap" } `); err != nil { fmt.Println(err) } // 保存工作簿 if err := f.SaveAs("Book1.xlsx"); err != nil { fmt.Println(err) } }

执行效果

chart 相关参数

type:chart的类型 ,支持的图表参考:https://www.bookstack.cn/read/excelize-2.2-zh/e83b0c83bed9634a.md

series : 数据信息集合

name: 序列名称

categories: 分类轴标签

values: 图表数据区域

line: 如果是折线图有效

legend: 图例设置方法

position: 图例位置,top/buttom/left/right/top_right

show_legend_key: bool,是否显示图例,默认flase

plotarea: 设置标签

show_bubble_size,bool 默认flase,气泡大小

show_cat_name,bool,默认ture,类别名称

show_leader_lines ,bool,默认flase,显示引导线

show_percent,bool,默认flase,百分比

show_series_name,bool,默认flase,系列名称

show_val,bool,默认flase,值

通过参数 x_axis 和 y_axis 参数设置坐标轴选项。

更多参考 https://www.bookstack.cn/read/excelize-2.2-zh/e83b0c83bed9634a.md

Go

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:ISO/OSI模型
下一篇:[mysql] [note] mysql 报错Aborted connection
相关文章