go excelize xlsx
baliance/gooxml: Office Open/OOXML/ECMA-376 documents (.docx, .xlsx, .pptx)
Installation
go get github.com/360EntSecGroup-Skylar/excelize
单元格转换
// excelize.ToAlphaString(36) convert 36 to column title AK
// excelize.TitleToNumber("AK") convert AK and ak to column title 36
xlsx.SetCellStr(sheet, excelize.ToAlphaString(j)+i, strings.TrimSpace(row[j]))
设置某个单元格的值
func New(name, sheet string) *excelize.File {
xlsx, err := excelize.OpenFile(name)
if err != nil {
file = excelize.NewFile()
}
//没有则创建
index = file.GetSheetIndex(sheet)
if index < 1 {
index = file.NewSheet(sheet)
for i, v := range Names {
xlsx.SetCellValue(sheet, excelize.ToAlphaString(i)+"1", v)
}
}
return xlsx
}
获取单元格内容方式1: 行迭代器
// func (f *File) Rows(sheet string) (*Rows, error)
// 根据给定的工作表名称(大小写敏感)获取该工作表的行迭代器。使用行迭代器遍历单元格:
// 行迭代器 - 单行操作
// func (rows *Rows) Columns() []string 返回当前行所有列的值。
// 行迭代器 - 遍历操作
// func (rows *Rows) Next() bool
rows, err := xlsx.Rows("Sheet1")
for rows.Next() {
for _, colCell := range rows.Columns() {
fmt.Print(colCell, "\t")
}
fmt.Println()
}
获取全部单元格的值方式二: xlsx.GetRows("Sheet1")
// 例如,获取并遍历输出名为 Sheet1 的工作表上的所有单元格的值:
for _, row := range xlsx.GetRows("Sheet1") {
for _, colCell := range row {
fmt.Print(colCell, "\t")
}
fmt.Println()
}
合并单元格区域为左上角的值
func (f *File) MergeCell(sheet, hcell, vcell string)
根据给定的工作表名(大小写敏感)和单元格坐标区域合并单元格。例如,合并名为 Sheet1 的工作表上 D3:E9 区域内的单元格:
xlsx.MergeCell("Sheet1", "D3", "E9")
如果给定的单元格坐标区域与已有的其他合并单元格相重叠,已有的合并单元格将会被删除。
获取全部合并单元格
根据给定的工作表名(大小写敏感)获取全部合并单元格的坐标区域和值。
func (f *File) GetMergeCells(sheet string) []MergeCell
Create XLSX file
Here is a minimal example usage that will create XLSX file.
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)
}
}
Reading XLSX file
The following constitutes the bare to read a XLSX document.
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()
}
}
Add chart to XLSX file
With Excelize chart generation and management is as easy as a few lines of code. You can build charts based off data in your worksheet or generate charts without any data in your worksheet at all.
Excelize
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$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"}}`)
// Save xlsx file by the given path.
err := xlsx.SaveAs("./Book1.xlsx")
if err != nil {
fmt.Println(err)
}
}
Add picture to XLSX file
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)
}
}