工作表

    1. xlsx.SetColVisible("Sheet1", "D", false)

    设置列宽度

    1. func (f *File) SetColWidth(sheet, startcol, endcol string, width float64)

    根据给定的工作表名称(大小写敏感)、列范围和宽度值设置单个或多个列的宽度。例如设置名为 Sheet1 工作表上 AH 列的宽度为 20

    1. xlsx := excelize.NewFile()
    2. xlsx.SetColWidth("Sheet1", "A", "H", 20)
    3. err := xlsx.Save()
    4. if err != nil {
    5. fmt.Println(err)
    6. }

    设置行高度

    1. func (f *File) SetRowHeight(sheet string, row int, height float64)

    根据给定的工作表名称(大小写敏感)、行号和高度值设置单行高度。例如设置名为 Sheet1 工作表首行的高度为 50

    1. xlsx.SetRowHeight("Sheet1", 1, 50)

    设置行可见性

    1. func (f *File) SetRowVisible(sheet string, rowIndex int, visible bool)

    根据给定的工作表名称(大小写敏感)和行号设置行可见性。例如隐藏名为 Sheet1 工作表上第二行:

    1. xlsx.SetRowVisible("Sheet1", 2, false)

    获取工作表名

    1. func (f *File) GetSheetName(index int) string

    根据给定的工作表索引获取工作表名称,如果工作表不存在将返回空字符。

    获取列可见性

      根据给定的工作表名称(大小写敏感)和列名获取工作表中指定列的可见性,可见返回值为 true,否则为 false。例如,获取名为 Sheet1 的工作表上 D 列的可见性:

      1. xlsx.GetColVisible("Sheet1", "D")

      获取列宽度

      1. func (f *File) GetColWidth(sheet, column string) float64

      根据给定的工作表和列名获取工作表中指定列的宽度。

      1. func (f *File) GetRowHeight(sheet string, row int) float64

      根据给定的工作表名称(大小写敏感)和行号获取工作表中指定行的高度。例如,获取名为 Sheet1 的工作表首行的高度:

      1. xlsx.GetRowHeight("Sheet1", 1)

      获取行可见性

      1. func (f *File) GetRowVisible(sheet string, rowIndex int) bool

      根据给定的工作表名称(大小写敏感)和行号获取工作表中指定行的可见性。例如,获取名为 Sheet1 的工作表第 2 行的可见性:

      获取工作表索引

      1. func (f *File) GetSheetIndex(name string) int

      根据给定的工作表名称(大小写敏感)获取该工作表的索引,如果工作表不存在将返回 0。获取到的索引可以在设置工作簿默认工作表时,作为调用 SetActiveSheet() 函数的参数使用。

      获取工作表列表

      1. func (f *File) GetSheetMap() map[int]string

      获取工作簿中以名称和索引构成的全部工作表的列表。

      1. xlsx, err := excelize.OpenFile("./Book1.xlsx")
      2. if err != nil {
      3. return
      4. }
      5. fmt.Println(index, name)
      6. }

      获取工作表属性

      1. func (f *File) GetSheetPrOptions(name string, opts ...SheetPrOptionPtr) error

      例如:

      1. xl := excelize.NewFile()
      2. const sheet = "Sheet1"
      3. var (
      4. codeName excelize.CodeName
      5. enableFormatConditionsCalculation excelize.EnableFormatConditionsCalculation
      6. published excelize.Published
      7. fitToPage excelize.FitToPage
      8. autoPageBreaks excelize.AutoPageBreaks
      9. outlineSummaryBelow excelize.OutlineSummaryBelow
      10. )
      11. if err := xl.GetSheetPrOptions(sheet,
      12. &codeName,
      13. &enableFormatConditionsCalculation,
      14. &published,
      15. &fitToPage,
      16. &autoPageBreaks,
      17. &outlineSummaryBelow,
      18. ); err != nil {
      19. panic(err)
      20. }
      21. fmt.Println("Defaults:")
      22. fmt.Printf("- codeName: %q\n", codeName)
      23. fmt.Println("- published:", published)
      24. fmt.Println("- fitToPage:", fitToPage)
      25. fmt.Println("- autoPageBreaks:", autoPageBreaks)
      26. fmt.Println("- outlineSummaryBelow:", outlineSummaryBelow)

      输出:

      1. Defaults:
      2. - codeName: ""
      3. - enableFormatConditionsCalculation: true
      4. - published: true
      5. - fitToPage: false
      6. - autoPageBreaks: false
      7. - outlineSummaryBelow: true

      插入列

      1. func (f *File) InsertCol(sheet, column string)

      根据给定的工作表名称(大小写敏感)和列名称,在指定列前插入空白列。例如,在名为 Sheet1 的工作表的 列前插入空白列:

      1. xlsx.InsertCol("Sheet1", "C")

      插入行

      1. func (f *File) InsertRow(sheet string, row int)

      根据给定的工作表名称(大小写敏感)和行索引,在指定行前插入空白行。例如,在名为 Sheet1 的工作表的第 3 行前插入空白行:

      1. xlsx.InsertRow("Sheet1", 2)
      1. func (f *File) DuplicateRow(sheet string, row int)

      根据给定的工作表名称(大小写敏感)和行索引,在指定行后复制该行。例如,将名为 Sheet1 的工作表的第 3 行后复制到第 4 行:

      1. xlsx.DuplicateRow("Sheet1", 2)

      创建行的分级显示

      1. func (f *File) SetRowOutlineLevel(sheet string, rowIndex int, level uint8)

      根据给定的工作表名称(大小写敏感)、行索引和分级参数创建组。例如,在名为 Sheet1 的工作表的第 2 行创建 1 级分组。

      1. xlsx.SetRowOutlineLevel("Sheet1", 2, 1)

      创建列的分级显示

      根据给定的工作表名称(大小写敏感)、列名称和分级参数创建组。例如,在名为 Sheet1 的工作表的 D 列创建 2 级分组。

      "创建列的分级显示"

      1. xlsx.SetColOutlineLevel("Sheet1", "D", 2)

      获取行的分级显示

      1. func (f *File) GetRowOutlineLevel(sheet string, rowIndex int) uint8

      根据给定的工作表名称(大小写敏感)和行索引获取分组级别。例如,获取名为 Sheet1 的工作表第 2 行的分组级别。

      1. xlsx.GetRowOutlineLevel("Sheet1", 2)

      获取列的分级显示

      1. func (f *File) GetColOutlineLevel(sheet, column string) uint8
      1. xlsx.GetColOutlineLevel("Sheet1", "D")

      行迭代器

      1. func (f *File) Rows(sheet string) (*Rows, error)

      根据给定的工作表名称(大小写敏感)获取该工作表的行迭代器。使用行迭代器遍历单元格:

      1. rows, err := xlsx.Rows("Sheet1")
      2. for rows.Next() {
      3. for _, colCell := range rows.Columns() {
      4. fmt.Print(colCell, "\t")
      5. }
      6. fmt.Println()
      7. }
      1. func (rows *Rows) Columns() []string

      返回当前行所有列的值。

      1. func (rows *Rows) Next() bool

      如果下一行有值存在将返回 true

      1. func (rows *Rows) Error() error

      当查找下一行出现错误时将返回 error

      在工作表中搜索

      1. func (f *File) SearchSheet(sheet, value string, reg ...bool) []string

      根据给定的工作表名称(大小写敏感),单元格值或正则表达式来获取坐标。此函数仅支持字符串和数字的完全匹配,不支持公式计算后的结果、格式化数字和条件搜索。如果搜索结果是合并的单元格,将返回合并区域左上角的坐标。

      例如,在名为 Sheet1 的工作表中搜索值 100 的坐标:

      1. xlsx.SearchSheet("Sheet1", "100")

      例如,在名为 Sheet1 的工作表中搜索 0-9 范围内数值的坐标:

      1. xlsx.SearchSheet("Sheet1", "[0-9]", true)
      1. func (f *File) ProtectSheet(sheet string, settings *FormatSheetProtection)

      防止其他用户意外或有意更改、移动或删除工作表中的数据。例如,为名为 的工作表设置密码保护,但是允许选择锁定的单元格、选择未锁定的单元格、编辑方案:

      取消保护工作表

        根据给定的工作表名称(大小写敏感)取消保护该工作表。