03-行和列操作

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)
}
  • 结果显示
第二行的级别是:2

6.3 设置列的分级显示

  • 语法
func (f *File) SetColOutlineLevel(sheet, col string, level uint8) error
  • 示例
    参考行的分级显示

6.4 获取列的分级显示级别

  • 语法
func (f *File) GetRowOutlineLevel(sheet string, row int) (uint8, error)

posted on   运维开发玄德公  阅读(28)  评论(0编辑  收藏  举报  

相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
< 2025年3月 >
23 24 25 26 27 28 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 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示