@
1. 设置单元格格式
1.1 值类型
| func (f *File) SetCellValue(sheet string, axis string, value interface{}) error |
说明:
- axis:单元格位置
- value:向单元格中写入的值
| package main |
| |
| import ( |
| "fmt" |
| "github.com/xuri/excelize/v2" |
| "time" |
| ) |
| |
| func main() { |
| f := excelize.NewFile() |
| |
| t, _ := time.ParseInLocation("20060102150405", "20100424082959", time.Local) |
| |
| |
| f.SetCellValue("Sheet1","B1","LiuBei") |
| |
| f.SetCellValue("Sheet1","B2",t) |
| |
| f.SetCellValue("Sheet1","B3",11) |
| |
| f.SetCellValue("Sheet1","B4",3.1415926) |
| |
| if err := f.SaveAs("sanGuo.xlsx"); err != nil { |
| fmt.Println(err) |
| } |
| } |
1.2 设置布尔型值
| func (f *File) SetCellBool(sheet, axis string, value bool) error |
| package main |
| |
| import ( |
| "fmt" |
| "github.com/xuri/excelize/v2" |
| ) |
| |
| func main() { |
| f := excelize.NewFile() |
| |
| f.SetCellBool("Sheet1","B1",true) |
| f.SetCellBool("Sheet1","B2",false) |
| |
| if err := f.SaveAs("sanGuo.xlsx"); err != nil { |
| fmt.Println(err) |
| } |
| } |
- 结果

1.3 设置为默认字符型值
| func (f *File) SetCellDefault(sheet string, axis string, value string) error |
| f.SetCellDefault("Sheet1","B1","liuBbei") |
1.4 设置字符型值
据说和“默认字符型的区别是”它会进行特殊字符过滤,并且字符串的累计长度应不超过 32767,但是并没有试验出差别
| func (f *File) SetCellStr(sheet, axis, value string) error |
| f.SetCellStr("Sheet1","B1","liuBbei") |
1.5 设置实数格式
| func (f *File) SetCellInt(sheet, axis string, value int) error |
| f.SetCellInt("Sheet1","B1",1) |
2. 超链接
2.1 设置超链接
| func (f *File) SetCellHyperLink(sheet, axis, link, linkType string, opts ...HyperlinkOpts) error |
| type HyperlinkOpts struct { |
| Display *string |
| Tooltip *string |
| } |
| f.SetCellHyperLink("Sheet1", "B2", url, "External", excelize.HyperlinkOpts{ |
| Display: &url, |
| Tooltip: &tooltip, |
| }) |
| package main |
| |
| import ( |
| "github.com/xuri/excelize/v2" |
| ) |
| |
| func main() { |
| f := excelize.NewFile() |
| |
| tooltip := "My blog address" |
| url := "https://blog.csdn.net/xingzuo_1840" |
| |
| |
| f.SetCellHyperLink("Sheet1", "B2",url, "External", excelize.HyperlinkOpts{ |
| Display: &url, |
| Tooltip: &tooltip, |
| }) |
| |
| |
| style,_ := f.NewStyle(&excelize.Style{ |
| Font: &excelize.Font{Color: "#1265BE", Underline: "single"}, |
| }) |
| f.SetCellStyle("Sheet1", "B2", "A3", style) |
| |
| |
| f.SetCellValue("Sheet1", "B2", "LiuBei") |
| |
| f.SaveAs("sanGuo.xlsx") |
| } |
结果显示

2.2 获取超链接
| func (f *File) GetCellHyperLink(sheet, axis string) (bool, string, error) |
3.富文本
3.1 设置富文本格式
| func (f *File) SetCellRichText(sheet, cell string, runs []RichTextRun) error |
| type RichTextRun struct { |
| Font *Font |
| Text string |
| } |
| package main |
| |
| import ( |
| "github.com/xuri/excelize/v2" |
| ) |
| |
| func main() { |
| f := excelize.NewFile() |
| |
| |
| runs := []excelize.RichTextRun{ |
| { |
| Font: &excelize.Font{ |
| Size: 20, |
| Color: "#0000FF", |
| }, |
| Text: "LiuBei", |
| }, |
| { |
| Font: &excelize.Font{ |
| Size: 12, |
| Color: "#FF0000", |
| }, |
| Text: "\r\nGuanYu", |
| }, |
| { |
| Font: &excelize.Font{ |
| Size: 12, |
| Color: "#000000", |
| }, |
| Text: "\r\nZhangFei", |
| }, |
| } |
| f.SetCellRichText("Sheet1","B2",runs) |
| |
| |
| |
| styleId,_ := f.NewStyle(&excelize.Style{ |
| Alignment: &excelize.Alignment{ |
| WrapText: true, |
| }, |
| }) |
| f.SetCellStyle("Sheet1","B2","B2",styleId) |
| |
| |
| |
| |
| f.SaveAs("sanGuo.xlsx") |
| } |
结果显示

