自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

如何使用 Go 讀取和寫入 Excel (XLSX) 文件

譯文
開發(fā) 后端
在本文中,我們將解釋如何使用 Go 的 Excelize 庫讀寫 Excel (xlsx) 文件并生成圖片和圖表。

 

【51CTO.com快譯】Excelize 是一個(gè)純 Go 編寫的庫,提供一組函數(shù),允許寫入和讀取 XLSX / XLSM / XLTM / XLTX 文件。支持讀取和寫入 Microsoft Excel™ 2007 及更高版本生成的電子表格文檔。通過高兼容性支持復(fù)雜組件,并提供流式API,用于從包含大量數(shù)據(jù)的工作表中生成或讀取數(shù)據(jù)。這個(gè)庫需要 Go 1.15 或更高版本才可以。完整的API文檔可以使用go的內(nèi)置文檔工具查看,也可以在線訪問go.dev和docs reference。 

• GitHub。

• 文件。

安裝

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

創(chuàng)建一個(gè) XLSX 文件

下面是創(chuàng)建 XLSX 文件的一個(gè)簡單示例: 

  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 文件所需的代碼實(shí)現(xiàn):

  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,只需幾行代碼即可實(shí)現(xiàn)圖表生成和管理??梢愿鶕?jù)工作表中的數(shù)據(jù)生成圖表,也可以在工作表中不包含任何數(shù)據(jù)的情況下生成圖表。

將圖表添加到 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譯稿,合作站點(diǎn)轉(zhuǎn)載請(qǐng)注明原文譯者和出處為51CTO.com】

 

責(zé)任編輯:梁菲 來源: DZone
相關(guān)推薦

2023-01-15 17:11:44

Rust

2024-06-25 09:08:24

.NETCSV文件

2023-10-31 12:59:00

C++編程語言

2020-12-10 10:46:23

PythonExcel圖片

2023-06-02 13:20:42

Java讀取編寫

2009-07-10 10:37:11

WINAPI

2023-12-11 09:27:04

2021-06-28 11:15:22

Go語言16GB文件

2021-07-08 10:50:02

Go語言16GB文件代碼

2009-11-03 14:22:10

ADO.NET Exc

2010-06-07 09:26:32

Hadoop集群

2022-03-08 09:09:08

Go塊讀取音視頻

2022-10-12 09:55:14

xls文件xlsx文件

2011-08-03 17:38:30

iPhone NSUserDefa 自定義

2022-09-27 10:07:01

要使用 source

2023-05-19 08:01:57

Go 語言map

2024-12-03 00:40:55

2023-11-03 11:56:34

2023-11-02 08:01:50

NPOI開源

2009-12-04 17:06:47

PHP讀取Excel文
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)