欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Golang读写Excel的方法教程

程序员文章站 2022-06-24 07:54:17
介绍 excelize 是 golang 编写的一个用来操作 office excel 文档类库,基于微软的 office openxml 标准。可以使用它来读取、写...

介绍

Golang读写Excel的方法教程

excelize 是 golang 编写的一个用来操作 office excel 文档类库,基于微软的 office openxml 标准。可以使用它来读取、写入 xlsx 文件。相比较其他的开源类库,excelize 支持写入原本带有图片(表)的文档,还支持向 excel 中插入图片,并且在保存后不会丢失图表样式。

安装

go get github.com/luxurioust/excelize

创建 xlsx

package main

import (
 "fmt"
 "os"

 "github.com/luxurioust/excelize"
)

func main() {
 xlsx := excelize.createfile()
 // create a new sheet.
 xlsx.newsheet(2, "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(2)
 // save xlsx file by the given path.
 err := xlsx.writeto("./workbook.xlsx")
 if err != nil {
  fmt.println(err)
  os.exit(1)
 }
}

读写已有文档

package main

import (
 "fmt"
 "os"
 "strconv"

 "github.com/luxurioust/excelize"
)

func main() {
 xlsx, err := excelize.openfile("./workbook.xlsx")
 if err != nil {
  fmt.println(err)
  os.exit(1)
 }
 // get value from cell by given sheet index and axis.
 cell := xlsx.getcellvalue("sheet1", "b2")
 fmt.println(cell)
 // get sheet index.
 index := xlsx.getsheetindex("sheet2")
 // get all the rows in a sheet.
 rows := xlsx.getrows("sheet" + strconv.itoa(index))
 for _, row := range rows {
  for _, colcell := range row {
   fmt.print(colcell, "\t")
  }
  fmt.println()
 }
}

向 excel 中插入图表

Golang读写Excel的方法教程

package main

import (
 "fmt"
 "os"

 "github.com/luxurioust/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.createfile()
 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":"bar3d","series":[{"name":"=sheet1!$a$2","categories":"=sheet1!$b$1:$d$1","values":"=sheet1!$b$2:$d$2"},{"name":"=sheet1!$a$2","categories":"=sheet1!$b$1:$d$1","values":"=sheet1!$b$3:$d$3"},{"name":"=sheet1!$a$3","categories":"=sheet1!$b$1:$d$1","values":"=sheet1!$b$4:$d$4"}],"title":{"name":"fruit 3d line chart"}}`)
 // save xlsx file by the given path.
 err := xlsx.writeto("./workbook.xlsx")
 if err != nil {
  fmt.println(err)
  os.exit(1)
 }
}

向 excel 中插入图片

package main

import (
 "fmt"
 "os"
 _ "image/gif"
 _ "image/jpeg"
 _ "image/png"

 "github.com/luxurioust/excelize"
)

func main() {
 xlsx, err := excelize.openfile("./workbook.xlsx")
 if err != nil {
  fmt.println(err)
  os.exit(1)
 }
 // insert a picture.
 err = xlsx.addpicture("sheet1", "a2", "./image1.gif", "")
 if err != nil {
  fmt.println(err)
 }
 // insert a picture to sheet 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)
  os.exit(1)
 }
}

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

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对的支持。