增加基础的创建excel功能
This commit is contained in:
82
excel/create.go
Normal file
82
excel/create.go
Normal file
@ -0,0 +1,82 @@
|
||||
// Package excel...
|
||||
//
|
||||
// Description : excel...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 2021-11-19 12:25 下午
|
||||
package excel
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/xuri/excelize/v2"
|
||||
)
|
||||
|
||||
// NewExcel 获取excel实例
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 12:28 下午 2021/11/19
|
||||
func NewExcel() *Create {
|
||||
return &Create{
|
||||
fileHandler: excelize.NewFile(),
|
||||
}
|
||||
}
|
||||
|
||||
// Create 创建excel
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 12:27 下午 2021/11/19
|
||||
type Create struct {
|
||||
// fileHandler excel文件处理句柄
|
||||
fileHandler *excelize.File
|
||||
}
|
||||
|
||||
// GenerateSheet 生成sheet
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 12:43 下午 2021/11/19
|
||||
func (c *Create) GenerateSheet(sheetList []SheetData) error {
|
||||
for _, sheet := range sheetList {
|
||||
sheetIndex := c.fileHandler.NewSheet(sheet.Name)
|
||||
if sheet.IsDefault {
|
||||
// 设置活跃
|
||||
c.fileHandler.SetActiveSheet(sheetIndex)
|
||||
}
|
||||
for lineIdx, colList := range sheet.Data {
|
||||
for colIdx, col := range colList {
|
||||
position := c.getColPosition(colIdx, lineIdx)
|
||||
if err := c.fileHandler.SetCellValue(sheet.Name, position, col); nil != err {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Save 保存文件
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 2:04 下午 2021/11/19
|
||||
func (c *Create) Save(fullFilePath string) error {
|
||||
return c.fileHandler.SaveAs(fullFilePath)
|
||||
}
|
||||
|
||||
// getColPosition 获取列名
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 3:14 下午 2021/11/19
|
||||
func (c *Create) getColPosition(colIndex int, dataLineIndex int) string {
|
||||
realIndex := colIndex % 26
|
||||
first := colIndex / 26
|
||||
if first == 0 {
|
||||
return fmt.Sprintf("%s%d", WordMap[realIndex], dataLineIndex+1)
|
||||
}
|
||||
return fmt.Sprintf("%s%s%d", WordMap[first], WordMap[realIndex], dataLineIndex+1)
|
||||
}
|
@ -16,3 +16,46 @@ type ReadResult struct {
|
||||
Error error // 异常信息
|
||||
Result map[string][]map[string]interface{} // 查询结果 sheet => dataList
|
||||
}
|
||||
|
||||
// SheetData ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 12:35 下午 2021/11/19
|
||||
type SheetData struct {
|
||||
IsDefault bool // 是否默认工作表
|
||||
Name string // sheet 名称
|
||||
Data [][]interface{} // 数据列表
|
||||
}
|
||||
|
||||
var (
|
||||
// WordMap 单元格序号映射
|
||||
WordMap = map[int]string{
|
||||
0: "A",
|
||||
1: "B",
|
||||
2: "C",
|
||||
3: "D",
|
||||
4: "E",
|
||||
5: "F",
|
||||
6: "G",
|
||||
7: "H",
|
||||
8: "I",
|
||||
9: "J",
|
||||
10: "K",
|
||||
11: "L",
|
||||
12: "M",
|
||||
13: "N",
|
||||
14: "O",
|
||||
15: "P",
|
||||
16: "Q",
|
||||
17: "R",
|
||||
18: "S",
|
||||
19: "T",
|
||||
20: "U",
|
||||
21: "V",
|
||||
22: "W",
|
||||
23: "X",
|
||||
24: "Y",
|
||||
25: "Z",
|
||||
}
|
||||
)
|
||||
|
Reference in New Issue
Block a user