目录

go语言操作excel

目录

简介

excelize包提供了一组函数,允许您对XLSX / XLSM / XLTM文件进行写入和读取。支持读取和写入由Microsoft Excel™2007和更高版本生成的电子表格文档。高兼容性支持复杂组件,并提供流API,用于从包含大量数据的工作表中生成或读取数据。这个库需要Go版本1.15或更高版本。

更多信息

快速上手

安装

安装命令

1
go get github.com/xuri/excelize/v2

更新

更新命令

1
go get -u github.com/xuri/excelize/v2

创建excel文档

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package main

import (
	"fmt"
	"github.com/360EntSecGroup-Skylar/excelize/v2"
)

func main()  {
	f := excelize.NewFile()
	//创建一个工作表
	index := f.NewSheet("Sheet2")
	//更改sheet1名字
	f.SetSheetName("Sheet1","test")
	//设置单元格的值
	f.SetCellValue("Sheet2","A2","Hello world!")
	f.SetCellValue("test","B1",100)
	//设置工作簿的默认工作表
	f.SetActiveSheet(index)

	//根据指定路径保存文件
	err := f.SaveAs("./Book1.xlsx")
	if err != nil{
		fmt.Println(err)
	}
}

读取Excel文档

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package main

import (
	"fmt"
	"github.com/360EntSecGroup-Skylar/excelize/v2"
)

func main()  {
	//打开Excel
	f ,err := excelize.OpenFile("./Book1.xlsx")
	if err != nil{
		fmt.Println(err.Error())
		return
	}

	//获取工作表中指定单元格的值
	cell ,err := f.GetCellValue("test","B1")
	if err != nil{
		fmt.Println(err.Error())
		return
	}
	fmt.Println(cell)
	//获取Sheet2上所有单元格
	rows,err := f.GetRows("Sheet2")
	for _,row := range rows{
		for _,colCell := range row{
			fmt.Print(colCell,"\t")
		}
		fmt.Println()
	}
}

在Excel文档中创建图标

使用 Excelize 生成图表十分简单,仅需几行代码。您可以根据工作表中的已有数据构建图表,或向工作表中添加数据并创建图表。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package main

import (
	"fmt"
	"github.com/360EntSecGroup-Skylar/excelize/v2"
)

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}
	f := excelize.NewFile()
	for k,v := range  categories{
		f.SetCellValue("Sheet1",k,v)
	}
	for k,v := range values{
		f.SetCellValue("Sheet1",k,v)
	}

	err := f.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"}}`)
	if err != nil {
		fmt.Println(err)
	}
	// 根据指定路径保存文件
	err = f.SaveAs("./Book1.xlsx")
	if err != nil {
		fmt.Println(err)
	}
}

向文档中插入图片

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package main

import (
	"fmt"
	_ "image/jpeg"
	"github.com/360EntSecGroup-Skylar/excelize/v2"
)

func main()  {
	f,err := excelize.OpenFile("./Book1.xlsx")
	if err != nil{
		fmt.Println(err.Error())
		return
	}
	//插入图片
	err = f.AddPicture("Sheet1","K2",".//touxiang.jpg","")
	if err != nil{
		fmt.Println(err)
		return
	}

	//在工作表中插入图片并设置图片缩放比例
	err = f.AddPicture("Sheet1","D2","./touxiang.jpg",`{"x_scale": 0.5, "y_scale": 0.5}`)
	if err != nil{
		fmt.Println(err)
		return
	}

	//在工作表中插入图片,并设置图片的打印属性
	err = f.AddPicture("Sheet1", "H2", "./touxiang.jpg", `{"x_offset": 15, "y_offset": 10, "print_obj": true, "lock_aspect_ratio": false, "locked": false}`)
	if err != nil {
		fmt.Println(err)
	}
	// 保存文件
	err = f.Save()
	if err != nil {
		fmt.Println(err)
	}
}

工作簿

创建

1
func NewFile() *File

使用 NewFile 新建 Excel 工作薄,新创建的工作簿中会默认包含一个名为 Sheet1 的工作表。

打开

1
func OpenFile(filename string) (*File, error)

使用 OpenFile 打开已有 Excel 文档。

保存

1
func (f *File) Save() error

使用 Save 保存对 Excel 文档的编辑。

另存为

1
func (f *File) SaveAs(name string) error

使用 SaveAs 保存 Excel 文档为指定文件。

工作表

新建工作表

1
func (f *File) NewSheet(name string) int

根据给定的工作表名称添加新的工作表,并返回工作表索引。新创建的工作簿将会包含一个名为 Sheet1 的默认工作簿。

删除工作表

1
func (f *File) DeleteSheet(name string)

根据给定的工作表名称删除指定工作表,谨慎使用此方法,这将会影响到与被删除工作表相关联的公式、引用、图表等元素。如果有其他组件引用了被删除工作表上的值,将会引发错误提示,甚至将会导致打开工作簿失败。当工作簿中仅包含一个工作表时,调用此方法无效。

复制工作表

1
func (f *File) CopySheet(from, to int) error

根据给定的被复制工作表与目标工作表索引复制工作表,目标工作表索引需要开发者自行确认是否已经存在。目前支持仅包含单元格值和公式的工作表间的复制,不支持包含表格、图片、图表和透视表等元素的工作表之间的复制。

1
2
3
4
// 名称为 Sheet1 的工作表已经存在 ...
index := f.NewSheet("Sheet2")
err := f.CopySheet(1, index)
return err

设置工作表背景图片

1
func (f *File) SetSheetBackground(sheet, picture string) error

根据给定的工作表名称和图片地址为指定的工作表设置平铺效果的背景图片

设置默认工作表

1
func (f *File) SetActiveSheet(index int)

根据给定的索引值设置默认工作表,索引的值应该大于 0 且小于工作簿所包含的累积工作表总数。

获取默认工作表索引

1
func (f *File) GetActiveSheetIndex() int

获取默认工作表的索引,如果没有找到默认工作表将返回 0

获取工作表视图属性

1
func (f *File) GetSheetViewOptions(name string, viewIndex int, opts ...SheetViewOptionPtr) error

根据给定的工作表名称、视图索引和视图参数获取工作表视图属性,viewIndex 可以是负数,如果是这样,则向后计数(-1 代表最后一个视图)。

