1 可见性
1.1 设置列可见性
| func (f *File) SetColVisible(sheet string, columns string, visible bool) error |
| package main |
| |
| import ( |
| "fmt" |
| "github.com/xuri/excelize/v2" |
| ) |
| |
| func main() { |
| f := excelize.NewFile() |
| |
| |
| f.SetCellValue("sheet1", "A1", "LiuBei") |
| f.SetCellValue("sheet1", "B1", "GuanYu") |
| f.SetCellValue("sheet1", "C1", "ZhangFei") |
| f.SetColVisible("sheet1","B",false) |
| |
| if err := f.SaveAs("sanGuo.xlsx"); err != nil { |
| fmt.Println(err) |
| } |
| } |
结果显示
如下可见,B列被隐藏

1.2 获取列可见性
| func (f *File) GetColVisible(sheet string, col string) (bool, error) |
| status,_ := f.GetColVisible("sheet1","B") |
| package main |
| |
| import ( |
| "fmt" |
| "github.com/xuri/excelize/v2" |
| ) |
| |
| func main() { |
| f := excelize.NewFile() |
| |
| |
| f.SetCellValue("sheet1", "A1", "LiuBei") |
| f.SetCellValue("sheet1", "B1", "GuanYu") |
| f.SetCellValue("sheet1", "C1", "ZhangFei") |
| f.SetColVisible("sheet1","B",false) |
| status,_ := f.GetColVisible("sheet1","B") |
| fmt.Println(status) |
| |
| } |
1.3 设置行可见性
| func (f *File) SetRowVisible(sheet string, row int, visible bool) error |
| err := f.SetRowVisible("Sheet1", 2, false) |
1.4 获取行可见性
| func (f *File) GetRowVisible(sheet string, row int) (bool, error) |
| err := f.GetRowVisible("Sheet1", 2) |
2. 指定列列宽
2.1 设置指定列列宽
| func (f *File) SetColWidth(sheet string, startCol string, endCol string, width float64) error |
| package main |
| |
| import ( |
| "fmt" |
| "github.com/xuri/excelize/v2" |
| ) |
| |
| func main() { |
| f := excelize.NewFile() |
| |
| |
| f.SetCellValue("sheet1", "A1", "LiuBei") |
| f.SetCellValue("sheet1", "B1", "GuanYu") |
| f.SetCellValue("sheet1", "C1", "ZhangFei") |
| f.SetColWidth("sheet1","B","C",30) |
| |
| if err := f.SaveAs("sanGuo.xlsx"); err != nil { |
| fmt.Println(err) |
| } |
| } |
2.2 获取指定列列宽
| func (f *File) GetColWidth(sheet, col string) (float64, error) |
3. 指定行行高
3.1 设置指定行行高
| func (f *File) SetRowHeight(sheet string, row int, height float64) error |
| f.SetRowHeight("sheet1",5,30) |
| package main |
| |
| import ( |
| "fmt" |
| "github.com/xuri/excelize/v2" |
| ) |
| |
| func main() { |
| f := excelize.NewFile() |
| |
| |
| f.SetCellValue("sheet1", "A1", "LiuBei") |
| f.SetCellValue("sheet1", "B1", "GuanYu") |
| f.SetCellValue("sheet1", "C1", "ZhangFei") |
| f.SetRowHeight("sheet1",5,30) |
| |
| if err := f.SaveAs("sanGuo.xlsx"); err != nil { |
| fmt.Println(err) |
| } |
| } |
3.2 或者指定行行高
| func (f *File) GetRowHeight(sheet string, row int) (float64, error) |
| height, err := f.GetRowHeight("Sheet1", 1) |
4. 列增删
4.1 插入空列
| func (f *File) InsertCol(sheet string, col string) error |
| package main |
| |
| import ( |
| "fmt" |
| |
| "github.com/xuri/excelize/v2" |
| ) |
| |
| func main() { |
| f := excelize.NewFile() |
| |
| f.SetCellValue("Sheet1", "A1", "liuBei") |
| f.SetCellValue("Sheet1", "B1", "guanYu") |
| f.SetCellValue("Sheet1", "C1", "zhangFei") |
| f.InsertCol("sheet1","B") |
| if err := f.SaveAs("sanGuo.xlsx"); err != nil { |
| fmt.Println(err) |
| } |
| } |
如下可见,插入了B列。

4.2 删除列
| func (f *File) RemoveCol(sheet string, col string) error |
5. 行增删
5.1 插入空白行
| func (f *File) InsertRow(sheet string, row int) error |
| err := f.InsertRow("Sheet1", 3) |
5.2 复制行到指定位置
| func (f *File) DuplicateRowTo(sheet string, row, row2 int) error |
| f.DuplicateRowTo("sheet1",2,6) |
| package main |
| |
| import ( |
| "fmt" |
| |
| "github.com/xuri/excelize/v2" |
| ) |
| |
| func main() { |
| f := excelize.NewFile() |
| |
| f.SetCellValue("Sheet1", "A1", "liuBei") |
| f.SetCellValue("Sheet1", "A2", "guanYu") |
| f.SetCellValue("Sheet1", "A3", "zhangFei") |
| f.DuplicateRowTo("sheet1",2,6) |
| if err := f.SaveAs("sanGuo.xlsx"); err != nil { |
| fmt.Println(err) |
| } |
| } |

