forked from mentax/csv2xlsx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwrite.go
179 lines (134 loc) · 2.89 KB
/
write.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package main
import (
"fmt"
"io"
"unicode"
"github.com/tealeg/xlsx/v3"
)
func writeAllSheets(xlFile *xlsx.File, dataFiles []string, sheetNames []string, exampleRowNumber int, delimiter rune) (err error) {
for i, dataFileName := range dataFiles {
sheet, err := getSheet(xlFile, sheetNames, i)
if err != nil {
return err
}
var exampleRow *xlsx.Row
if exampleRowNumber != 0 && exampleRowNumber <= sheet.MaxRow {
// example row counting from 1
exampleRow, _ = sheet.Row(exampleRowNumber - 1)
sheet.RemoveRowAtIndex(exampleRowNumber - 1)
}
err = writeSheet(dataFileName, sheet, exampleRow, delimiter)
if err != nil {
return err
}
}
return nil
}
func writeSheet(dataFileName string, sheet *xlsx.Sheet, exampleRow *xlsx.Row, delimiter rune) error {
data, err := getCsvData(dataFileName)
if err != nil {
return err
}
data.Comma = delimiter
var i int
for {
record, err := data.Read()
if err == io.EOF || record == nil {
break
} else if err != nil {
return err
}
// if i > 5000 {
// break
// }
// if i%500 == 0 {
// fmt.Println(i)
// }
i++
writeRowToXls(sheet, record, exampleRow)
}
return nil
}
func buildXls(p *params) (err error) {
var xlFile *xlsx.File
if p.xlsxTemplate == "" {
xlFile = xlsx.NewFile()
} else {
xlFile, err = xlsx.OpenFile(p.xlsxTemplate)
if err != nil {
return err
}
}
writeAllSheets(xlFile, p.input, p.sheets, p.exampleRow, p.delimiter)
return xlFile.Save(p.output)
}
func writeRowToXls(sheet *xlsx.Sheet, record []string, exampleRow *xlsx.Row) {
var row *xlsx.Row
var cell *xlsx.Cell
row = sheet.AddRow()
var cellsLen int
if exampleRow != nil {
cellsLen = exampleRow.Sheet.MaxCol
}
for k, v := range record {
cell = row.AddCell()
setCellValue(cell, v)
if exampleRow != nil && cellsLen > k {
style := exampleRow.GetCell(k).GetStyle()
format := exampleRow.GetCell(k).GetNumberFormat()
cell.SetStyle(style)
cell.SetFormat(format)
}
}
}
// setCellValue set data in correct format.
func setCellValue(cell *xlsx.Cell, v string) {
// fast path
if v == "" {
cell.SetString("")
return
}
// field marked as string
if v[0] == '\'' {
cell.SetString(v[1:])
return
}
if isNumeric(v) {
cell.SetNumeric(v)
return
}
cell.SetString(v)
}
func getSheet(xlFile *xlsx.File, sheetNames []string, i int) (sheet *xlsx.Sheet, err error) {
var sheetName string
if len(sheetNames) > i {
sheetName = sheetNames[i]
} else {
sheetName = fmt.Sprintf(SheetNamesTemplate, i+1)
}
sheet, ok := xlFile.Sheet[sheetName]
if ok != true {
sheet, err = xlFile.AddSheet(sheetName)
if err != nil {
return nil, err
}
}
return sheet, nil
}
func isNumeric(s string) bool {
//if len(s)> 15{
// return false
//}
for _, c := range s {
if c == '.' {
continue
}
if c == '-' { // minus
continue
}
if !unicode.IsDigit(c) {
return false
}
}
return true
}