可选视图参数 类型
DefaultGridColor bool
RightToLeft bool
ShowFormulas bool
ShowGridLines bool
ShowRowColHeaders bool
  • 例1,获取名为 Sheet1 的工作表上最后一个视图的网格线属性设置:

    1
    2
    
    var showGridLines excelize.ShowGridLines
    err = f.GetSheetViewOptions("Sheet1", -1, &showGridLines)
    
  • 例2

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    
    xl := excelize.NewFile()
    const sheet = "Sheet1"
    var (
        defaultGridColor  excelize.DefaultGridColor
        rightToLeft       excelize.RightToLeft
        showFormulas      excelize.ShowFormulas
        showGridLines     excelize.ShowGridLines
        showRowColHeaders excelize.ShowRowColHeaders
        zoomScale         excelize.ZoomScale
        topLeftCell       excelize.TopLeftCell
    )
    if err := xl.GetSheetViewOptions(sheet, 0,
        &defaultGridColor,
        &rightToLeft,
        &showFormulas,
        &showGridLines,
        &showRowColHeaders,
        &zoomScale,
        &topLeftCell,
    ); err != nil {
        panic(err)
    }
    fmt.Println("Default:")
    fmt.Println("- defaultGridColor:", defaultGridColor)
    fmt.Println("- rightToLeft:", rightToLeft)
    fmt.Println("- showFormulas:", showFormulas)
    fmt.Println("- showGridLines:", showGridLines)
    fmt.Println("- showRowColHeaders:", showRowColHeaders)
    fmt.Println("- zoomScale:", zoomScale)
    fmt.Println("- topLeftCell:", `"`+topLeftCell+`"`)
    if err := xl.SetSheetViewOptions(sheet, 0, excelize.TopLeftCell("B2")); err != nil {
        panic(err)
    }
    if err := xl.GetSheetViewOptions(sheet, 0, &topLeftCell); err != nil {
        panic(err)
    }
    if err := xl.SetSheetViewOptions(sheet, 0, excelize.ShowGridLines(false)); err != nil {
        panic(err)
    }
    if err := xl.GetSheetViewOptions(sheet, 0, &showGridLines); err != nil {
        panic(err)
    }
    fmt.Println("After change:")
    fmt.Println("- showGridLines:", showGridLines)
    fmt.Println("- topLeftCell:", topLeftCell)
      
    输出:
    Default:
    - defaultGridColor: true
    - rightToLeft: false
    - showFormulas: false
    - showGridLines: true
    - showRowColHeaders: true
    - zoomScale: 0
    - topLeftCell: ""
    After change:
    - showGridLines: false
    - topLeftCell: B2
    

    设置工作表页面布局

    1
    
    func (f *File) SetPageLayout(sheet string, opts ...PageLayoutOption) error
    

    根据给定的工作表名称和页面布局参数设置工作表的页面布局属性。目前支持设置的页面布局属性:

    • 通过 PageLayoutOrientation 方法设置页面布局方向,默认页面布局方向为“纵向”。下面的表格是 Excelize 中页面布局方向 PageLayoutOrientation 参数的列表:

      参数 方向
      OrientationPortrait 纵向
      OrientationLandscape 横向
    • 通过 PageLayoutPaperSize 方法设置页面纸张大小,默认页面布局大小为“信纸 8½ × 11 英寸”。下面的表格是 Excelize 中页面布局大小和索引 PageLayoutPaperSize 参数的关系对照:

      索引 纸张大小
      1 信纸 8½ × 11 英寸
      2 简式信纸 8½ × 11 英寸
      3 卡片 11 × 17 英寸
      4 账单 17 × 11 英寸
      5 律师公文纸 8½ × 14 英寸
      6 报告单 5½ × 8½ 英寸
      7 行政公文纸 7½ × 10 英寸
      8 A3 297 × 420 毫米
      9 A4 210 × 297 毫米
      10 A4(小) 210 × 297 毫米
      11 A5 148 × 210 毫米
      12 B4 250 × 353 毫米
      13 B5 176 × 250 毫米
      14 对开本 8½ × 13 英寸
      15 四开 215 × 275 毫米
      16 美式标准纸张 10 × 14 英寸
      17 美式标准纸张 11 × 17 英寸
      18 Note paper 8.5 × 11 英寸
      19 信封 #9 3.875 × 8.875 英寸
      20 信封 #10 4-1/8 × 9½ 英寸
      21 信封 #11 4.5 × 10.375 英寸
      22 信封 #12 4.75 × 11 英寸
      23 信封 #14 5 × 11.5 英寸
      24 C paper 17 × 22 英寸
      25 D paper 22 × 34 英寸
      26 E paper 34 × 44 英寸
      27 信封 DL 110 × 220 毫米
      28 信封 C5 162 × 229 毫米
      29 信封 C3 324 × 458 毫米
      30 信封 C4 229 × 324 毫米
      31 信封 C6 114 × 162 毫米
      32 信封 C65 114 × 229 毫米
      33 信封 B4 250 × 353 毫米
      34 信封 B5 176 × 250 毫米
      35 信封 B6 176 × 125 毫米
      36 信封 Italy 110 × 230 毫米
      37 君主式信封 3.88 × 7.5 英寸
      38 信封 6 3/4 3.625 × 6.5 英寸
      39 US standard fanfold 14.875 × 11 英寸
      40 German standard fanfold 8.5 × 12 英寸
      41 German legal fanfold 8.5 × 13 英寸
      42 ISO B4 250 × 353 毫米
      43 日式明信片 100 × 148 毫米
      44 Standard paper 9 × 11 英寸
      45 Standard paper 10 × 11 英寸
      46 Standard paper 15 × 11 英寸
      47 邀请信 220 × 220 毫米
      50 Letter extra paper 9.275 × 12 英寸
      51 Legal extra paper 9.275 × 15 英寸
      52 Tabloid extra paper 11.69 × 18 英寸
      53 A4 extra paper 236 × 322 毫米
      54 Letter transverse paper 8.275 × 11 英寸
      55 A4 transverse paper 210 × 297 毫米
      56 Letter extra transverse paper 9.275 × 12 英寸
      57 SuperA/SuperA/A4 paper 227 × 356 毫米
      58 SuperB/SuperB/A3 paper 305 × 487 毫米
      59 Letter plus paper 8.5 × 12.69 英寸
      60 A4 plus paper 210 × 330 毫米
      61 A5 transverse paper 148 × 210 毫米
      62 JIS B5 transverse paper 182 × 257 毫米
      63 A3 extra paper 322 × 445 毫米
      64 A5 extra paper 174 × 235 毫米
      65 ISO B5 extra paper 201 × 276 毫米
      66 A2 420 × 594 毫米
      67 A3 transverse paper 297 × 420 毫米
      68 A3 extra transverse paper 322 × 445 毫米
      69 双层日式明信片 200 × 148 毫米
      70 A6 105 × 148 毫米
      71 日式信封 Kaku #2
      72 日式信封 Kaku #3
      73 日式信封 Chou #3
      74 日式信封 Chou #4
      75 Letter Rotated (11in x 8 1/2 11 in)
      76 A3 横向旋转 420 × 297 毫米
      77 A4 横向旋转 297 × 210 毫米
      78 A5 横向旋转 210 × 148 毫米
      79 B4 (JIS) 横向旋转 364 × 257 毫米
      80 B5 (JIS) 横向旋转 257 × 182 毫米
      81 日式明信片 横向旋转 148 × 100 毫米
      82 双层日式明信片 横向旋转 148 × 200 毫米
      83 A6 横向旋转 148 × 105 毫米
      84 日式信封 Kaku #2 横向旋转
      85 日式信封 Kaku #3 横向旋转
      86 日式信封 Chou #3 横向旋转
      87 日式信封 Chou #4 横向旋转
      88 B6 (JIS) 128 × 182 毫米
      89 B6 (JIS) 横向旋转 182 × 128 毫米
      90 12 × 11 英寸
      91 日式信封 You #4
      92 日式信封 You #4 横向旋转
      93 中式 16 开 146 × 215 毫米
      94 中式 32 开 97 × 151 毫米
      95 中式大 32 开 97 × 151 毫米
      96 中式信封 #1 102 × 165 毫米
      97 中式信封 #2 102 × 176 毫米
      98 中式信封 #3 125 × 176 毫米
      99 中式信封 #4 110 × 208 毫米
      100 中式信封 #5 110 × 220 毫米
      101 中式信封 #6 120 × 230 毫米
      102 中式信封 #7 160 × 230 毫米
      103 中式信封 #8 120 × 309 毫米
      104 中式信封 #9 229 × 324 毫米
      105 中式信封 #10 324 × 458 毫米
      106 中式 16 开 横向旋转
      107 中式 32 开 横向旋转
      108 中式大 32 开 横向旋转
      109 中式信封 #1 横向旋转 165 × 102 毫米
      110 中式信封 #2 横向旋转 176 × 102 毫米
      111 中式信封 #3 横向旋转 176 × 125 毫米
      112 中式信封 #4 横向旋转 208 × 110 毫米
      113 中式信封 #5 横向旋转 220 × 110 毫米
      114 中式信封 #6 横向旋转 230 × 120 毫米
      115 中式信封 #7 横向旋转 230 × 160 毫米
      116 中式信封 #8 横向旋转 309 × 120 毫米
      117 中式信封 #9 横向旋转 324 × 229 毫米
      118 中式信封 #10 横向旋转 458 × 324 毫米
    • 例如,将名为 Sheet1 的工作表页面布局设置为横向并使用 A4(小) 210 × 297 毫米纸张:

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    
    xl := excelize.NewFile()
    const sheet = "Sheet1"
    if err := xl.SetPageLayout(
        "Sheet1",
        excelize.PageLayoutOrientation(excelize.OrientationLandscape),
    ); err != nil {
        panic(err)
    }
    if err := xl.SetPageLayout(
        "Sheet1",
        excelize.PageLayoutPaperSize(10),
    ); err != nil {
        panic(err)
    }
    

