Go 语言读写 Excel

网友投稿 588 2022-05-29

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

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

安装

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

创建 XLSX

package main import (     "fmt"     "github.com/360EntSecGroup-Skylar/excelize" ) func main() {     xlsx := excelize.NewFile()     // Create a new sheet.     index := xlsx.NewSheet("Sheet2")     // Set value of a cell.     xlsx.SetCellValue("Sheet2", "A2", "Hello world.")     xlsx.SetCellValue("Sheet1", "B2", 100)     // Set active sheet of the workbook.     xlsx.SetActiveSheet(index)     // Save xlsx file by the given path.     err := xlsx.SaveAs("./Book1.xlsx")     if err != nil {         fmt.Println(err)     } }

读写已有文档

package main import (     "fmt"     "github.com/360EntSecGroup-Skylar/excelize" ) func main() {     xlsx, err := excelize.OpenFile("./Book1.xlsx")     if err != nil {         fmt.Println(err)         return     }     // Get value from cell by given worksheet name and axis.     cell := xlsx.GetCellValue("Sheet1", "B2")     fmt.Println(cell)     // Get all the rows in the Sheet1.     rows := xlsx.GetRows("Sheet1")     for _, row := range rows {         for _, colCell := range row {             fmt.Print(colCell, "\t")         }         fmt.Println()     } }

向 Excel 中插入图表

package main import (     "fmt"     "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}     xlsx := excelize.NewFile()     for k, v := range categories {         xlsx.SetCellValue("Sheet1", k, v)     }     for k, v := range values {         xlsx.SetCellValue("Sheet1", k, v)     }     xlsx.AddChart("Sheet1", "E1", `{"type":"col3DClustered","series":[{"name":"Sheet1!$A","categories":"Sheet1!$B:$D","values":"Sheet1!$B:$D"},{"name":"Sheet1!$A","categories":"Sheet1!$B:$D","values":"Sheet1!$B:$D"},{"name":"Sheet1!$A","categories":"Sheet1!$B:$D","values":"Sheet1!$B:$D"}],"title":{"name":"Fruit 3D Clustered Column Chart"}}`)     // Save xlsx file by the given path.     err := xlsx.SaveAs("./Book1.xlsx")     if err != nil {         fmt.Println(err)     } }

Go 语言读写 Excel

向 Excel 中插入图片

package main import (     "fmt"     _ "image/gif"     _ "image/jpeg"     _ "image/png"     "github.com/360EntSecGroup-Skylar/excelize" ) func main() {     xlsx, err := excelize.OpenFile("./Book1.xlsx")     if err != nil {         fmt.Println(err)         return     }     // Insert a picture.     err = xlsx.AddPicture("Sheet1", "A2", "./image1.png", "")     if err != nil {         fmt.Println(err)     }     // Insert a picture to worksheet with scaling.     err = xlsx.AddPicture("Sheet1", "D2", "./image2.jpg", `{"x_scale": 0.5, "y_scale": 0.5}`)     if err != nil {         fmt.Println(err)     }     // Insert a picture offset in the cell with printing support.     err = xlsx.AddPicture("Sheet1", "H2", "./image3.gif", `{"x_offset": 15, "y_offset": 10, "print_obj": true, "lock_aspect_ratio": false, "locked": false}`)     if err != nil {         fmt.Println(err)     }     // Save the xlsx file with the origin path.     err = xlsx.Save()     if err != nil {         fmt.Println(err)     } }

还有其他一些功能,在这里就不一一列举了,详细使用文档以及获取后期的维护更新可以从项目的主页获取

github.com/360EntSecGroup-Skylar/excelize

Go

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Vue进阶(幺捌陆):CSS 属性选择器
下一篇:从Postman到ApiPost——码农闰土
相关文章