增加基础的创建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)
|
||||
}
|
Reference in New Issue
Block a user