获取工作表页面布局

1
func (f *File) GetPageLayout(sheet string, opts ...PageLayoutOptionPtr) error

根据给定的工作表名称和页面布局参数获取工作表的页面布局属性。

  • 通过 PageLayoutOrientation 方法获取页面布局方向
  • 通过 PageLayoutPaperSize 方法获取页面纸张大小 例如,获取名为 Sheet1 的工作表页面布局设置:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
xl := excelize.NewFile()
const sheet = "Sheet1"
var (
    orientation excelize.PageLayoutOrientation
    paperSize   excelize.PageLayoutPaperSize
)
if err := xl.GetPageLayout("Sheet1", &orientation); err != nil {
    panic(err)
}
if err := xl.GetPageLayout("Sheet1", &paperSize); err != nil {
    panic(err)
}
fmt.Println("Defaults:")
fmt.Printf("- orientation: %q\n", orientation)
fmt.Printf("- paper size: %d\n", paperSize)
// Output:
// Defaults:
// - orientation: "portrait"
// - paper size: 1

设置列可见性

1
func (f *File) SetColVisible(sheet, col string, visible bool) error

根据给定的工作表名称(大小写敏感)和列名称设置列可见性。例如隐藏名为 Sheet1 工作表上的 D 列:

1
err := f.SetColVisible("Sheet1", "D", false)

设置列宽度

1
func (f *File) SetColWidth(sheet, startcol, endcol string, width float64) error

根据给定的工作表名称(大小写敏感)、列范围和宽度值设置单个或多个列的宽度。例如设置名为 Sheet1 工作表上 AH 列的宽度为 20

1
2
f := excelize.NewFile()
err := f.SetColWidth("Sheet1","A","H",20)

设置行高度

1
func (f *File) SetRowHeight(sheet string, row int, height float64) error

根据给定的工作表名称(大小写敏感)、行号和高度值设置单行高度。例如设置名为 Sheet1 工作表首行的高度为 50

1
err := f.SetRowHeight("Sheet1", 1, 50)

设置行可见性

1
func (f *File) SetRowVisible(sheet string, row int, visible bool) error

根据给定的工作表名称(大小写敏感)和行号设置行可见性。例如隐藏名为 Sheet1 工作表上第二行:

1
err := f.SetRowVisible("Sheet1", 2, false)

获取工作表名

1
func (f *File) GetSheetName(index int) string

根据给定的工作表索引获取工作表名称,如果工作表不存在将返回空字符。

获取列可见性

1
func (f *File) GetColVisible(sheet, column string) (bool, error)

根据给定的工作表名称(大小写敏感)和列名获取工作表中指定列的可见性,可见返回值为 true,否则为 false。例如,获取名为 Sheet1 的工作表上 D 列的可见性:

1
visible, err := f.GetColVisible("Sheet1", "D")

获取列宽度

1
func (f *File) GetColWidth(sheet, col string) (float64, error)

根据给定的工作表和列名获取工作表中指定列的宽度。

获取行高度

1
func (f *File) GetRowHeight(sheet string, row int) (float64, error)

根据给定的工作表名称(大小写敏感)和行号获取工作表中指定行的高度。例如,获取名为 Sheet1 的工作表首行的高度:

1
height, err := f.GetRowHeight("Sheet1", 1)

获取行可见性

1
func (f *File) GetRowVisible(sheet string, row int) (bool, error)

根据给定的工作表名称(大小写敏感)和行号获取工作表中指定行的可见性。例如,获取名为 Sheet1 的工作表第 2 行的可见性:

1
err := f.GetRowVisible("Sheet1", 2)

获取工作表索引

1
func (f *File) GetSheetIndex(name string) int

根据给定的工作表名称(大小写敏感)获取该工作表的索引,如果工作表不存在将返回 0。获取到的索引可以在设置工作簿默认工作表时,作为调用 SetActiveSheet() 函数的参数使用。

获取工作表列表

1
func (f *File) GetSheetMap() map[int]string

获取工作簿中以名称和索引构成的全部工作表的列表。

1
2
3
4
5
6
7
f, err := excelize.OpenFile("./Book1.xlsx")
if err != nil {
    return
}
for index, name := range f.GetSheetMap() {
    fmt.Println(index, name)
}

获取工作表属性

1
func (f *File) GetSheetPrOptions(name string, opts ...SheetPrOptionPtr) error

根据给定的工作表名称(大小写敏感)和筛选想获取工作表属性。

可选属性 类型
CodeName string
EnableFormatConditionsCalculation bool
Published bool
FitToPage bool
AutoPageBreaks bool
OutlineSummaryBelow bool

例如:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
xl := excelize.NewFile()
const sheet = "Sheet1"
var (
    codeName                          excelize.CodeName
    enableFormatConditionsCalculation excelize.EnableFormatConditionsCalculation
    published                         excelize.Published
    fitToPage                         excelize.FitToPage
    autoPageBreaks                    excelize.AutoPageBreaks
    outlineSummaryBelow               excelize.OutlineSummaryBelow
)
if err := xl.GetSheetPrOptions(sheet,
    &codeName,
    &enableFormatConditionsCalculation,
    &published,
    &fitToPage,
    &autoPageBreaks,
    &outlineSummaryBelow,
); err != nil {
    panic(err)
}
fmt.Println("Defaults:")
fmt.Printf("- codeName: %q\n", codeName)
fmt.Println("- enableFormatConditionsCalculation:", enableFormatConditionsCalculation)
fmt.Println("- published:", published)
fmt.Println("- fitToPage:", fitToPage)
fmt.Println("- autoPageBreaks:", autoPageBreaks)
fmt.Println("- outlineSummaryBelow:", outlineSummaryBelow)

输出
Defaults:
- codeName: ""
- enableFormatConditionsCalculation: true
- published: true
- fitToPage: false
- autoPageBreaks: false
- outlineSummaryBelow: true

插入列

1
func (f *File) InsertCol(sheet, column string) error

根据给定的工作表名称(大小写敏感)和列名称,在指定列前插入空白列。例如,在名为 Sheet1 的工作表的 C 列前插入空白列:

1
err := f.InsertCol("Sheet1", "C")

插入行

1
func (f *File) InsertRow(sheet string, row int) error

根据给定的工作表名称(大小写敏感)和行号,在指定行前插入空白行。例如,在名为 Sheet1 的工作表的第 3 行前插入空白行:

1
err := f.InsertRow("Sheet1", 3)

