Golang操作Excel的模块Excelize学习总结2-设置单元格数据

Golang中操作Excel的模块学习作者大佬的文档 : https://xuri.me/excelize/zh-hans/作者大佬的视频 : https://www.bilibili.com/video/BV1hU4y1F7wQ
摘要由CSDN通过智能技术生成

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", "字符串"<
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值