• Go语言excelize包-03-行和列操作(可见性、指定列宽、指定行高、列删除、行删除、插入列、插入行、分级显示)


    1 可见性

    1.1 设置列可见性

    • 语法
    func (f *File) SetColVisible(sheet string, columns string, visible bool) error
    
    • 1
    • 完整示例
    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)
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    结果显示

    如下可见,B列被隐藏

    在这里插入图片描述

    1.2 获取列可见性

    • 语法
    func (f *File) GetColVisible(sheet string, col string) (bool, error)
    
    • 1
    • 语法示例
    status,_ := f.GetColVisible("sheet1","B")
    
    • 1
    • 完整示例
    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
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    1.3 设置行可见性

    • 语法
    func (f *File) SetRowVisible(sheet string, row int, visible bool) error
    
    • 1
    • 语法示例
    err := f.SetRowVisible("Sheet1", 2, false)
    
    • 1

    1.4 获取行可见性

    • 语法
    func (f *File) GetRowVisible(sheet string, row int) (bool, error)
    
    • 1
    • 语法示例
    err := f.GetRowVisible("Sheet1", 2)
    
    • 1

    2. 指定列列宽

    2.1 设置指定列列宽

    • 语法示例
    func (f *File) SetColWidth(sheet string, startCol string, endCol string, width float64) error
    
    • 1
    • 完整示例
    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)
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    2.2 获取指定列列宽

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

    3. 指定行行高

    3.1 设置指定行行高

    • 语法
    func (f *File) SetRowHeight(sheet string, row int, height float64) error
    
    • 1
    • 语法示例
    f.SetRowHeight("sheet1",5,30)
    
    • 1
    • 完整示例
    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)
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    3.2 或者指定行行高

    • 语法
    func (f *File) GetRowHeight(sheet string, row int) (float64, error)
    
    • 1
    • 语法示例
    height, err := f.GetRowHeight("Sheet1", 1)
    
    • 1

    4. 列增删

    4.1 插入空列

    • 语法
    func (f *File) InsertCol(sheet string, col string) error
    
    • 1
    • 完整示例
    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)
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 结果显示

    如下可见,插入了B列。

    在这里插入图片描述

    4.2 删除列

    • 语法
    func (f *File) RemoveCol(sheet string, col string) error
    
    • 1

    5. 行增删

    5.1 插入空白行

    • 语法
    func (f *File) InsertRow(sheet string, row int) error
    
    • 1
    • 语法示例
    err := f.InsertRow("Sheet1", 3)
    
    • 1

    5.2 复制行到指定位置

    • 语法
    func (f *File) DuplicateRowTo(sheet string, row, row2 int) error
    
    • 1
    • 语法示例
    f.DuplicateRowTo("sheet1",2,6)
    
    • 1
    • 完整示例
    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)
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 显示结果

    在这里插入图片描述

    5.3 追加复制行到下一行

    • 语法

    将第n行复制,并插入到该行之后。

    func (f *File) DuplicateRow(sheet string, row int) error
    
    • 1
    • 完整示例
    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)
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    结果显示

    如下可见,第二行被复制并插入到了第二行之后

    在这里插入图片描述

    5.4 删除行

    • 语法
    func (f *File) RemoveRow(sheet string, row int) error
    
    • 1

    5.5 按行赋值

    • 语法
    func (f *File) SetSheetRow(sheet string, axis string, slice interface{}) error
    
    • 1
    • 完整代码
    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)
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 结果
      在这里插入图片描述

    6. 分级显示

    6.1 设置行分级显示

    • 语法
    func (f *File) SetRowOutlineLevel(sheet string, row int, level uint8) error
    
    • 1
    • 语法示例
     f.SetRowOutlineLevel("sheet1",2,1)
    
    • 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)
    	}
    }
    
    • 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
    • 结果显示
      在这里插入图片描述

    6.2 获取行的分级显示级别

    • 语法
    func (f *File) GetRowOutlineLevel(sheet string, row int) (uint8, error)
    
    • 1
    • 语法示例
    level,_ := f.GetRowOutlineLevel("Sheet1", 3)
    
    • 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)
    	level,_ := f.GetRowOutlineLevel("Sheet1", 3)
    	fmt.Printf("第三行的级别是:%d\n",level)
    }
    
    • 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
    • 结果显示
    第二行的级别是:2
    
    • 1

    6.3 设置列的分级显示

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

    6.4 获取列的分级显示级别

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

    在这里插入图片描述

  • 相关阅读:
    LeetCode 2296.设计一个文本编辑器
    机器学习-2-机器学习领域六种基础算法的历史溯源
    多重背包理论基础
    适配器模式
    html调用手机打电话、发短信网页源码/热门挪车自动拨打电话、发送短信html源码
    1200万像素通过算法无失真扩展到1.92亿像素——加权概率模型收缩模型图像像素扩展算法
    使用设计模式来增强你的 SpringBoot 开发
    分享76个PHP源码总有一个适合你
    js设计模式
    Element Plus中的注意事项
  • 原文地址:https://blog.csdn.net/xingzuo_1840/article/details/126689215