追加复制行

1
func (f *File) DuplicateRow(sheet string, row int) error

根据给定的工作表名称(大小写敏感)和行号,在该行后追加复制。例如,将名为 Sheet1 的工作表的第 2 行复制到第 3 行:

1
err := f.DuplicateRow("Sheet1", 2)

请谨慎使用此方法,这将影响所有对该工作表中原有公式、图表等资源引用的更改。如果该工作表包含任何引用值,在使用此方法后使用 Excel 应用程序打开它时将可能导致文件错误。excelize 目前仅支持对工作表上部分引用对更新。

复制行

1
func (f *File) DuplicateRowTo(sheet string, row, row2 int) error

根据给定的工作表名称(大小写敏感)和行号,在指定行后复制该行。例如,将名为 Sheet1 的工作表的第 2 行后复制到第 7 行:

1
err := f.DuplicateRowTo("Sheet1", 2, 7)

请谨慎使用此方法,这将影响所有对该工作表中原有公式、图表等资源引用的更改。如果该工作表包含任何引用值,在使用此方法后使用 Excel 应用程序打开它时将可能导致文件错误。excelize 目前仅支持对工作表上部分引用对更新。

创建行的分级显示

1
func (f *File) SetRowOutlineLevel(sheet string, row int, level uint8) error

根据给定的工作表名称(大小写敏感)、行号和分级参数创建组。例如,在名为 Sheet1 的工作表的第 2 行创建 1 级分组。

1
err := f.SetRowOutlineLevel("Sheet1", 2, 1)

创建列的分级显示

1
func (f *File) SetColOutlineLevel(sheet, col string, level uint8) error

根据给定的工作表名称(大小写敏感)、列名称和分级参数创建组。例如,在名为 Sheet1 的工作表的 D 列创建 2 级分组。

1
err := f.SetColOutlineLevel("Sheet1", "D", 2)

获取行的分级显示

1
func (f *File) GetRowOutlineLevel(sheet string, row int) (uint8, error)

根据给定的工作表名称(大小写敏感)和行号获取分组级别。例如,获取名为 Sheet1 的工作表第 2 行的分组级别。

1
err := f.GetRowOutlineLevel("Sheet1", 2)

获取列的分级显示

1
func (f *File) GetColOutlineLevel(sheet, col string) (uint8, error)

根据给定的工作表名称(大小写敏感)和列名称获取分组分级。例如,获取名为 Sheet1 的工作表的 D 列的分组级别。

1
level, err := f.GetColOutlineLevel("Sheet1", "D")

行迭代器

1
func (f *File) Rows(sheet string) (*Rows, error)

根据给定的工作表名称(大小写敏感)获取该工作表的行迭代器。使用行迭代器遍历单元格:

1
2
3
4
5
6
7
8
rows, err := f.Rows("Sheet1")
for rows.Next() {
   row, err := rows.Columns()
   for _, colCell := range row {
       fmt.Print(colCell, "\t")
   }
   fmt.Println()
}

行迭代器 - 单行操作

1
func (rows *Rows) Columns() ([]string, error)

返回当前行所有列的值。

行迭代器 - 遍历操作

1
func (rows *Rows) Next() bool

如果下一行有值存在将返回 true

行迭代器 - 错误处理

1
func (rows *Rows) Error() error

当查找下一行出现错误时将返回 error

在工作表中搜索

1
func (f *File) SearchSheet(sheet, value string, reg ...bool) ([]string, error)

根据给定的工作表名称(大小写敏感),单元格值或正则表达式来获取坐标。此函数仅支持字符串和数字的完全匹配,不支持公式计算后的结果、格式化数字和条件搜索。如果搜索结果是合并的单元格,将返回合并区域左上角的坐标。

例如,在名为 Sheet1 的工作表中搜索值 100 的坐标:

1
result, err := f.SearchSheet("Sheet1", "100")

例如,在名为 Sheet1 的工作表中搜索 0-9 范围内数值的坐标:

1
result, err := f.SearchSheet("Sheet1", "[0-9]", true)

保护工作表

1
func (f *File) ProtectSheet(sheet string, settings *FormatSheetProtection) error

防止其他用户意外或有意更改、移动或删除工作表中的数据。例如,为名为 Sheet1 的工作表设置密码保护,但是允许选择锁定的单元格、选择未锁定的单元格、编辑方案:

1
2
3
4
err := f.ProtectSheet("Sheet1", &excelize.FormatSheetProtection{
    Password:      "password",
    EditScenarios: false,
})

取消保护工作表

1
func (f *File) UnprotectSheet(sheet string) error

根据给定的工作表名称(大小写敏感)取消保护该工作表。

单元格

设置单元格的值

1
func (f *File)SetCellValue(sheet, axis string, value interface{}) error

根据给定的工作表名和单元格坐标设置单元格的值。

支持的数据类型
int
int8
int16
int32
int64
uint
uint8
uint16
uint32
uint64
float32
float64
string
[]byte
time.Duration
time.Time
bool
nil

设置布尔型值

1
func (f *File) SetCellBool(sheet, axis string, value bool) error

根据给定的工作表名和单元格坐标设置布尔型单元格的值。

设置默认字符型值

1
func (f *File) SetCellDefault(sheet, axis, value string) error

根据给定的工作表名和单元格坐标设置字符型单元格的值,字符将不会进行特殊字符过滤。

设置实数

1
func (f *File) SetCellInt(sheet, axis string, value int) error

根据给定的工作表名和单元格坐标设置实数单元格的值。

设置字符型值

1
func (f *File) SetCellStr(sheet, axis, value string) error

根据给定的工作表名和单元格坐标设置字符型单元格的值,字符将会进行特殊字符过滤,并且字符串的累计长度应不超过 32767,多余的字符将会被忽略。

设置整行单元格

1
func (f *File) SetSheetRow(sheet, axis string, slice interface{}) 

如下:在A1处开始插入整行

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
f.SetSheetRow(name,"A1",&[]interface{}{
			v.CharacterId,
			v.ProvinceName,
			v.CompanyCode,
			v.CompanyName,
			v.StoreCode,
			v.StoreName,
			v.Address,
			v.Contact,
			v.Contact1,
			v.Expireb,
			v.Expiree,
		})

设置单元格样式

1
func (f *File) SetCellStyle(sheet, hcell, vcell string, styleID int) error

根据给定的工作表名、单元格坐标区域和样式索引设置单元格的值。样式索引可以通过 NewStyle 函数获取。注意,在同一个坐标区域内的 diagonalDowndiagonalUp 需要保持颜色一致。

  • 例1,为名为 Sheet1 的工作表 D7 单元格设置边框样式:
1
2
3
4
5
style, err := f.NewStyle(`{"border":[{"type":"left","color":"0000FF","style":3},{"type":"top","color":"00FF00","style":4},{"type":"bottom","color":"FFFF00","style":5},{"type":"right","color":"FF0000","style":6},{"type":"diagonalDown","color":"A020F0","style":7},{"type":"diagonalUp","color":"A020F0","style":8}]}`)
if err != nil {
    fmt.Println(err)
}
err = f.SetCellStyle("Sheet1", "D7", "D7", style)

单元格 D7 的四个边框被设置了不同的样式和颜色,这与调用 NewStyle 函数时的参数有关,需要设置不同的样式可参考该章节的文档。

  • 例2,为名为 Sheet1 的工作表 D7 单元格设置渐变样式:
1
2
3
4
5
style, err := f.NewStyle(`{"fill":{"type":"gradient","color":["#FFFFFF","#E0EBF5"],"shading":1}}`)
if err != nil {
    fmt.Println(err)
}
err = f.SetCellStyle("Sheet1", "D7", "D7", style)

