1. 新建工作表
| func (f *File) NewSheet(sheet string) int |
见 1.1
2. 删除工作表
| func (f *File) DeleteSheet(sheet string) |
3. 复制工作表
将第"x"张表的内容,复制到已有的
第“y”张表。
| func (f *File) CopySheet(from, to int) error |
| package main |
| |
| import ( |
| "fmt" |
| |
| "github.com/xuri/excelize/v2" |
| ) |
| |
| func main() { |
| f := excelize.NewFile() |
| |
| indexXiShu := f.NewSheet("西蜀") |
| f.NewSheet("东吴") |
| f.NewSheet("曹魏") |
| |
| f.SetCellValue("西蜀", "B2", "刘备") |
| f.SetCellValue("西蜀", "B3", "关羽") |
| f.SetCellValue("西蜀", "B4", "张飞") |
| f.SetCellValue("东吴", "B2", "孙权") |
| f.SetCellValue("曹魏", "B2", "曹操") |
| |
| f.CopySheet(1,2) |
| |
| f.SetActiveSheet(indexXiShu) |
| |
| if err := f.SaveAs("sanGuo.xlsx"); err != nil { |
| fmt.Println(err) |
| } |
| } |
说明:
- 创建的代码创建了四张表: sheet1(创建工作簿时默认创建)、"西蜀"、“东吴”、“曹魏”
- 从第1张表复制(0号开始计),即复制“西蜀”表
- 复制到第2张表,即复制到“东吴表”
- 如果目标表的值超出了已有表,则不会新创建(什么也不会发生)
- 查看结果
如下可见,“东吴”表复制了“西蜀”表的内容。且原有内容被覆盖。

4. 工作表分组
4.1 创建组
| func (f *File) GroupSheets(sheets []string) error |
| err = f.GroupSheets([]string{"西蜀","东吴"}) |
4.2 删除分组
| func (f *File) UngroupSheets() error |
5. 默认工作表
5.1 设置默认工作表
| func (f *File) SetActiveSheet(index int) |
见 1.1
5.2 获取默认工作表
| func (f *File) GetActiveSheetIndex() int |
6. 工作表可见性
6.1 设置工作表可见性
| func (f *File) SetSheetVisible(sheet string, visible bool) error |
visible
的值 true
表示可见,flase
表示不可见。
隐藏东吴表
| package main |
| |
| import ( |
| "fmt" |
| |
| "github.com/xuri/excelize/v2" |
| ) |
| |
| func main() { |
| f := excelize.NewFile() |
| |
| indexXiShu := f.NewSheet("西蜀") |
| f.NewSheet("东吴") |
| f.NewSheet("曹魏") |
| |
| f.SetCellValue("西蜀", "B2", "刘备") |
| f.SetCellValue("西蜀", "B3", "关羽") |
| f.SetCellValue("西蜀", "B4", "张飞") |
| f.SetCellValue("东吴", "B2", "孙权") |
| f.SetCellValue("曹魏", "B2", "曹操") |
| f.SetSheetVisible("东吴",false) |
| |
| |
| f.SetActiveSheet(indexXiShu) |
| |
| if err := f.SaveAs("sanGuo.xlsx"); err != nil { |
| fmt.Println(err) |
| } |
| } |
- 结果显示

如图可见,东吴表被隐藏。
6.2 获取工作表可见性
| func (f *File) GetSheetVisible(sheet string) bool |
上例中添加如下代码:
| statusDongWu := f.GetSheetVisible("东吴") |
| fmt.Printf("东吴表的状态是:%t ",statusDongWu) |
结果显示
7. 表格整体属性
| func (f *File) SetSheetFormatPr(sheet string, opts ...SheetFormatPrOptions) error |
可选格式参数 |
作用 |
数据类型 |
BaseColWidth |
基础宽度 |
uint8 |
DefaultColWidth |
默认列宽 |
float64 |
DefaultRowHeight |
默认行高 |
float64 |
CustomHeight |
自定义高度 |
bool |
ZeroHeight |
表格高仅为有内容的部分 |
bool |
ThickTop |
顶部厚 |
bool |
ThickBottom |
底部厚 |
bool |
7.1 自定义列宽
| f.SetSheetFormatPr("西蜀",excelize.DefaultColWidth(30.0)) |
或者
| f.SetSheetFormatPr("西蜀",excelize.BaseColWidth(30)) |
| package main |
| |
| import ( |
| "fmt" |
| |
| "github.com/xuri/excelize/v2" |
| ) |
| |
| func main() { |
| f := excelize.NewFile() |
| |
| indexXiShu := f.NewSheet("西蜀") |
| |
| f.SetCellValue("西蜀", "A1", "刘备") |
| f.SetCellValue("西蜀", "A2", "关羽") |
| f.SetCellValue("西蜀", "A3", "张飞") |
| f.SetSheetFormatPr("西蜀",excelize.DefaultColWidth(30.0)) |
| |
| |
| f.SetActiveSheet(indexXiShu) |
| |
| if err := f.SaveAs("sanGuo.xlsx"); err != nil { |
| fmt.Println(err) |
| } |
| } |
- 结果显示

7.2 自定义行高
处设置默认列宽之外还需要设置 自定义列宽为true
| f.SetSheetFormatPr("西蜀",excelize.DefaultRowHeight(30.0),excelize.CustomHeight(true)) |
| package main |
| |
| import ( |
| "fmt" |
| |
| "github.com/xuri/excelize/v2" |
| ) |
| |
| func main() { |
| f := excelize.NewFile() |
| |
| indexXiShu := f.NewSheet("西蜀") |
| |
| |
| f.SetCellValue("西蜀", "A1", "刘备") |
| f.SetCellValue("西蜀", "A2", "关羽") |
| f.SetCellValue("西蜀", "A3", "张飞") |
| f.SetSheetFormatPr("西蜀",excelize.DefaultRowHeight(30.0),excelize.CustomHeight(true)) |
| |
| |
| f.SetActiveSheet(indexXiShu) |
| |
| if err := f.SaveAs("sanGuo.xlsx"); err != nil { |
| fmt.Println(err) |
| } |
| } |
- 结果显示