3.2 获取富文本格式
| package main |
| |
| import ( |
| "fmt" |
| "github.com/xuri/excelize/v2" |
| ) |
| |
| func main() { |
| |
| f, err := excelize.OpenFile("sanGuo.xlsx") |
| if err != nil { |
| fmt.Println(err) |
| return |
| } |
| defer func() { |
| if err = f.Close(); err != nil { |
| fmt.Println(err) |
| } |
| }() |
| |
| rune,_ := f.GetCellRichText("Sheet1","B2") |
| fmt.Printf("%+v",rune) |
| } |
| [{Font:0xc00034d920 Text:LiuBei} |
| {Font:0xc00034d980 Text:GuanYu} |
| {Font:0xc00034d9e0 Text:ZhangFei}] |
4. 获取单元格信息
4.1 获取单元格的值
| func (f *File) GetCellValue(sheet, axis string, opts ...Options) (string, error) |
| cell, err := f.GetCellValue("Sheet1", "B2") |
4.2 按列获取单元格数据
| func (f *File) GetCols(sheet string, opts ...Options) ([][]string, error) |
- 完整示例
- 创建sanGuo.xlsx表如下:

- 代码
| package main |
| |
| import ( |
| "fmt" |
| "github.com/xuri/excelize/v2" |
| ) |
| |
| func main() { |
| f, err := excelize.OpenFile("sanGuo.xlsx") |
| if err != nil { |
| fmt.Println(err) |
| return |
| } |
| defer func() { |
| if err = f.Close(); err != nil { |
| fmt.Println(err) |
| } |
| }() |
| cols,_ := f.GetCols("Sheet1") |
| fmt.Printf("GetCols函数获取到数据:\n%+v\n",cols) |
| } |
结果显示
| GetCols函数获取到数据: |
| [[序号 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15] [IP 10.10.181.128 10.10.181.129 10.10.181.130 10.10.181.131 10.10.181.132 10.10.181.133 10.10.181.134 10.10.181.135 10.10.181.136 10.10.181.137 10.10.181.138 10.10.181.139 10.10.181.140 10.10.181.141 10.10.181.142] [责任人 关羽 关羽 关羽 关羽 诸葛亮 诸葛亮 诸葛亮 诸葛亮 诸葛亮 张飞 张飞 张飞 赵云 赵云 赵云]] |
4.3 按行获取全部单元格的值
| func (f *File) GetRows(sheet string, opts ...Options) ([][]string, error) |
| rows,_ := f.GetRows("Sheet1") |
| package main |
| |
| import ( |
| "fmt" |
| "github.com/xuri/excelize/v2" |
| ) |
| |
| func main() { |
| f, err := excelize.OpenFile("sanGuo.xlsx") |
| if err != nil { |
| fmt.Println(err) |
| return |
| } |
| defer func() { |
| if err = f.Close(); err != nil { |
| fmt.Println(err) |
| } |
| }() |
| rows,_ := f.GetRows("Sheet1") |
| fmt.Printf("GetRows函数获取到数据:\n%+v\n",rows) |
| } |
| GetRows函数获取到数据: |
| [[序号 IP 责任人] [1 10.10.181.128 关羽] [2 10.10.181.129 关羽] [3 10.10.181.130 关羽] [4 10.10.181.131 关羽] [5 10.10.181.132 诸葛亮] [6 10.10.181.133 诸葛亮] [7 10.10.181.134 诸葛亮] [8 10.10.181.135 诸葛亮] [9 10.10.181.136 诸葛亮] [10 10.10.181.137 张飞] [11 10.10.181.138 张飞] [12 10.10.181.139 张飞] [13 10.10.181.140 赵云] [14 10.10.181.141 赵云] [15 10.10.181.142 赵云]] |
4.4 获取单元格类型
| func (f *File) GetCellType(sheet, axis string) (CellType, error) |
4.5 获取样式索引
| func (f *File) GetCellStyle(sheet, axis string) (int, error) |
5. 合并单元格
5.1 合并单元格
| func (f *File) MergeCell(sheet, hCell, vCell string) error |
| err := f.UnmergeCell("Sheet1", "B2", "D4") |
5.2 取消合并单元格
| func (f *File) UnmergeCell(sheet string, hCell, vCell string) error |
| err := f.UnmergeCell("Sheet1", "B2", "D4") |
5.3 获取合并单元格位置
获取整个表的所有合并单元格位置
| func (f *File) GetMergeCells(sheet string) ([]MergeCell, error) |
说明:MergeCell 类型为 []string
,标记了一个合并单元格的开始和结束位置。
| mergeCells,_ := f.GetMergeCells("Sheet1") |
| package main |
| |
| import ( |
| "fmt" |
| "github.com/xuri/excelize/v2" |
| ) |
| |
| func main() { |
| f := excelize.NewFile() |
| f.MergeCell("Sheet1","B2","D4") |
| f.MergeCell("Sheet1","B9","E10") |
| mergeCells,_ := f.GetMergeCells("Sheet1") |
| fmt.Printf("%+v",mergeCells) |
| } |
5.4 获取合并单元格数值
| func (m *MergeCell) GetCellValue() string |
注意,该方法是*MergeCell
,不是*File
的
| package main |
| |
| import ( |
| "fmt" |
| "github.com/xuri/excelize/v2" |
| ) |
| |
| func main() { |
| f := excelize.NewFile() |
| f.SetCellValue("sheet1","B2","LiuBei") |
| f.SetCellValue("sheet1","B9","GuanYu") |
| f.MergeCell("Sheet1","B2","D4") |
| f.MergeCell("Sheet1","B9","E10") |
| mergeCells,_ := f.GetMergeCells("Sheet1") |
| for _,mergeCell := range mergeCells { |
| mergeCellValue := mergeCell.GetCellValue() |
| fmt.Println(mergeCellValue) |
| } |
| } |
5.5 获取合并单元格区域左上角单元格坐标
5.6 获取合并单元格区域左上角单元格坐标
| func (m *MergeCell) GetEndAxis() string |

【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了