-
Notifications
You must be signed in to change notification settings - Fork 0
/
excel.go
78 lines (64 loc) · 1.24 KB
/
excel.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package excel
import (
"fmt"
"io"
"github.com/xuri/excelize/v2"
)
const (
FirstRowIndex = 0
)
func OpenFile(filePath string) (*excelize.File, error) {
file, err := excelize.OpenFile(filePath)
if err != nil {
return nil, err
}
return file, nil
}
func OpenReader(reader io.Reader) (*excelize.File, error) {
file, err := excelize.OpenReader(reader)
if err != nil {
return nil, err
}
return file, nil
}
func ToBytes(file *excelize.File) ([]byte, error) {
buf, err := file.WriteToBuffer()
if err != nil {
return nil, err
}
return buf.Bytes(), nil
}
func GetSheetName(file *excelize.File, index int) string {
return file.GetSheetName(index)
}
func Close(f *excelize.File) {
if f != nil {
_ = f.Close()
}
}
func ToChar(i int) string {
return string(rune('A' + i))
}
func ToCellName(cellIndex int, rowIndex int) string {
return fmt.Sprint(ToChar(cellIndex), rowIndex+1)
}
type GenerateExcelConfig[T any] struct {
Header string
Value func(obj *T) string
}
type DataRef struct {
SheetName string
CellName string
Data interface{}
TextColor string
CellFormula string
}
type ErrorDataRow struct {
CellErrors *[]DataRef
Detail *DataRef
}
type RowInfo struct {
SheetName string
RowIndex int
RowData []string
}