-
打开文件
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 - TableStyleDark11- show_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: %vn", wb.SheetCount)
// 新建文件
file := excelize.NewFile()
fmt.Printf("file.SheetCount: %vn", 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: %vn", cell)
// 列名转索引
col_n, err := excelize.ColumnNameToNumber("AA")
if err != nil {
fmt.Println(err)
}
fmt.Printf("col_n: %vn", col_n)
// 列索引转列名
col_nm, err := excelize.ColumnNumberToName(100)
if err != nil {
fmt.Println(err)
}
fmt.Printf("col_nm: %vn", col_nm)
// 坐标字符串转索引 行,列的数字索引
coln, rown, err := excelize.CellNameToCoordinates("A1")
if err != nil {
fmt.Println(err)
}
fmt.Printf("rown: %vn", rown)
fmt.Printf("coln: %vn", coln)
// 行列索引转坐标字符串
cell_nm, err := excelize.CoordinatesToCellName(2, 2)
if err != nil {
fmt.Println(err)
}
fmt.Printf("cell_nm: %vn", cell_nm)
cell_nm2, err := excelize.CoordinatesToCellName(2, 2, true)
if err != nil {
fmt.Println(err)
}
fmt.Printf("cell_nm2: %vn", cell_nm2) // $C$3 绝对坐标
// 补充一些前面文档遗漏的
// 设置行样式
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中的样式
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



