如何使用 Go 读取和写入 Excel (XLSX) 文件

译文
开发 后端
在本文中,我们将解释如何使用 Go 的 Excelize 库读写 Excel (xlsx) 文件并生成图片和图表。

 

【51CTO.com快译】Excelize 是一个纯 Go 编写的库,提供一组函数,允许写入和读取 XLSX / XLSM / XLTM / XLTX 文件。支持读取和写入 Microsoft Excel™ 2007 及更高版本生成的电子表格文档。通过高兼容性支持复杂组件,并提供流式API,用于从包含大量数据的工作表中生成或读取数据。这个库需要 Go 1.15 或更高版本才可以。完整的API文档可以使用go的内置文档工具查看,也可以在线访问go.dev和docs reference。 

• GitHub。

• 文件。

安装

  1. go get github.com/xuri/excelize/v2 

创建一个 XLSX 文件

下面是创建 XLSX 文件的一个简单示例: 

  1. package main 
  2.  
  3. import ( 
  4.     "fmt" 
  5.  
  6.     "github.com/xuri/excelize/v2" 
  7.  
  8. func main() { 
  9.     f := excelize.NewFile() 
  10.     // Create a new sheet. 
  11.     index := f.NewSheet("Sheet2") 
  12.     // Set value of a cell. 
  13.     f.SetCellValue("Sheet2", "A2", "Hello world.") 
  14.     f.SetCellValue("Sheet1", "B2", 100) 
  15.     // Set active sheet of the workbook. 
  16.     f.SetActiveSheet(index) 
  17.     // Save spreadsheet by the given path. 
  18.     if err := f.SaveAs("Book1.xlsx"); err != nil { 
  19.         fmt.Println(err) 
  20.     } 

读取 XLSX 文件

以下是读取 XLSX 文件所需的代码实现:

  1. package main 
  2.  
  3. import ( 
  4.     "fmt" 
  5.  
  6.     "github.com/xuri/excelize/v2" 
  7.  
  8. func main() { 
  9.     f, err := excelize.OpenFile("Book1.xlsx") 
  10.     if err != nil { 
  11.         fmt.Println(err) 
  12.         return 
  13.     } 
  14.     // Get value from cell by given worksheet name and axis. 
  15.     cell, err := f.GetCellValue("Sheet1", "B2") 
  16.     if err != nil { 
  17.         fmt.Println(err) 
  18.         return 
  19.     } 
  20.     fmt.Println(cell) 
  21.     // Get all the rows in the Sheet1. 
  22.     rows, err := f.GetRows("Sheet1") 
  23.     if err != nil { 
  24.         fmt.Println(err) 
  25.         return 
  26.     } 
  27.     for _, row := range rows { 
  28.         for _, colCell := range row { 
  29.             fmt.Print(colCell, "\t") 
  30.         } 
  31.         fmt.Println() 
  32.     } 

将图表添加到 XLSX 文件

使用 Excelize,只需几行代码即可实现图表生成和管理。可以根据工作表中的数据生成图表,也可以在工作表中不包含任何数据的情况下生成图表。

将图表添加到 Excel 文档

  1. package main 
  2.  
  3. import ( 
  4.     "fmt" 
  5.  
  6.     "github.com/xuri/excelize/v2" 
  7.  
  8. func main() { 
  9.     categories := map[string]string{ 
  10.         "A2": "Small", "A3": "Normal", "A4": "Large", 
  11.         "B1": "Apple", "C1": "Orange", "D1": "Pear"} 
  12.     values := map[string]int{ 
  13.         "B2": 2, "C2": 3, "D2": 3, "B3": 5, "C3": 2, "D3": 4, "B4": 6, "C4": 7, "D4": 8} 
  14.     f := excelize.NewFile() 
  15.     for k, v := range categories { 
  16.         f.SetCellValue("Sheet1", k, v) 
  17.     } 
  18.     for k, v := range values { 
  19.         f.SetCellValue("Sheet1", k, v) 
  20.     } 
  21.     if err := f.AddChart("Sheet1", "E1", `{ 
  22.         "type": "col3DClustered", 
  23.         "series": [ 
  24.         { 
  25.             "name": "Sheet1!$A$2", 
  26.             "categories": "Sheet1!$B$1:$D$1", 
  27.             "values": "Sheet1!$B$2:$D$2" 
  28.         }, 
  29.         { 
  30.             "name": "Sheet1!$A$3", 
  31.             "categories": "Sheet1!$B$1:$D$1", 
  32.             "values": "Sheet1!$B$3:$D$3" 
  33.         }, 
  34.         { 
  35.             "name": "Sheet1!$A$4", 
  36.             "categories": "Sheet1!$B$1:$D$1", 
  37.             "values": "Sheet1!$B$4:$D$4" 
  38.         }], 
  39.         "title": 
  40.         { 
  41.             "name": "Fruit 3D Clustered Column Chart" 
  42.         } 
  43.     }`); err != nil { 
  44.         fmt.Println(err) 
  45.         return 
  46.     } 
  47.     // Save spreadsheet by the given path. 
  48.     if err := f.SaveAs("Book1.xlsx"); err != nil { 
  49.         fmt.Println(err) 
  50.     } 

将图片添加到 XLSX 文件中 

  1. package main 
  2.  
  3. import ( 
  4.     "fmt" 
  5.     _ "image/gif" 
  6.     _ "image/jpeg" 
  7.     _ "image/png" 
  8.  
  9.     "github.com/xuri/excelize/v2" 
  10.  
  11. func main() { 
  12.     f, err := excelize.OpenFile("Book1.xlsx") 
  13.     if err != nil { 
  14.         fmt.Println(err) 
  15.         return 
  16.     } 
  17.     // Insert a picture. 
  18.     if err := f.AddPicture("Sheet1", "A2", "image.png", ""); err != nil { 
  19.         fmt.Println(err) 
  20.     } 
  21.     // Insert a picture to worksheet with scaling. 
  22.     if err := f.AddPicture("Sheet1", "D2", "image.jpg", 
  23.         `{"x_scale": 0.5, "y_scale": 0.5}`); err != nil { 
  24.         fmt.Println(err) 
  25.     } 
  26.     // Insert a picture offset in the cell with printing support. 
  27.     if err := f.AddPicture("Sheet1", "H2", "image.gif", `{ 
  28.         "x_offset": 15, 
  29.         "y_offset": 10, 
  30.         "print_obj": true, 
  31.         "lock_aspect_ratio": false, 
  32.         "locked": false 
  33.     }`); err != nil { 
  34.         fmt.Println(err) 
  35.     } 
  36.     // Save the spreadsheet with the origin path. 
  37.     if err = f.Save(); err != nil { 
  38.         fmt.Println(err) 
  39.     } 

【51CTO译稿,合作站点转载请注明原文译者和出处为51CTO.com】

 

责任编辑:梁菲 来源: DZone
相关推荐

2023-01-15 17:11:44

Rust

2020-12-10 10:46:23

PythonExcel图片

2023-10-31 12:59:00

C++编程语言

2009-07-10 10:37:11

WINAPI

2023-06-02 13:20:42

Java读取编写

2023-12-11 09:27:04

2010-06-07 09:26:32

Hadoop集群

2021-07-08 10:50:02

Go语言16GB文件代码

2021-06-28 11:15:22

Go语言16GB文件

2009-11-03 14:22:10

ADO.NET Exc

2022-10-12 09:55:14

xls文件xlsx文件

2022-03-08 09:09:08

Go块读取音视频

2023-05-19 08:01:57

Go 语言map

2011-08-03 17:38:30

iPhone NSUserDefa 自定义

2022-09-27 10:07:01

要使用 source

2023-11-03 11:56:34

2023-11-02 08:01:50

NPOI开源

2021-07-04 12:44:04

PythonExcel身份证

2009-12-04 17:06:47

PHP读取Excel文

2016-12-07 09:30:00

Power QueryExcel文件
点赞
收藏

51CTO技术栈公众号