日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
如何使用Go讀取和寫入Excel(XLSX)文件

 

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

? GitHub。

? 文件。

安裝

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

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

下面是創(chuàng)建 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 文件所需的代碼實(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.     } 

【譯稿,合作站點(diǎn)轉(zhuǎn)載請注明原文譯者和出處為.com】


文章標(biāo)題:如何使用Go讀取和寫入Excel(XLSX)文件
文章地址:http://www.dlmjj.cn/article/cdhpoce.html