7.3 行高为内容部分
| f.SetSheetFormatPr("西蜀",excelize.ZeroHeight(true)) |
| package main |
| |
| import ( |
| "fmt" |
| |
| "github.com/xuri/excelize/v2" |
| ) |
| |
| func main() { |
| f := excelize.NewFile() |
| |
| indexXiShu := f.NewSheet("西蜀") |
| |
| |
| f.SetCellValue("西蜀", "A1", "刘备") |
| f.SetCellValue("西蜀", "A2", "关羽") |
| f.SetCellValue("西蜀", "A3", "张飞") |
| f.SetSheetFormatPr("西蜀",excelize.ZeroHeight(true)) |
| |
| |
| f.SetActiveSheet(indexXiShu) |
| |
| if err := f.SaveAs("sanGuo.xlsx"); err != nil { |
| fmt.Println(err) |
| } |
| } |
- 结果显示

7.4 获取表格属性
| func (f *File) GetSheetFormatPr(sheet string, opts ...SheetFormatPrOptionsPtr) error |
说明:该示例摘自官方文档。
| package main |
| |
| import ( |
| "fmt" |
| "github.com/xuri/excelize/v2" |
| ) |
| |
| func main() { |
| f := excelize.NewFile() |
| const sheet = "Sheet1" |
| |
| var ( |
| baseColWidth excelize.BaseColWidth |
| defaultColWidth excelize.DefaultColWidth |
| defaultRowHeight excelize.DefaultRowHeight |
| customHeight excelize.CustomHeight |
| zeroHeight excelize.ZeroHeight |
| thickTop excelize.ThickTop |
| thickBottom excelize.ThickBottom |
| ) |
| |
| if err := f.GetSheetFormatPr(sheet, |
| &baseColWidth, |
| &defaultColWidth, |
| &defaultRowHeight, |
| &customHeight, |
| &zeroHeight, |
| &thickTop, |
| &thickBottom, |
| ); err != nil { |
| fmt.Println(err) |
| } |
| fmt.Println("Defaults:") |
| fmt.Println("- baseColWidth:", baseColWidth) |
| fmt.Println("- defaultColWidth:", defaultColWidth) |
| fmt.Println("- defaultRowHeight:", defaultRowHeight) |
| fmt.Println("- customHeight:", customHeight) |
| fmt.Println("- zeroHeight:", zeroHeight) |
| fmt.Println("- thickTop:", thickTop) |
| fmt.Println("- thickBottom:", thickBottom) |
| } |
| Defaults: |
| - baseColWidth: 0 |
| - defaultColWidth: 0 |
| - defaultRowHeight: 15 |
| - customHeight: false |
| - zeroHeight: false |
| - thickTop: false |
| - thickBottom: false |
8. 表名
8.1 根据索引获取工作表名
| func (f *File) GetSheetName(index int) (name string) |
| package main |
| |
| import ( |
| "fmt" |
| "github.com/xuri/excelize/v2" |
| ) |
| |
| func main() { |
| f := excelize.NewFile() |
| sheetName := f.GetSheetName(0) |
| fmt.Println(sheetName) |
| } |
结果显示
8.2 根据表名获取工作表索引
| func (f *File) GetSheetIndex(sheet string) int |
表不存在返回-1
8.3 获取工作表列表
| func (f *File) GetSheetList() []string |
8.4 修改工作表名
| func (f *File) SetSheetName(oldName, newName string) |
| package main |
| |
| import ( |
| "fmt" |
| "github.com/xuri/excelize/v2" |
| ) |
| |
| func main() { |
| f := excelize.NewFile() |
| fmt.Println("修改前0号表表名:",f.GetSheetName(0)) |
| f.SetSheetName("Sheet1","xiShu") |
| fmt.Println("修改后0号表表名:",f.GetSheetName(0)) |
| |
| } |
结果显示
| 修改前0号表表名: Sheet1 |
| 修改后0号表表名: xiShu |
9. 表查询
| func (f *File) SearchSheet(sheet string, value string, reg ...bool) ([]string, error) |
说明:
- sheet :表名
- value:要查找的字串
- reg:是否匹配正则
| package main |
| |
| import ( |
| "fmt" |
| |
| "github.com/xuri/excelize/v2" |
| ) |
| |
| func main() { |
| f := excelize.NewFile() |
| |
| |
| f.SetCellValue("Sheet1", "A1", "刘备") |
| f.SetCellValue("Sheet1", "A2", "诸葛亮") |
| f.SetCellValue("Sheet1", "A3", "关羽") |
| f.SetCellValue("Sheet1", "A4", "张飞") |
| f.SetCellValue("Sheet1", "A5", "赵云") |
| f.SetCellValue("Sheet1", "A6", "马超") |
| f.SetCellValue("Sheet1", "A7", "黄忠") |
| f.SetCellValue("Sheet1", "A8", "关兴") |
| f.SetCellValue("Sheet1", "A9", "关平") |
| |
| result1,_ := f.SearchSheet("Sheet1","关羽") |
| |
| result2,_ := f.SearchSheet("Sheet1","^关.*",true) |
| fmt.Printf("精确搜索:%+v\n正则匹配: %+v",result1,result2) |
| } |
| 精确搜索:[A3] |
| 正则匹配: [A3 A8 A9] |

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