单元格 D7 被设置了渐变效果的颜色填充,渐变填充效果与调用 NewStyle 函数时的参数有关,需要设置不同的样式可参考该章节的文档。

  • 例3,为名为 Sheet1 的工作表 D7 单元格设置纯色填充:
1
2
3
4
5
style, err := f.NewStyle(`{"fill":{"type":"pattern","color":["#E0EBF5"],"pattern":1}}`)
if err != nil {
    fmt.Println(err)
}
err = f.SetCellStyle("Sheet1", "D7", "D7", style)

单元格 D7 被设置了纯色填充。

  • 例4,为名为 Sheet1 的工作表 D7 单元格设置字符间距与旋转角度:
1
2
3
4
5
6
f.SetCellValue("Sheet1", "D7", "样式")
style, err := f.NewStyle(`{"alignment":{"horizontal":"center","ident":1,"justify_last_line":true,"reading_order":0,"relative_indent":1,"shrink_to_fit":true,"text_rotation":45,"vertical":"","wrap_text":true}}`)
if err != nil {
    fmt.Println(err)
}
err = f.SetCellStyle("Sheet1", "D7", "D7", style)
  • 例5,Excel 中的日期和时间用实数表示,例如 2017/7/4 12:00:00 PM 可以用数字 42920.5 来表示。为名为 Sheet1 的工作表 D7 单元格设置时间格式:
1
2
3
4
5
6
7
f.SetCellValue("Sheet1", "D7", 42920.5)
f.SetColWidth("Sheet1", "D", "D", 13)
style, err := f.NewStyle(`{"number_format": 22}`)
if err != nil {
    fmt.Println(err)
}
err = f.SetCellStyle("Sheet1", "D7", "D7", style)

单元格 D7 被设置了时间格式。注意,当应用了时间格式的单元格宽度过窄无法完整展示时会显示为 ####,可以拖拽调整列宽或者通过调用 SetColWidth 函数设置列款到合适的大小使其正常显示。

  • 例6,为名为 Sheet1 的工作表 D7 单元格设置字体、字号、颜色和倾斜样式:
1
2
3
4
5
6
f.SetCellValue("Sheet1", "D7", "Excel")
style, err := f.NewStyle(`{"font":{"bold":true,"italic":true,"family":"Berlin Sans FB Demi","size":36,"color":"#777777"}}`)
if err != nil {
    fmt.Println(err)
}
err = f.SetCellStyle("Sheet1", "D7", "D7", style)
  • 例7,锁定并隐藏名为 Sheet1 的工作表 D7 单元格:
1
2
3
4
5
style, err := f.NewStyle(`{"protection":{"hidden":true, "locked":true}}`)
if err != nil {
    fmt.Println(err)
}
err = f.SetCellStyle("Sheet1", "D7", "D7", style)

要锁定单元格或隐藏公式,请保护工作表。在“审阅”选项卡上,单击“保护工作表”

设置超链接

1
func (f *File) SetCellHyperLink(sheet, axis, link, linkType string) error

根据给定的工作表、单元格坐标、链接资源和资源类型设置单元格的超链接。资源类型分为外部链接地址 External 和工作簿内部位置链接 Location 两种。每个工作表中的包含最大超链接限制为 65530 个。

  • 例1,为名为 Sheet1 的工作表 A3 单元格添加外部链接:
1
2
3
4
err := f.SetCellHyperLink("Sheet1", "A3", "https://github.com/360EntSecGroup-Skylar/excelize", "External")
// 为单元格设置字体和下划线样式
style, err := f.NewStyle(`{"font":{"color":"#1265BE","underline":"single"}}`)
err = f.SetCellStyle("Sheet1", "A3", "A3", style)
  • 例2,为名为 Sheet1 的工作表 A3 单元格添加内部位置链接:
1
err := f.SetCellHyperLink("Sheet1", "A3", "Sheet1!A40", "Location")

获取单元格的值

1
func (f *File) GetCellValue(sheet, axis string) (string, error)

根据给定的工作表和单元格坐标获取单元格的值,返回值将转换为 string 类型。如果可以将单元格格式应用于单元格的值,将返回应用后的值,否则将返回原始值。

获取全部单元格的值

1
func (f *File) GetRows(sheet string) ([][]string, error)

根据给定的工作表名(大小写敏感)获取该工作表上全部单元格的值,以二维数组形式返回,其中单元格的值将转换为 string 类型。如果可以将单元格格式应用于单元格的值,将使用应用后的值,否则将使用原始值。

例如,获取并遍历输出名为 Sheet1 的工作表上的所有单元格的值:

1
2
3
4
5
6
7
rows, err := f.GetRows("Sheet1")
for _, row := range rows {
    for _, colCell := range row {
        fmt.Print(colCell, "\t")
    }
    fmt.Println()
}

获取超链接

1
func (f *File) GetCellHyperLink(sheet, axis string) (bool, string, error)

根据给定的工作表名(大小写敏感)和单元格坐标获取单元格超链接,如果该单元格存在超链接,将返回 true 和链接地址,否则将返回 false 和空的链接地址。

例如,获取名为 Sheet1 的工作表上坐标为 H6 单元格的超链接:

1
link, target, err := f.GetCellHyperLink("Sheet1", "H6")

获取样式索引

1
func (f *File) GetCellStyle(sheet, axis string) (int, error)

根据给定的工作表名(大小写敏感)和单元格坐标获取单元格样式索引,获取到的索引可以在复制单元格样式时,作为调用 SetCellValue 函数的参数使用。

合并单元格

1
func (f *File) MergeCell(sheet, hcell, vcell string) error

根据给定的工作表名(大小写敏感)和单元格坐标区域合并单元格。例如,合并名为 Sheet1 的工作表上 D3:E9 区域内的单元格:

1
err := f.MergeCell("Sheet1", "D3", "E9")

如果给定的单元格坐标区域与已有的其他合并单元格相重叠,已有的合并单元格将会被删除。

获取合并单元格

根据给定的工作表名(大小写敏感)获取全部合并单元格的坐标区域和值。

1
func (f *File) GetMergeCells(sheet string) ([]MergeCell, error)

添加批注

1
func (f *File) AddComment(sheet, cell, format string) error

根据给定的工作表名称、单元格坐标和样式参数(作者与文本信息)添加批注。作者信息最大长度为 255 个字符,最大文本内容长度为 32512 个字符,超出该范围的字符将会被忽略。例如,为 Sheet1!$A$3 单元格添加批注:

1
err := f.AddComment("Sheet1", "A3", `{"author":"Excelize: ","text":"This is a comment."}`)

获取批注

1
func (f *File) GetComments() (comments map[string][]Comment)

通过该方法可以获取全部工作表中的批注。

设置公式

1
func (f *File) SetCellFormula(sheet, axis, formula string)

根据给定的工作表名(大小写敏感)和单元格坐设置取该单元格上的公式。公式的结果会在工作表被 Office Excel 应用程序打开时计算,Excelize 目前不提供公式计算引擎,所以无法计算公式结果。

获取公式

1
func (f *File) GetCellFormula(sheet, axis string) (string, error)

根据给定的工作表名(大小写敏感)和单元格坐标获取该单元格上的公式。

添加图表

1
func (f *File) AddChart(sheet, cell, format string) error

根据给定的工作表名称、单元格坐标和图表样式属性插入图表。

下面是 Excelize 支持创建的图表类型 type