5.3 追加复制行到下一行
将第n行复制,并插入到该行之后。
| func (f *File) DuplicateRow(sheet string, row int) error |
| package main |
| |
| import ( |
| "fmt" |
| |
| "github.com/xuri/excelize/v2" |
| ) |
| |
| func main() { |
| f := excelize.NewFile() |
| |
| f.SetCellValue("Sheet1", "A1", "liuBei") |
| f.SetCellValue("Sheet1", "A2", "guanYu") |
| f.SetCellValue("Sheet1", "A3", "zhangFei") |
| f.DuplicateRow("Sheet1",2) |
| if err := f.SaveAs("sanGuo.xlsx"); err != nil { |
| fmt.Println(err) |
| } |
| } |
结果显示
如下可见,第二行被复制并插入到了第二行之后

5.4 删除行
| func (f *File) RemoveRow(sheet string, row int) error |
5.5 按行赋值
| func (f *File) SetSheetRow(sheet string, axis string, slice interface{}) error |
| package main |
| |
| import ( |
| "fmt" |
| "github.com/xuri/excelize/v2" |
| ) |
| |
| func main() { |
| f := excelize.NewFile() |
| |
| |
| f.SetSheetRow("Sheet1","B1",&[]interface{}{"姓名","年龄","性别"}) |
| f.SetSheetRow("Sheet1","B2",&[]interface{}{"liuBei",25,"男"}) |
| |
| if err := f.SaveAs("sanGuo.xlsx"); err != nil { |
| fmt.Println(err) |
| } |
| } |
- 结果

6. 分级显示
6.1 设置行分级显示
| func (f *File) SetRowOutlineLevel(sheet string, row int, level uint8) error |
| f.SetRowOutlineLevel("sheet1",2,1) |
| package main |
| |
| import ( |
| "fmt" |
| |
| "github.com/xuri/excelize/v2" |
| ) |
| |
| func main() { |
| f := excelize.NewFile() |
| |
| f.SetCellValue("Sheet1", "A1", "name:") |
| f.SetCellValue("Sheet1", "A2", "- xiShu") |
| f.SetCellValue("Sheet1", "A3", "liuBei") |
| f.SetCellValue("Sheet1", "A4", "GuanYu") |
| f.SetCellValue("Sheet1", "A5", "zhangFei") |
| f.SetCellValue("Sheet1", "A6", "- dongWu:") |
| f.SetCellValue("Sheet1", "A7", "sunQuan") |
| f.SetRowOutlineLevel("sheet1",2,1) |
| f.SetRowOutlineLevel("sheet1",6,1) |
| f.SetRowOutlineLevel("sheet1",3,2) |
| f.SetRowOutlineLevel("sheet1",4,2) |
| f.SetRowOutlineLevel("sheet1",5,2) |
| f.SetRowOutlineLevel("sheet1",7,2) |
| if err := f.SaveAs("sanGuo.xlsx"); err != nil { |
| fmt.Println(err) |
| } |
| } |
- 结果显示

6.2 获取行的分级显示级别
| func (f *File) GetRowOutlineLevel(sheet string, row int) (uint8, error) |
| level,_ := f.GetRowOutlineLevel("Sheet1", 3) |
| package main |
| |
| import ( |
| "fmt" |
| |
| "github.com/xuri/excelize/v2" |
| ) |
| |
| func main() { |
| f := excelize.NewFile() |
| |
| f.SetCellValue("Sheet1", "A1", "name:") |
| f.SetCellValue("Sheet1", "A2", "- xiShu") |
| f.SetCellValue("Sheet1", "A3", "liuBei") |
| f.SetCellValue("Sheet1", "A4", "GuanYu") |
| f.SetCellValue("Sheet1", "A5", "zhangFei") |
| f.SetCellValue("Sheet1", "A6", "- dongWu:") |
| f.SetCellValue("Sheet1", "A7", "sunQuan") |
| f.SetRowOutlineLevel("sheet1",2,1) |
| f.SetRowOutlineLevel("sheet1",6,1) |
| f.SetRowOutlineLevel("sheet1",3,2) |
| f.SetRowOutlineLevel("sheet1",4,2) |
| f.SetRowOutlineLevel("sheet1",5,2) |
| f.SetRowOutlineLevel("sheet1",7,2) |
| level,_ := f.GetRowOutlineLevel("Sheet1", 3) |
| fmt.Printf("第三行的级别是:%d\n",level) |
| } |
6.3 设置列的分级显示
| func (f *File) SetColOutlineLevel(sheet, col string, level uint8) error |
6.4 获取列的分级显示级别
| func (f *File) GetRowOutlineLevel(sheet string, row int) (uint8, error) |

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