Golang操作Excel的模块Excelize – 设置单元格的值
-
新建excel文件
excelize.NewFile()
-
获取当前活动的sheet页索引
func (f *File) GetActiveSheetIndex() (index int)
-
指定单元格设置值
func (f *File) SetCellValue(sheet, axis string, value interface{}) error
-
指定单元格设置值,常规不转码
func (f *File) SetCellDefault(sheet, axis, value string) error
-
设置单元格的值 字符串
func (f *File) SetCellStr(sheet, axis, value string) error
-
设置单元格的值 布尔
func (f *File) SetCellBool(sheet, axis string, value bool) error
-
设置单元格的值 浮点型
第三个参数是值,必须是float64类型的,第四个是小数点的位数,第五个是32或者64,原始的数据是float32就是32,是float64就是64
func (f *File) SetCellFloat(sheet, axis string, value float64, prec, bitSize int) error
-
设置单元格的值 整数
func (f *File) SetCellInt(sheet, axis string, value int) error
-
设置单元格的值 公式
func (f *File) SetCellFormula(sheet, axis, formula string, opts ...FormulaOpts) error
-
设置链接 linkType -> Location 本地文件 -> External 外部链接,跳转页面
只是设置链接,不会设置显示的值,也不会设置样式
func (f *File) SetCellHyperLink(sheet, axis, link, linkType string, opts ...HyperlinkOpts) error
-
插入行 插入列 插入的那一行/列的索引就是当前传的参数 传入的就是当前行的索引,从1/A开始的
func (f *File) InsertRow(sheet string, row int) error
func (f *File) InsertCol(sheet, col string) error
-
追加复制的行,就是把当前行在下方在插入
func (f *File) DuplicateRow(sheet string, row int) error
-
复制指定行到指定行 把row行复制到row2行
func (f *File) DuplicateRowTo(sheet string, row, row2 int) error
-
删除行/列
func (f *File) RemoveRow(sheet string, row int) error
func (f *File) RemoveCol(sheet, col string) error
-
在指定sheet页查询满足正则的单元格
搜索到的单元格是满足字符匹配的 也就是说,满足unicode编码的单元格
func (f *File) SearchSheet(sheet, value string, reg ...bool) ([]string, error)
-
设置工作表(sheet页)的名称
func (f *File) SetSheetName(oldName, newName string)
-
通过切片生成整行的数据 第三个参数是切片的指针
func (f *File) SetSheetRow(sheet, axis string, slice interface{}) error
-
行列索引与单元格坐标的转换
func excelize.JoinCellName(col string, row int) (string, error)
行列索引转单元格坐标
func excelize.SplitCellName(cell string) (string, int, error)
单元格转列行索引 -
合并单元格
func (*excelize.File).MergeCell(sheet string, hcell string, vcell string) error
后面两个参数是起始单元格坐标和结束单元格的坐标获取到的合并单元格的所有基础单元格的值都是一样的,不知道是不是excel版本的问题,待确认
代码:main.go
package main
import (
"fmt"
"github.com/xuri/excelize/v2"
)
func main() {
f := excelize.NewFile()
f.Path = "../excel_files/TMP_02.xlsx"
// 获取当前活动sheet页的索引
sheet_idx := f.GetActiveSheetIndex()
sheetName := f.GetSheetName(sheet_idx)
// 设置单元格的值
// 在excel中SetCellValue设置的值都是常规模式
f.SetCellValue(sheetName, "A1", "字符串"<