excel_reader.go
3.9 KB
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
180
181
182
183
package excel
import (
"bytes"
"encoding/csv"
"github.com/extrame/xls"
"github.com/xuri/excelize/v2"
"golang.org/x/text/encoding/simplifiedchinese"
"golang.org/x/text/transform"
"io"
"unicode/utf8"
)
type Reader interface {
Read(importer *Importer, r File) ([][]string, error)
Header() HeaderInfo
}
var _ Reader = (*XLXSReader)(nil)
type XLXSReader struct {
headerInfo HeaderInfo
}
func (reader *XLXSReader) Read(excelImport *Importer, f File) ([][]string, error) {
excelFile, err := excelize.OpenReader(f)
if err != nil {
return nil, err
}
sheets := excelFile.GetSheetList()
if len(sheets) > 0 && sheets[0] != excelImport.Sheet {
excelImport.Sheet = sheets[0]
}
rows, err := excelFile.Rows(excelImport.Sheet)
if err != nil {
return nil, err
}
var (
rowDataList = make([][]string, 0) //数据列表
rowIndex int //行计数
lenColumn int
)
for rows.Next() {
rowIndex++
cols, err := rows.Columns()
if err != nil {
return nil, err
}
if rowIndex < excelImport.RowBegin {
continue
}
if rowIndex == excelImport.RowBegin {
reader.headerInfo = NewHeaderInfo(cols)
lenColumn = len(cols)
continue
}
if len(cols) > 0 {
if len(cols) < lenColumn {
padding := make([]string, lenColumn-len(cols))
cols = append(cols, padding...)
}
rowDataList = append(rowDataList, cols)
}
}
return rowDataList, nil
}
func (reader *XLXSReader) Header() HeaderInfo {
return reader.headerInfo
}
var _ Reader = (*XLSReader)(nil)
type XLSReader struct {
headerInfo HeaderInfo
}
func (reader *XLSReader) Read(excelImport *Importer, f File) ([][]string, error) {
wb, err := xls.OpenReader(f, "utf-8")
if err != nil {
return nil, err
}
sheet := wb.GetSheet(0)
var (
rowDataList = make([][]string, 0) //数据列表
rowIndex int //行计数
lenColumn int
)
for rowIndex <= int(sheet.MaxRow) {
row := sheet.Row(rowIndex)
cols := make([]string, 0)
if row.LastCol() > 0 {
for j := 0; j < row.LastCol(); j++ {
col := row.Col(j)
cols = append(cols, col)
}
}
rowIndex++
if rowIndex < excelImport.RowBegin {
continue
}
if rowIndex == excelImport.RowBegin {
reader.headerInfo = NewHeaderInfo(cols)
lenColumn = len(cols)
continue
}
if len(cols) == lenColumn {
rowDataList = append(rowDataList, cols)
}
}
return rowDataList, nil
}
func (reader *XLSReader) Header() HeaderInfo {
return reader.headerInfo
}
var _ Reader = (*CSVReader)(nil)
type CSVReader struct {
headerInfo HeaderInfo
}
func (reader *CSVReader) Read(excelImport *Importer, f File) ([][]string, error) {
utf8Reader, err := reader.PrepareCheck(f)
if err != nil {
return nil, err
}
csvReader := csv.NewReader(utf8Reader)
csvReader.FieldsPerRecord = -1
csvReader.LazyQuotes = true
records, err := csvReader.ReadAll()
if err != nil {
return nil, err
}
var (
rowDataList = make([][]string, 0) //数据列表
rowIndex int //行计数
lenColumn int
)
for i := 0; i < len(records); i++ {
rowIndex++
if rowIndex < excelImport.RowBegin {
continue
}
cols := records[i]
if rowIndex == excelImport.RowBegin {
reader.headerInfo = NewHeaderInfo(cols)
lenColumn = len(cols)
continue
}
if len(cols) > 0 {
if len(cols) != lenColumn {
padding := make([]string, lenColumn-len(cols))
cols = append(cols, padding...)
}
rowDataList = append(rowDataList, cols)
}
}
return rowDataList, nil
}
func (reader *CSVReader) Header() HeaderInfo {
return reader.headerInfo
}
func (reader *CSVReader) PrepareCheck(r io.Reader) (io.Reader, error) {
return GBKToUtf8(r)
}
func GBKToUtf8(readIn io.Reader) (io.Reader, error) {
var (
err error
fileByte []byte
)
fileByte, err = io.ReadAll(readIn)
if err != nil {
return nil, err
}
if utf8.Valid(fileByte) {
return bytes.NewReader(fileByte), nil
} else {
utf8Reader := transform.NewReader(bytes.NewReader(fileByte), simplifiedchinese.GBK.NewDecoder())
return utf8Reader, nil
}
}