-
Notifications
You must be signed in to change notification settings - Fork 40
/
connect.go
292 lines (268 loc) · 7.43 KB
/
connect.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
package excel
import (
"archive/zip"
"bytes"
"errors"
"fmt"
"io"
"strings"
)
// connect is default implement of connector.
type connect struct {
// list of sorted sheet name
sheets []string
sharedStringPaths []string
// xl/sharedStringPaths.xml
sharedStringPathsFile *zip.File
// xl/_rels/workbook.xml.rels
workbookRels *zip.File
// map["rId*"]"xl/path/to/target";
// just support rels for workbook now.
workbookRelsIDMap map[string]string
// xl/workbook.xml
workbookFile *zip.File
// map["sheet_id"]"sheet_name"
worksheetIDToNameMap map[string]string
// "xl/worksheets/sheet*.xml"
// map["xl/path/to/sheet*.xml"]*zip.File
worksheetFileMap map[string]*zip.File
// map["sheet_name"]*zip.File
worksheetNameFileMap map[string]*zip.File
worksheetNameList []string
// 实际的读取接口
zipReader *zip.Reader
// 仅读取文件时有效
zipReaderCloser io.ReadCloser
}
// Open a excel file
func (conn *connect) Open(filePath string) error {
var err error
zipReaderCloser, err := zip.OpenReader(filePath)
if err != nil {
return err
}
return conn.OpenReader(&zipReaderCloser.Reader)
}
// Open a excel file from a zipReader
func (conn *connect) OpenReader(reader *zip.Reader) error {
conn.zipReader = reader
// prepare for files
err := conn.init()
if err != nil {
if conn.zipReaderCloser != nil {
conn.zipReaderCloser.Close()
conn.zipReaderCloser = nil
}
conn.zipReader = nil
return err
}
return nil
}
// OpenReader read a binary of xlsx file.
func (conn *connect) OpenBinary(xlsxData []byte) error {
rd := bytes.NewReader(xlsxData)
var err error
conn.zipReader, err = zip.NewReader(rd, int64(rd.Len()))
if err != nil {
return err
}
// prepare for files
err = conn.init()
if err != nil {
// 没有zipReader,不用Close
return err
}
return nil
}
// Close file reader
func (conn *connect) Close() error {
if conn.zipReaderCloser != nil {
err := conn.zipReaderCloser.Close()
if err != nil {
return err
}
conn.zipReaderCloser = nil
}
conn.zipReader = nil
conn.sheets = conn.sheets[:0]
conn.sharedStringPaths = conn.sharedStringPaths[:0]
conn.sharedStringPathsFile = nil
conn.workbookFile = nil
conn.worksheetFileMap = nil
conn.worksheetNameFileMap = nil
return nil
}
// NewReader generate an new reader of a sheet
// sheetNamer: if sheetNamer is string, will use sheet as sheet name.
// if sheetNamer is int, will i'th sheet in the workbook, be careful the hidden sheet is counted. i ∈ [1,+inf]
// if sheetNamer is a object implements `GetXLSXSheetName()string`, the return value will be used.
// otherwise, will use sheetNamer as struct and reflect for it's name.
func (conn *connect) NewReader(sheetNamer interface{}) (Reader, error) {
return conn.NewReaderByConfig(&Config{Sheet: sheetNamer})
}
// MustReader will panic instead of return error
func (conn *connect) MustReader(sheetNamer interface{}) Reader {
rd, err := conn.NewReader(sheetNamer)
if err != nil {
panic(err)
}
return rd
}
// NewReaderByConfig make a new reader by config
func (conn *connect) NewReaderByConfig(config *Config) (Reader, error) {
if conn.zipReader == nil {
return nil, ErrConnectNotOpened
}
sheet := conn.parseSheetName(config.Sheet)
sheet = config.Prefix + sheet + config.Suffix
workSheetFile, ok := conn.worksheetNameFileMap[sheet]
if !ok {
return nil, fmt.Errorf("can not find worksheet named = %s", sheet)
}
rc, err := workSheetFile.Open()
if err != nil {
return nil, err
}
reader, err := newReader(conn, rc, config.TitleRowIndex, config.Skip, sheet)
return reader, err
}
// MustReaderByConfig panic insead of return error
func (conn *connect) MustReaderByConfig(config *Config) Reader {
rd, err := conn.NewReaderByConfig(config)
if err != nil {
panic(err)
}
return rd
}
// GetSheetNames return the sheet names.
func (conn *connect) GetSheetNames() []string {
if len(conn.worksheetNameFileMap) != len(conn.worksheetNameList) {
conn.worksheetNameList = make([]string, 0, len(conn.worksheetNameFileMap))
for name := range conn.worksheetNameFileMap {
conn.worksheetNameList = append(conn.worksheetNameList, name)
}
}
dst := make([]string, len(conn.worksheetNameList))
copy(dst, conn.worksheetNameList)
return dst
}
func (conn *connect) getSharedString(id int) string {
return conn.sharedStringPaths[id]
}
func (conn *connect) init() (err error) {
// Find file of "workbook.xml", "sharedString.xml" and files in worksheets
conn.worksheetFileMap = make(map[string]*zip.File)
for _, f := range conn.zipReader.File {
switch f.Name {
case _SharedStringPath:
conn.sharedStringPathsFile = f
case _WorkBookPath:
conn.workbookFile = f
case _WorkBookRels:
conn.workbookRels = f
default:
if strings.HasPrefix(f.Name, _WorkSheetsPrefix) {
// log.Println("WorksheetName:", f.Name)
conn.worksheetFileMap[f.Name] = f
}
}
}
if conn.workbookRels == nil {
return ErrWorkbookRelsNotExist
}
if conn.workbookFile == nil {
return ErrWorkbookNotExist
}
if conn.sharedStringPathsFile == nil {
// sharedStringPathsFile may not exist.
// return ErrSharedStringsNotExist
}
if conn.worksheetFileMap == nil || len(conn.worksheetFileMap) == 0 {
return ErrWorkbookNotExist
}
// prepare workbook rels
err = conn.readWorkbookRels()
if err != nil {
return errors.New("read workbook rels failed:" + err.Error())
}
// prepare workbook
err = conn.readWorkbook()
if err != nil {
return errors.New("read workbook failed:" + err.Error())
}
// prepare sharedstring
err = conn.readSharedString()
if err != nil {
return errors.New("read shared string failed:" + err.Error())
}
return nil
}
func (conn *connect) readWorkbookRels() error {
rc, err := conn.workbookRels.Open()
if err != nil {
return err
}
wbRels, err := readWorkbookRelsXML(rc)
if err != nil {
rc.Close()
return err
}
conn.workbookRelsIDMap = make(map[string]string, len(wbRels.Relationships))
for _, rel := range wbRels.Relationships {
if rel.Type == _RelTypeWorkSheet {
// Is a rels for worksheet
conn.workbookRelsIDMap[rel.ID] = _XL + rel.Target
}
}
// log.Println(conn.workbookRelsIDMap)
return nil
}
func (conn *connect) readWorkbook() error {
// Find name of sheets
rc, err := conn.workbookFile.Open()
if err != nil {
return err
}
wb, err := readWorkbookXML(rc)
if err != nil {
rc.Close()
return err
}
if conn.sheets == nil {
conn.sheets = make([]string, 0, len(wb.Sheets.Sheet))
}
conn.worksheetNameFileMap = make(map[string]*zip.File, len(wb.Sheets.Sheet))
conn.worksheetIDToNameMap = make(map[string]string, len(wb.Sheets.Sheet))
for _, sheet := range wb.Sheets.Sheet {
conn.sheets = append(conn.sheets, sheet.Name)
// record the sheet name to *zip.File
sheetTargetPath, ok := conn.workbookRelsIDMap[sheet.RID]
if !ok {
return fmt.Errorf("Sheet.RID = %s not exist", sheet.RID)
}
// log.Println(sheetTargetPath)
file, ok := conn.worksheetFileMap[sheetTargetPath]
if !ok {
return fmt.Errorf("Sheet %s not exist", sheetTargetPath)
}
// log.Println(sheet.Name)
conn.worksheetNameFileMap[sheet.Name] = file
conn.worksheetIDToNameMap[sheet.SheetID] = sheet.Name
}
rc.Close()
return nil
}
func (conn *connect) readSharedString() error {
// sharedStringPathsFile may not exist
if conn.sharedStringPathsFile == nil {
conn.sharedStringPaths = make([]string, 0)
return nil
}
rc, err := conn.sharedStringPathsFile.Open()
if err != nil {
return err
}
conn.sharedStringPaths = readSharedStringsXML(rc)
rc.Close()
return nil
}