Go 语言读写 Excel-LMLPHP











Excelize 是 Golang 编写的一个用来操作 Office Excel 文档类库,基于 ECMA-376 Office OpenXML 标准。可以使用它来读取、写入 XLSX 文件。相比较其他的开源类库,Excelize 支持写入原本带有图片(表)的文档,还支持向 Excel 中插入图片,并且在保存后不会丢失图表样式。

项目主页 https://github.com/360EntSecGroup-Skylar/excelize

安装

  1. go get github.com/360EntSecGroup-Skylar/excelize

创建 XLSX

  1. package main

  2. import (
  3.     "fmt"

  4.     "github.com/360EntSecGroup-Skylar/excelize"
  5. )

  6. func main() {
  7.     xlsx := excelize.NewFile()
  8.     // Create a new sheet.
  9.     index := xlsx.NewSheet("Sheet2")
  10.     // Set value of a cell.
  11.     xlsx.SetCellValue("Sheet2", "A2", "Hello world.")
  12.     xlsx.SetCellValue("Sheet1", "B2", 100)
  13.     // Set active sheet of the workbook.
  14.     xlsx.SetActiveSheet(index)
  15.     // Save xlsx file by the given path.
  16.     err := xlsx.SaveAs("./Book1.xlsx")
  17.     if err != nil {
  18.         fmt.Println(err)
  19.     }
  20. }
读取已有文档

  1. package main

  2. import (
  3.     "fmt"

  4.     "github.com/360EntSecGroup-Skylar/excelize"
  5. )

  6. func main() {
  7.     xlsx, err := excelize.OpenFile("./Book1.xlsx")
  8.     if err != nil {
  9.         fmt.Println(err)
  10.         return
  11.     }
  12.     // Get value from cell by given worksheet name and axis.
  13.     cell := xlsx.GetCellValue("Sheet1", "B2")
  14.     fmt.Println(cell)
  15.     // Get all the rows in the Sheet1.
  16.     rows := xlsx.GetRows("Sheet1")
  17.     for _, row := range rows {
  18.         for _, colCell := range row {
  19.             fmt.Print(colCell, "\t")
  20.         }
  21.         fmt.Println()
  22.     }
  23. }
向 Excel 文档中插入图表

  1. package main

  2. import (
  3.     "fmt"

  4.     "github.com/360EntSecGroup-Skylar/excelize"
  5. )

  6. func main() {
  7.     categories := map[string]string{"A2": "Small", "A3": "Normal", "A4": "Large", "B1": "Apple", "C1": "Orange", "D1": "Pear"}
  8.     values := map[string]int{"B2": 2, "C2": 3, "D2": 3, "B3": 5, "C3": 2, "D3": 4, "B4": 6, "C4": 7, "D4": 8}
  9.     xlsx := excelize.NewFile()
  10.     for k, v := range categories {
  11.         xlsx.SetCellValue("Sheet1", k, v)
  12.     }
  13.     for k, v := range values {
  14.         xlsx.SetCellValue("Sheet1", k, v)
  15.     }
  16.     xlsx.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"}}`)
  17.     // Save xlsx file by the given path.
  18.     err := xlsx.SaveAs("./Book1.xlsx")
  19.     if err != nil {
  20.         fmt.Println(err)
  21.     }
  22. }
Go 语言读写 Excel-LMLPHP












向 Excel 文档中插入图片

  1. package main

  2. import (
  3.     "fmt"
  4.     _ "image/gif"
  5.     _ "image/jpeg"
  6.     _ "image/png"

  7.     "github.com/360EntSecGroup-Skylar/excelize"
  8. )

  9. func main() {
  10.     xlsx, err := excelize.OpenFile("./Book1.xlsx")
  11.     if err != nil {
  12.         fmt.Println(err)
  13.         return
  14.     }
  15.     // Insert a picture.
  16.     err = xlsx.AddPicture("Sheet1", "A2", "./image1.png", "")
  17.     if err != nil {
  18.         fmt.Println(err)
  19.     }
  20.     // Insert a picture to worksheet with scaling.
  21.     err = xlsx.AddPicture("Sheet1", "D2", "./image2.jpg", `{"x_scale": 0.5, "y_scale": 0.5}`)
  22.     if err != nil {
  23.         fmt.Println(err)
  24.     }
  25.     // Insert a picture offset in the cell with printing support.
  26.     err = xlsx.AddPicture("Sheet1", "H2", "./image3.gif", `{"x_offset": 15, "y_offset": 10, "print_obj": true, "lock_aspect_ratio": false, "locked": false}`)
  27.     if err != nil {
  28.         fmt.Println(err)
  29.     }
  30.     // Save the xlsx file with the origin path.
  31.     err = xlsx.Save()
  32.     if err != nil {
  33.         fmt.Println(err)
  34.     }
  35. }
还有其他一些功能,在这里就不一一列举了,详细使用文档以及获取后期的维护更新可以从项目的主页获取 https://github.com/360EntSecGroup-Skylar/excelize

09-06 19:52