-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexcel.go
76 lines (62 loc) · 1.52 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
package csfutil
import (
"fmt"
"github.com/xuri/excelize/v2"
"strconv"
)
const (
sheet = "Sheet1"
labelColumn = "A"
valueColumn = "B"
extraValueColumn = "C"
)
func ExportExcel(csf *CSFUtil, output string) error {
f := excelize.NewFile()
defer f.Close()
for i := 0; i < len(csf.Order); i++ {
row := strconv.Itoa(i + 1)
if err := f.SetCellValue(sheet, labelColumn+row, csf.Values[csf.Order[i]].Label.ValueString()); err != nil {
return err
}
if err := f.SetCellValue(sheet, valueColumn+row, csf.Values[csf.Order[i]].Value.ValueString()); err != nil {
return err
}
if csf.Values[csf.Order[i]].Value.HaveExtra {
if err := f.SetCellValue(sheet, extraValueColumn+row, csf.Values[csf.Order[i]].Value.ExtraValueString()); err != nil {
return err
}
}
}
return f.SaveAs(output)
}
func ImportExcel(input, output string) error {
f, err := excelize.OpenFile(input)
if err != nil {
return err
}
defer f.Close()
csf, err := Open(output)
if err != nil {
return err
}
rows, err := f.GetRows(sheet)
if err != nil {
return err
}
if len(rows) < 1 {
return fmt.Errorf("no data to read")
}
for i, row := range rows {
switch len(row) {
case 1:
csf.WriteLabelValue(NewLabelValue(row[0], ""), false)
case 2:
csf.WriteLabelValue(NewLabelValue(row[0], row[1]), false)
case 3:
csf.WriteLabelValue(NewLabelValue(row[0], row[1], row[2]), false)
default:
return fmt.Errorf("wrong column count, want 2~3, got %d, at row: %d", len(row), i)
}
}
return csf.Save()
}