名称 图表类型
area 二维面积图
areaStacked 二维堆积面积图
areaPercentStacked 二维百分比堆积面积图
area3D 三维面积图
area3DStacked 三维堆积面积图
area3DPercentStacked 三维百分比堆积面积图
bar 二维簇状条形图
barStacked 二维堆积条形图
barPercentStacked 二维百分比堆积条形图
bar3DClustered 三维簇状条形图
bar3DStacked 三维堆积条形图
bar3DPercentStacked 三维百分比堆积条形图
col 二维簇状柱形图
colStacked 二维堆积柱形图
colPercentStacked 二维百分比堆积柱形图
col3DClustered 三维簇状柱形图
col3D 三维柱形图
col3DStacked 三维堆积柱形图
col3DPercentStacked 三维百分比堆积柱形图
doughnut 圆环图
line 折线图
pie 饼图
pie3D 三维饼图
radar 雷达图
scatter 散点图

在 Office Excel 中图表数据区域 series 指定了绘制哪些数据的信息集合、图例项(系列)和水平(分类)轴标签。

下面是 Excelize 中 series 的可选参数:

参数 含义
name 图例项(系列),在图表图例和公式栏中显示。name 参数是可选的,如果不指定该值默认将会使用 Series 1 .. n 表示。name 支持使用公式表示,例如:Sheet1!$A$1
categories 水平(分类)轴标签。在大多数图表类型中,categories 属性是可选的,默认为形如 1..n 的连续序列。
values 图表数据区域,是 series 中最重要的参数,也是创建图表时唯一的必选参数。该选项将图表与其显示的工作表数据链接起来。

参数 legend 提供对图例项的属性设置方法,下面是 Excelize 中 legend 的可选参数:

参数 类型 含义
position string 图例位置
show_legend_key bool 显示图例,但不与图表重叠

其中参数 position 默认值为 right,下面是可选值:

可选值 含义
top 靠上
bottom 靠下
left 靠左
right 靠右
top_right 右上

其中参数 show_legend_key 默认值为 false

通过可选 title 对象的 name 参数设置图表标题,标题将会在图表上方显示。参数 name 支持使用公式表示,例如 Sheet1!$A$1,如果不指定图标标题默认值为空。

参数 show_blanks_as 提供“隐藏和清空单元格”设置,默认值为: gap 即“空单元格显示为”:“空距”。下面是该参数的可选值:

含义
gap 空距
span 用直线连接数据点
zero 零值

参数 format 提供对图表偏移、缩放、高宽比设置和打印属性等参数的设置,其参数与在 AddPicture() 函数中所使用的相同。

通过可选 plotarea 对象设置数据标签格式,可选参数如下:

参数 类型 默认值 含义
show_bubble_size bool false 气泡大小
show_cat_name bool true 类别名称
show_leader_lines bool false 显示引导线
show_percent bool false 百分比
show_series_name bool false 系列名称
show_val bool false

通过参数 x_axisy_axis 参数设置坐标轴选项。下面是该参数的可选值:

参数 类型 默认值 含义
reverse_order bool false 逆序刻度值
maximum int 0 最大值,0 代表自动
minimum int 0 最小值,0 代表自动

通过可选 dimension 对象设置图表的大小,可选参数如下:

参数 类型 默认值 含义
height int 290 高度
width int 480 宽度

二维面积图

例如,创建如下效果的二维面积图:

https://www.xieys.club/images/posts/82b35549167ca765457735345bdaf043.png https://www.xieys.club/images/posts/82b35549167ca765457735345bdaf043.png

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package main
import (
    "fmt"
    "github.com/360EntSecGroup-Skylar/excelize/v2"
)
func main() {
    categories := map[string]string{"A2": "Small", "A3": "Normal", "A4": "Large", "B1": "Apple", "C1": "Orange", "D1": "Pear"}
    values := map[string]int{"B2": 7, "C2": 7, "D2": 8, "B3": 5, "C3": 4, "D3": 4, "B4": 2, "C4": 3, "D4": 3}
    f := excelize.NewFile()
    for k, v := range categories {
        f.SetCellValue("Sheet1", k, v)
    }
    for k, v := range values {
        f.SetCellValue("Sheet1", k, v)
    }
    err := f.AddChart("Sheet1", "E1", `{"type":"area","series":[{"name":"Sheet1!$A$2","categories":"","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"}],"format":{"x_scale":1.0,"y_scale":1.0,"x_offset":15,"y_offset":10,"print_obj":true,"lock_aspect_ratio":false,"locked":false},"legend":{"position":"bottom","show_legend_key":false},"title":{"name":"Fruit 2D Area Chart"},"plotarea":{"show_bubble_size":true,"show_cat_name":false,"show_leader_lines":false,"show_percent":true,"show_series_name":true,"show_val":true},"show_blanks_as":"zero"}`)
    if err != nil {
        fmt.Println(err)
    }
    // 保存工作簿
    err = f.SaveAs("./Book1.xlsx")
    if err != nil {
        fmt.Println(err)
    }
}

二维堆积面积图

例如,创建如下效果的二维堆积面积图:

https://www.xieys.club/images/posts/df7af7f5a29c2f9d17cdc490659b1d28.png https://www.xieys.club/images/posts/df7af7f5a29c2f9d17cdc490659b1d28.png

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package main
import (
    "fmt"
    "github.com/360EntSecGroup-Skylar/excelize/v2"
)
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}
    f := excelize.NewFile()
    for k, v := range categories {
        f.SetCellValue("Sheet1", k, v)
    }
    for k, v := range values {
        f.SetCellValue("Sheet1", k, v)
    }
    err := f.AddChart("Sheet1", "E1", `{"type":"areaStacked","series":[{"name":"Sheet1!$A$2","categories":"","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"}],"format":{"x_scale":1.0,"y_scale":1.0,"x_offset":15,"y_offset":10,"print_obj":true,"lock_aspect_ratio":false,"locked":false},"legend":{"position":"bottom","show_legend_key":false},"title":{"name":"Fruit 2D Stacked Area Chart"},"plotarea":{"show_bubble_size":true,"show_cat_name":false,"show_leader_lines":false,"show_percent":true,"show_series_name":true,"show_val":true},"show_blanks_as":"zero"}`)
    if err != nil {
        fmt.Println(err)
    }
    // 保存工作簿
    err = f.SaveAs("./Book1.xlsx")
    if err != nil {
        fmt.Println(err)
    }
}

三维面积图

例如,创建如下效果的三维面积图:

https://www.xieys.club/images/posts/54d85fe3af386b64c30a940ce1344396.png https://www.xieys.club/images/posts/54d85fe3af386b64c30a940ce1344396.png

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package main
import (
    "fmt"
    "github.com/360EntSecGroup-Skylar/excelize/v2"
)
func main() {
    categories := map[string]string{"A2": "Small", "A3": "Normal", "A4": "Large", "B1": "Apple", "C1": "Orange", "D1": "Pear"}
    values := map[string]int{"B2": 7, "C2": 7, "D2": 8, "B3": 5, "C3": 4, "D3": 4, "B4": 2, "C4": 3, "D4": 3}
    f := excelize.NewFile()
    for k, v := range categories {
        f.SetCellValue("Sheet1", k, v)
    }
    for k, v := range values {
        f.SetCellValue("Sheet1", k, v)
    }
    err := f.AddChart("Sheet1", "E1", `{"type":"area3D","series":[{"name":"Sheet1!$A$2","categories":"","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"}],"format":{"x_scale":1.0,"y_scale":1.0,"x_offset":15,"y_offset":10,"print_obj":true,"lock_aspect_ratio":false,"locked":false},"legend":{"position":"bottom","show_legend_key":false},"title":{"name":"Fruit 3D Area Chart"},"plotarea":{"show_bubble_size":true,"show_cat_name":false,"show_leader_lines":false,"show_percent":true,"show_series_name":true,"show_val":true},"show_blanks_as":"zero"}`)
    if err != nil {
        fmt.Println(err)
    }
    // 保存工作簿
    err = f.SaveAs("./Book1.xlsx")
    if err != nil {
        fmt.Println(err)
    }
}

