game_sync/statistics/task/excelmgr.go

89 lines
1.4 KiB
Go

package main
import (
"fmt"
"github.com/xuri/excelize/v2"
)
type ExcelData struct {
*excelize.File
Head []string
Index int
IndexCell int
}
func (e *ExcelData) SetHead(head []string) *ExcelData {
if e == nil {
return nil
}
e.Index = 1
e.SetSheetRow("Sheet1", "A1", &head)
return e
}
func (e *ExcelData) SetRow(row []string) *ExcelData {
if e == nil {
return nil
}
e.Index++
e.SetSheetRow("Sheet1", "A"+fmt.Sprintf("%d", e.Index), &row)
return e
}
func (e *ExcelData) SetCell(val interface{}) *ExcelData {
if e == nil {
return nil
}
e.IndexCell++
cell := fmt.Sprintf("%c%d", 'A'+e.IndexCell-1, e.Index)
e.SetCellValue("Sheet1", cell, val)
return e
}
func (e *ExcelData) NewLine() *ExcelData {
if e == nil {
return nil
}
e.Index++
e.IndexCell = 0
return e
}
type ExcelMgr struct {
List map[int]*ExcelData
}
func NewExcelMgr() *ExcelMgr {
return &ExcelMgr{
List: make(map[int]*ExcelData),
}
}
func (e *ExcelMgr) Register(id int, head []string) *ExcelData {
e.List[id] = &ExcelData{
File: excelize.NewFile(),
Head: head,
Index: 0,
}
e.List[id].NewSheet("Sheet1")
if len(head) > 0 {
e.List[id].SetHead(head)
}
return e.List[id]
}
func (e *ExcelMgr) Get(id int) *ExcelData {
return e.List[id]
}
func (e *ExcelMgr) Save(id int, fileName string) error {
d := e.List[id]
if d == nil {
return nil
}
return d.SaveAs(fileName)
}