-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxlsx_src.go
58 lines (53 loc) · 1.19 KB
/
xlsx_src.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
package xlsxcfg
import (
"context"
"log"
"github.com/xuri/excelize/v2"
)
func LoadXlsxFiles(ctx context.Context, param *Config, files ...string) (data map[string][]any, err error) {
data = map[string][]any{}
var configData map[string][]any
for _, xlsFile := range files {
configData, err = loadXlsxFile(ctx, xlsFile, param)
if err != nil {
return
}
for sht, d := range configData {
if data[sht] != nil {
log.Printf("duplicated sheet[%s] in file: %s", sht, xlsFile)
}
data[sht] = d
}
}
return
}
func loadXlsxFile(ctx context.Context, filePath string, param *Config) (data map[string][]any, err error) {
f, err := excelize.OpenFile(filePath)
if err != nil {
return
}
defer f.Close()
sheets := f.WorkBook.Sheets.Sheet
data = make(map[string][]any, len(sheets))
for _, sh := range sheets {
sht, err1 := f.Cols(sh.Name)
if err1 != nil {
return nil, err1
}
sp := newSheetParser(param)
sp.SetName(sh.Name + param.Sheet.RowTypeSuffix)
for sht.Next() {
cells, err := sht.Rows()
if err != nil {
return nil, err
}
sp.Feed(cells)
}
sheetData, err1 := sp.Parse(ctx)
if err1 != nil {
return nil, err1
}
data[sh.Name] = sheetData
}
return
}