三维堆积面积图

例如,创建如下效果的三维堆积面积图:

https://www.xieys.club/images/posts/aac7f41b8f2063e0c66806f9f459202b.png https://www.xieys.club/images/posts/aac7f41b8f2063e0c66806f9f459202b.png

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package main
import (
    "fmt"
    "github.com/360EntSecGroup-Skylar/excelize/v2"
)
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}
    f := excelize.NewFile()
    for k, v := range categories {
        f.SetCellValue("Sheet1", k, v)
    }
    for k, v := range values {
        f.SetCellValue("Sheet1", k, v)
    }
    err := f.AddChart("Sheet1", "E1", `{"type":"area3DStacked","series":[{"name":"Sheet1!$A$2","categories":"","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"}],"format":{"x_scale":1.0,"y_scale":1.0,"x_offset":15,"y_offset":10,"print_obj":true,"lock_aspect_ratio":false,"locked":false},"legend":{"position":"bottom","show_legend_key":false},"title":{"name":"Fruit 3D Stacked Area Chart"},"plotarea":{"show_bubble_size":true,"show_cat_name":false,"show_leader_lines":false,"show_percent":true,"show_series_name":true,"show_val":true},"show_blanks_as":"zero"}`)
    if err != nil {
        fmt.Println(err)
    }
    // 保存工作簿
    err = f.SaveAs("./Book1.xlsx")
    if err != nil {
        fmt.Println(err)
    }
}

三维百分比堆积面积图

例如,创建如下效果的三维百分比堆积面积图:

https://www.xieys.club/images/posts/cf48a9c9d3eefefd15b13bd5d753004e.png https://www.xieys.club/images/posts/cf48a9c9d3eefefd15b13bd5d753004e.png

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package main
import (
    "fmt"
    "github.com/360EntSecGroup-Skylar/excelize/v2"
)
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}
    f := excelize.NewFile()
    for k, v := range categories {
        f.SetCellValue("Sheet1", k, v)
    }
    for k, v := range values {
        f.SetCellValue("Sheet1", k, v)
    }
    err := f.AddChart("Sheet1", "E1", `{"type":"area3DPercentStacked","series":[{"name":"Sheet1!$A$2","categories":"","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"}],"format":{"x_scale":1.0,"y_scale":1.0,"x_offset":15,"y_offset":10,"print_obj":true,"lock_aspect_ratio":false,"locked":false},"legend":{"position":"bottom","show_legend_key":false},"title":{"name":"Fruit 3D 100% Stacked Area Chart"},"plotarea":{"show_bubble_size":true,"show_cat_name":false,"show_leader_lines":false,"show_percent":true,"show_series_name":true,"show_val":true},"show_blanks_as":"zero"}`)
    if err != nil {
        fmt.Println(err)
    }
    // 保存工作簿
    err = f.SaveAs("./Book1.xlsx")
    if err != nil {
        fmt.Println(err)
    }
}

二维簇状条形图

例如,创建如下效果的二维簇状条形图:

https://www.xieys.club/images/posts/65d1de0148334802ca5a50a28a542160.png https://www.xieys.club/images/posts/65d1de0148334802ca5a50a28a542160.png

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package main
import (
    "fmt"
    "github.com/360EntSecGroup-Skylar/excelize/v2"
)
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}
    f := excelize.NewFile()
    for k, v := range categories {
        f.SetCellValue("Sheet1", k, v)
    }
    for k, v := range values {
        f.SetCellValue("Sheet1", k, v)
    }
    err := f.AddChart("Sheet1", "E1", `{"type":"bar","series":[{"name":"Sheet1!$A$2","categories":"","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"}],"format":{"x_scale":1.0,"y_scale":1.0,"x_offset":15,"y_offset":10,"print_obj":true,"lock_aspect_ratio":false,"locked":false},"legend":{"position":"left","show_legend_key":false},"title":{"name":"Fruit 2D Clustered Bar Chart"},"plotarea":{"show_bubble_size":true,"show_cat_name":false,"show_leader_lines":false,"show_percent":true,"show_series_name":true,"show_val":true},"show_blanks_as":"zero"}`)
    if err != nil {
        fmt.Println(err)
    }
    // 保存工作簿
    err = f.SaveAs("./Book1.xlsx")
    if err != nil {
        fmt.Println(err)
    }
}

二维堆积条形图

例如,创建如下效果的二维堆积条形图:

https://www.xieys.club/images/posts/6300c3be6054214fe9cb2337885e2ec3.png https://www.xieys.club/images/posts/6300c3be6054214fe9cb2337885e2ec3.png

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package main
import (
    "fmt"
    "github.com/360EntSecGroup-Skylar/excelize/v2"
)
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}
    f := excelize.NewFile()
    for k, v := range categories {
        f.SetCellValue("Sheet1", k, v)
    }
    for k, v := range values {
        f.SetCellValue("Sheet1", k, v)
    }
    err := f.AddChart("Sheet1", "E1", `{"type":"barStacked","series":[{"name":"Sheet1!$A$2","categories":"","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"}],"format":{"x_scale":1.0,"y_scale":1.0,"x_offset":15,"y_offset":10,"print_obj":true,"lock_aspect_ratio":false,"locked":false},"legend":{"position":"left","show_legend_key":false},"title":{"name":"Fruit 2D Stacked Bar Chart"},"plotarea":{"show_bubble_size":true,"show_cat_name":false,"show_leader_lines":false,"show_percent":true,"show_series_name":true,"show_val":true},"show_blanks_as":"zero"}`)
    if err != nil {
        fmt.Println(err)
    }
    // 保存工作簿
    err = f.SaveAs("./Book1.xlsx")
    if err != nil {
        fmt.Println(err)
    }
}

二维百分比堆积条形图

例如,创建如下效果的二维百分比堆积条形图:

https://www.xieys.club/images/posts/d79a8a12a58f8df8f10497f148588287.png https://www.xieys.club/images/posts/d79a8a12a58f8df8f10497f148588287.png

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package main
import (
    "fmt"
    "github.com/360EntSecGroup-Skylar/excelize/v2"
)
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}
    f := excelize.NewFile()
    for k, v := range categories {
        f.SetCellValue("Sheet1", k, v)
    }
    for k, v := range values {
        f.SetCellValue("Sheet1", k, v)
    }
    err := f.AddChart("Sheet1", "E1", `{"type":"barPercentStacked","series":[{"name":"Sheet1!$A$2","categories":"","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"}],"format":{"x_scale":1.0,"y_scale":1.0,"x_offset":15,"y_offset":10,"print_obj":true,"lock_aspect_ratio":false,"locked":false},"legend":{"position":"left","show_legend_key":false},"title":{"name":"Fruit 2D Stacked 100% Bar Chart"},"plotarea":{"show_bubble_size":true,"show_cat_name":false,"show_leader_lines":false,"show_percent":true,"show_series_name":true,"show_val":true},"show_blanks_as":"zero"}`)
    if err != nil {
        fmt.Println(err)
    }
    // 保存工作簿
    err = f.SaveAs("./Book1.xlsx")
    if err != nil {
        fmt.Println(err)
    }
}

