Golang *** 作Excel的模块Excelize学习总结-工具函数

Golang *** 作Excel的模块Excelize学习总结-工具函数,第1张

常用的工具函数

打开文件
func OpenFile(filename string, opt ...Options) (*File, error)

新建文件
func excelize.NewFile() *excelize.File

拆分单元格坐标 单元格坐标字符串拆分成 列名称 行索引
func excelize.SplitCellName(cell string) (string, int, error)

通过行列名称组合单元格坐标
func excelize.JoinCellName(col string, row int) (string, error)

列名转索引
func excelize.ColumnNameToNumber(name string) (int, error)

列索引转列名
func excelize.ColumnNumberToName(num int) (string, error)

坐标字符串转索引 行,列的数字索引
func excelize.CellNameToCoordinates(cell string) (int, int, error)

行列数字索引转坐标字符串 最后一个是否绝对坐标 例如:$A$1
func excelize.CoordinatesToCellName(col int, row int, abs ...bool) (string, error)

其他一些知识点

设置行样式
func (*excelize.File).SetColStyle(sheet string, columns string, styleID int) error

设置列宽
func (*excelize.File).SetColWidth(sheet string, startcol string, endcol string, width float64) error

设置行高
func (*excelize.File).SetRowHeight(sheet string, row int, height float64) error

创建表格
func (*excelize.File).AddTable(sheet string, hcell string, vcell string, format string) error

format 字符串其实在底层会转换成 formatTable机构提
type formatTable struct {
TableName string json:"table_name"
TableStyle string json:"table_style"
ShowFirstColumn bool json:"show_first_column"
ShowLastColumn bool json:"show_last_column"
ShowRowStripes bool json:"show_row_stripes"
ShowColumnStripes bool json:"show_column_stripes"
}
但是这是个私有的结构体
所以在使用时只有通过json字符串的方式传入

table_style 是内置的表格样式 选择范围是
- TableStyleLight1 - TableStyleLight21
- TableStyleMedium1 - TableStyleMedium28
- TableStyleDark1 - TableStyleDark11show_row_stripes 是否条纹样式显示 就是奇偶行填充颜色不同show_column_stripes 是否条纹样式显示 就是奇偶列填充颜色不同
自动筛选
func (*excelize.File).AutoFilter(sheet string, hcell string, vcell string, format string) error

当format不填,只是将首行转换成筛选框
如果有筛选条件,会将筛选条件加到指定列,但是并不会筛选

package main

import (
	"fmt"

	"github.com/xuri/excelize/v2"
)

func main() {
	// 打开已存在的文件
	wb, err := excelize.OpenFile("../excel_files/TMP_06.xlsx")
	if err != nil {
		fmt.Println(err)
	}
	fmt.Printf("wb.SheetCount: %v\n", wb.SheetCount)
	// 新建文件
	file := excelize.NewFile()
	fmt.Printf("file.SheetCount: %v\n", file.SheetCount)

	// 拆分单元格坐标
	col, row, err := excelize.SplitCellName("AA10")
	if err != nil {
		fmt.Println(err)
	}
	fmt.Println(col, row)
	// 通过行列名称组合单元格坐标
	cell, err := excelize.JoinCellName(col, row)
	if err != nil {
		fmt.Println(err)
	}
	fmt.Printf("cell: %v\n", cell)

	// 列名转索引
	col_n, err := excelize.ColumnNameToNumber("AA")
	if err != nil {
		fmt.Println(err)
	}
	fmt.Printf("col_n: %v\n", col_n)
	// 列索引转列名
	col_nm, err := excelize.ColumnNumberToName(100)
	if err != nil {
		fmt.Println(err)
	}
	fmt.Printf("col_nm: %v\n", col_nm)
	// 坐标字符串转索引  行,列的数字索引
	coln, rown, err := excelize.CellNameToCoordinates("A1")
	if err != nil {
		fmt.Println(err)
	}
	fmt.Printf("rown: %v\n", rown)
	fmt.Printf("coln: %v\n", coln)
	// 行列索引转坐标字符串
	cell_nm, err := excelize.CoordinatesToCellName(2, 2)
	if err != nil {
		fmt.Println(err)
	}
	fmt.Printf("cell_nm: %v\n", cell_nm)
	cell_nm2, err := excelize.CoordinatesToCellName(2, 2, true)
	if err != nil {
		fmt.Println(err)
	}
	fmt.Printf("cell_nm2: %v\n", cell_nm2) // $C  绝对坐标

	// 补充一些前面文档遗漏的
	// 设置行样式
	style_idx, err := wb.NewStyle(`{"border":[{
						"type":"top",
						"color":"#000000",
						"style":1
					},{
						"type":"left",
						"color":"#000000",
						"style":1
					},{
						"type":"right",
						"color":"#000000",
						"style":1
					},{
						"type":"bottom",
						"color":"#000000",
						"style":1
					}
				],"font":{
					"size":12,
					"color":"#000000",
					"family":"仿宋",
					"bold":true
				},"fill":{
					"type":"gradient",
					"color":["#00aa00","#00aa00"],
					"shading":1
				},"alignment":{
					"horizontal":"center",
					"vertical":"center"
				}
				}`)

	if err != nil {
		fmt.Println("创建样式", err)
	}
	if err := wb.SetColStyle("Sheet1", "A:G", style_idx); err != nil {
		fmt.Println(err)
	}
	// 设置列宽
	if err := wb.SetColWidth("Sheet1", "A", "G", 18); err != nil {
		fmt.Println(err)
	}
	// 设置行高
	if err := wb.SetRowHeight("Sheet1", 1, 7); err != nil {
		fmt.Println(err)
	}
	// 创建表格
	sheet_idx := wb.NewSheet("Sheet2")
	wb.SetActiveSheet(sheet_idx)

	if err := wb.AddTable("Sheet2", "A1", "C10", ""); err != nil {
		fmt.Println(err)
	}
	// 应用样式   table_style 引用excel中的样式
	/*table_style 的取值范围
	TableStyleLight1 - TableStyleLight21
	TableStyleMedium1 - TableStyleMedium28
	TableStyleDark1 - TableStyleDark11
	*/
	/*
		show_row_stripes 是否奇偶行不同颜色显示
		show_col_stripes 奇偶列
	*/
	if err := wb.AddTable("Sheet2", "F1", "H10", `{
		"table_name":"table",
		"table_style":"TableStyleMedium2",
		"show_first_column":true,
		"show_row_stripes":true
	}`); err != nil {
		fmt.Println(err)
	}
	// 设置筛选,在已有数据的基础上加上筛选
	if err := wb.AutoFilter("Sheet2", "J6", "N20", ``); err != nil {
		fmt.Println(err)
	}

	// 先设置列样式
	idx, err := wb.NewStyle(`{"number_format":1}`)
	if err != nil {
		fmt.Println(err)
	}
	if err := wb.SetColStyle("Sheet1", "Q", idx); err != nil {
		fmt.Println(err)
	}
	// 设置过滤条件    只是会设置筛选条件,但是并不会筛选
	if err := wb.AutoFilter("Sheet1", "O1", "T6", `{"column":"Q","expression":"x >= 20 "}`); err != nil {
		fmt.Println(err)
	}

	// 保存
	wb.Save()
}

更多内容去到大佬的文档或者视频
作者大佬的文档 : https://xuri.me/excelize/zh-hans/
作者大佬的视频 : https://www.bilibili.com/video/BV1hU4y1F7wQ

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/995644.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-21
下一篇 2022-05-21

发表评论

登录后才能评论

评论列表(0条)

保存