如何在 Go 语言中导入和导出 Excel XLSX
这篇文章展示了如何在 Go 语言中导入和导出 Excel XLSX 文件。
Excelize 是一个用纯 Go 编写的库,提供了一组允许您写入和读取 XLSX 文件的函数。它支持读取和写入 Microsoft Excel™ 2007 及更高版本生成的 XLSX 文件。它支持在不丢失原始图表的情况下保存文件。该库需要 Go 1.10 或更高版本。完整的 API 文档可以使用 Go 内置的文档工具查看,或在线访问 godoc.org 和 文档参考。
GitHub: https://github.com/360EntSecGroup-Skylar/excelize
文档: https://xuri.me/excelize/
安装
go get github.com/360EntSecGroup-Skylar/excelize
创建 XLSX 文件
这是一个最小的使用示例,它将创建一个 XLSX 文件。
package main
import "github.com/360EntSecGroup-Skylar/excelize"
func main() {
f := excelize.NewFile()
// Create a new sheet.
index := f.NewSheet("Sheet2")
// Set value of a cell.
f.SetCellValue("Sheet2", "A2", "Hello world.")
f.SetCellValue("Sheet1", "B2", 100)
// Set active sheet of the workbook.
f.SetActiveSheet(index)
// Save xlsx file by the given path.
if err := f.SaveAs("Book1.xlsx"); err != nil {
println(err.Error())
}
}
读取 XLSX 文件
以下是读取 XLSX 文档的基础。
package main
import "github.com/360EntSecGroup-Skylar/excelize"
func main() {
f, err := excelize.OpenFile("Book1.xlsx")
if err != nil {
println(err.Error())
return
}
// Get value from cell by given worksheet name and axis.
cell, err := f.GetCellValue("Sheet1", "B2")
if err != nil {
println(err.Error())
return
}
println(cell)
// Get all the rows in the Sheet1.
rows, err := f.GetRows("Sheet1")
for _, row := range rows {
for _, colCell := range row {
print(colCell, "\t")
}
println()
}
}
将图表添加到 XLSX 文件
使用 Excelize,图表生成和管理就像几行代码一样简单。您可以基于工作表中的数据构建图表,也可以在工作表中根本没有数据的情况下生成图表。
package main
import "github.com/360EntSecGroup-Skylar/excelize"
func main() {
categories := map[string]string{"A2": "Small", "A3": "Normal",
"A4": "Large", "B1": "Apple", "C1": "Orange", "D1": "Pear"}
values := map[string]int{"B2": 2, "C2": 3, "D2": 3, "B3": 5,
"C3": 2, "D3": 4, "B4": 6, "C4": 7, "D4": 8}
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":"col3DClustered",
"series":[{"name":"Sheet1!$A$2","categories":"Sheet1!$B$1:$D$1",
"values":"Sheet1!$B$2:$D$2"},{"name":"Sheet1!$A$3","categories":"Sheet1!$B$1:$D$1",
"values":"Sheet1!$B$3:$D$3"},{"name":"Sheet1!$A$4","categories":"Sheet1!$B$1:$D$1",
"values":"Sheet1!$B$4:$D$4"}],"title":{"name":"Fruit 3D Clustered Column Chart"}}`);
err != nil {
println(err.Error())
return
}
// Save xlsx file by the given path.
if err := f.SaveAs("Book1.xlsx"); err != nil {
println(err.Error())
}
}
将图片添加到 XLSX 文件
package main
import (
_ "image/gif"
_ "image/jpeg"
_ "image/png"
"github.com/360EntSecGroup-Skylar/excelize"
)
func main() {
f, err := excelize.OpenFile("Book1.xlsx")
if err != nil {
println(err.Error())
return
}
// Insert a picture.
if err := f.AddPicture("Sheet1", "A2", "image.png", ""); err != nil {
println(err.Error())
}
// Insert a picture to worksheet with scaling.
if err := f.AddPicture("Sheet1", "D2", "image.jpg",
`{"x_scale": 0.5, "y_scale": 0.5}`); err != nil {
println(err.Error())
}
// Insert a picture offset in the cell with printing support.
if err := f.AddPicture("Sheet1", "H2", "image.gif",
`{"x_offset": 15, "y_offset": 10, "print_obj": true,
"lock_aspect_ratio": false, "locked": false}`); err != nil {
println(err.Error())
}
// Save the xlsx file with the origin path.
if err = f.Save(); err != nil {
println(err.Error())
}
}
历史
- 2019年12月30日:初始版本