三维簇状条形图

例如,创建如下效果的三维簇状条形图:

https://www.xieys.club/images/posts/03410ea1b1be72912a558e8aadb485c2.png https://www.xieys.club/images/posts/03410ea1b1be72912a558e8aadb485c2.png

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package main
import (
    "fmt"
    "github.com/360EntSecGroup-Skylar/excelize/v2"
)
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}
    f := excelize.NewFile()
    for k, v := range categories {
        f.SetCellValue("Sheet1", k, v)
    }
    for k, v := range values {
        f.SetCellValue("Sheet1", k, v)
    }
    err := f.AddChart("Sheet1", "E1", `{"type":"bar3DClustered","series":[{"name":"Sheet1!$A$2","categories":"","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"}],"format":{"x_scale":1.0,"y_scale":1.0,"x_offset":15,"y_offset":10,"print_obj":true,"lock_aspect_ratio":false,"locked":false},"legend":{"position":"left","show_legend_key":false},"title":{"name":"Fruit 3D Clustered Bar Chart"},"plotarea":{"show_bubble_size":true,"show_cat_name":false,"show_leader_lines":false,"show_percent":true,"show_series_name":true,"show_val":true},"show_blanks_as":"zero"}`)
    if err != nil {
        fmt.Println(err)
    }
    // 保存工作簿
    err = f.SaveAs("./Book1.xlsx")
    if err != nil {
        fmt.Println(err)
    }
}

三维堆积条形图

例如,创建如下效果的三维堆积条形图:

https://www.xieys.club/images/posts/87975753f44183dde6f8a7ab47247902.png https://www.xieys.club/images/posts/87975753f44183dde6f8a7ab47247902.png

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package main
import (
    "fmt"
    "github.com/360EntSecGroup-Skylar/excelize/v2"
)
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}
    f := excelize.NewFile()
    for k, v := range categories {
        f.SetCellValue("Sheet1", k, v)
    }
    for k, v := range values {
        f.SetCellValue("Sheet1", k, v)
    }
    err := f.AddChart("Sheet1", "E1", `{"type":"bar3DStacked","series":[{"name":"Sheet1!$A$2","categories":"","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"}],"format":{"x_scale":1.0,"y_scale":1.0,"x_offset":15,"y_offset":10,"print_obj":true,"lock_aspect_ratio":false,"locked":false},"legend":{"position":"left","show_legend_key":false},"title":{"name":"Fruit 3D Stacked Bar Chart"},"plotarea":{"show_bubble_size":true,"show_cat_name":false,"show_leader_lines":false,"show_percent":true,"show_series_name":true,"show_val":true},"show_blanks_as":"zero"}`)
    if err != nil {
        fmt.Println(err)
    }
    // 保存工作簿
    err = f.SaveAs("./Book1.xlsx")
    if err != nil {
        fmt.Println(err)
    }
}

三维百分比堆积条形图

例如,创建如下效果的三维百分比堆积条形图:

https://www.xieys.club/images/posts/2702c790e4df75511ba4f8d0bb7261d1.png https://www.xieys.club/images/posts/2702c790e4df75511ba4f8d0bb7261d1.png

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package main
import (
    "fmt"
    "github.com/360EntSecGroup-Skylar/excelize/v2"
)
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}
    f := excelize.NewFile()
    for k, v := range categories {
        f.SetCellValue("Sheet1", k, v)
    }
    for k, v := range values {
        f.SetCellValue("Sheet1", k, v)
    }
    err := f.AddChart("Sheet1", "E1", `{"type":"bar3DPercentStacked","series":[{"name":"Sheet1!$A$2","categories":"","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"}],"format":{"x_scale":1.0,"y_scale":1.0,"x_offset":15,"y_offset":10,"print_obj":true,"lock_aspect_ratio":false,"locked":false},"legend":{"position":"left","show_legend_key":false},"title":{"name":"Fruit 3D 100% Stacked Bar Chart"},"plotarea":{"show_bubble_size":true,"show_cat_name":false,"show_leader_lines":false,"show_percent":true,"show_series_name":true,"show_val":true},"show_blanks_as":"zero"}`)
    if err != nil {
        fmt.Println(err)
    }
    // 保存工作簿
    err = f.SaveAs("./Book1.xlsx")
    if err != nil {
        fmt.Println(err)
    }
}

二维簇状柱形图

例如,创建如下效果的二维簇状柱形图:

https://www.xieys.club/images/posts/78c8332baa86143f6d69f56f56f29d2d.png https://www.xieys.club/images/posts/78c8332baa86143f6d69f56f56f29d2d.png

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package main
import (
    "fmt"
    "github.com/360EntSecGroup-Skylar/excelize/v2"
)
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}
    f := excelize.NewFile()
    for k, v := range categories {
        f.SetCellValue("Sheet1", k, v)
    }
    for k, v := range values {
        f.SetCellValue("Sheet1", k, v)
    }
    err := f.AddChart("Sheet1", "E1", `{"type":"col","series":[{"name":"Sheet1!$A$2","categories":"","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"}],"format":{"x_scale":1.0,"y_scale":1.0,"x_offset":15,"y_offset":10,"print_obj":true,"lock_aspect_ratio":false,"locked":false},"legend":{"position":"left","show_legend_key":false},"title":{"name":"Fruit 2D Column Chart"},"plotarea":{"show_bubble_size":true,"show_cat_name":false,"show_leader_lines":false,"show_percent":true,"show_series_name":true,"show_val":true},"show_blanks_as":"zero"}`)
    if err != nil {
        fmt.Println(err)
    }
    // 保存工作簿
    err = f.SaveAs("./Book1.xlsx")
    if err != nil {
        fmt.Println(err)
    }
}

三维簇状柱形图

例如,创建如下效果的三维簇状柱形图:

https://www.xieys.club/images/posts/a5f2adbcca7a140527723a6f2910937b.png https://www.xieys.club/images/posts/a5f2adbcca7a140527723a6f2910937b.png

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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}
    f := excelize.NewFile()
    for k, v := range categories {
        f.SetCellValue("Sheet1", k, v)
    }
    for k, v := range values {
        f.SetCellValue("Sheet1", k, v)
    }
    err := f.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"}],"format":{"x_scale":1.0,"y_scale":1.0,"x_offset":15,"y_offset":10,"print_obj":true,"lock_aspect_ratio":false,"locked":false},"legend":{"position":"bottom","show_legend_key":false},"title":{"name":"Fruit 3D Clustered Column Chart"},"plotarea":{"show_bubble_size":true,"show_cat_name":false,"show_leader_lines":false,"show_percent":true,"show_series_name":true,"show_val":true},"show_blanks_as":"zero","x_axis":{"reverse_order":true},"y_axis":{"maximum":7.5,"minimum":0.5}}`)
    if err != nil {
        fmt.Println(err)
    }
    // 保存工作簿
    err = f.SaveAs("./Book1.xlsx")
    if err != nil {
        fmt.Println(err)
    }
}

圆环图

例如,创建如下效果的圆环图:

https://www.xieys.club/images/posts/96bc74a7e96a88615c1c77e052a06b57.png https://www.xieys.club/images/posts/96bc74a7e96a88615c1c77e052a06b57.png

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package main
import (
    "fmt"
    "github.com/360EntSecGroup-Skylar/excelize/v2"
)
func main() {
    categories := map[string]string{"A1": "Apple", "B1": "Orange", "C1": "Pear"}
    values := map[string]int{"A2": 2, "B2": 3, "C2": 3}
    f := excelize.N