![image](http://studygolang.qiniudn.com/170326/a71ba93152df4e9ba1d658df14b26633.png)
Excelize 是 Golang 编写的一个用来操作 Office Excel 文档类库,基于微软的 OpenXML 标准。可以使用它来读取、写入 XLSX 文件。相比较其他的开源类库,Excelize 支持写入带有图表的文档,并且在保存后不会丢失图表样式。
安装
<pre class="brush:shell;toolbar: true; auto-links: false;">go get github.com/Luxurioust/excelize</pre>
创建 XLSX
<pre class="brush:cpp ;toolbar: true; auto-links: false;">package main
import (
"fmt"
"github.com/Luxurioust/excelize"
)
func main() {
xlsx := excelize.CreateFile()
xlsx = excelize.NewSheet(xlsx, 2, "Sheet2")
xlsx = excelize.NewSheet(xlsx, 3, "Sheet3")
xlsx = excelize.SetCellInt(xlsx, "Sheet2", "A23", 10)
xlsx = excelize.SetCellStr(xlsx, "Sheet3", "B20", "Hello")
err := excelize.Save(xlsx, "~/Workbook.xlsx")
if err != nil {
fmt.Println(err)
}
}</pre>
修改已有文档
<pre class="brush:cpp ;toolbar: true; auto-links: false;">package main
import (
"fmt"
"github.com/Luxurioust/excelize"
)
func main() {
xlsx, err := excelize.OpenFile("~/Workbook.xlsx")
if err != nil {
fmt.Println(err)
}
xlsx = excelize.SetCellInt(xlsx, "Sheet2", "B2", 100)
xlsx = excelize.SetCellStr(xlsx, "Sheet2", "C11", "Hello")
xlsx = excelize.NewSheet(xlsx, 3, "TestSheet")
xlsx = excelize.SetCellInt(xlsx, "Sheet3", "A23", 10)
xlsx = excelize.SetCellStr(xlsx, "Sheet3", "b230", "World")
xlsx = excelize.SetActiveSheet(xlsx, 2)
if err != nil {
fmt.Println(err)
}
err = excelize.Save(xlsx, "~/Workbook.xlsx")
}</pre>