This repository has been archived by the owner on Apr 8, 2021. It is now read-only.
forked from csg2008/xls
-
Notifications
You must be signed in to change notification settings - Fork 0
/
workbook.go
368 lines (332 loc) · 8.63 KB
/
workbook.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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
package xls
import (
"bytes"
"encoding/binary"
"io"
"strings"
"unicode/utf16"
)
//WorkBook excel work book
type WorkBook struct {
Debug bool
Is5ver bool
Type uint16
Codepage uint16
Xfs []XF
Fonts []Font
Formats map[uint16]*Format
sheets []*WorkSheet
Author string
rs io.ReadSeeker
sst []string
ref *extSheetRef
continue_utf16 uint16
continue_rich uint16
continue_apsb uint32
dateMode uint16
}
//newWorkBookFromOle2 read workbook from ole2 file
func newWorkBookFromOle2(rs io.ReadSeeker) *WorkBook {
var wb = &WorkBook{
rs: rs,
ref: new(extSheetRef),
sheets: make([]*WorkSheet, 0),
Formats: make(map[uint16]*Format),
}
wb.parse(rs)
wb.prepare()
return wb
}
// SetDebug set debug flag
func (w *WorkBook) SetDebug(debug bool) {
w.Debug = debug
}
func (w *WorkBook) parse(buf io.ReadSeeker) {
b := new(bof)
bp := new(bof)
offset := 0
for {
if err := binary.Read(buf, binary.LittleEndian, b); err == nil {
bp, b, offset = w.parseBof(buf, b, bp, offset)
} else {
break
}
}
}
func (wb *WorkBook) parseBof(buf io.ReadSeeker, b *bof, pre *bof, offset_pre int) (after *bof, after_using *bof, offset int) {
after = b
after_using = pre
var bts = make([]byte, b.Size)
binary.Read(buf, binary.LittleEndian, bts)
item := bytes.NewReader(bts)
switch b.Id {
case XLS_Type_BOF:
bif := new(biffHeader)
binary.Read(item, binary.LittleEndian, bif)
if bif.Ver != 0x600 {
wb.Is5ver = true
}
wb.Type = bif.Type
case XLS_Type_CODEPAGE:
binary.Read(item, binary.LittleEndian, &wb.Codepage)
case XLS_Type_CONTINUE:
if pre.Id == XLS_Type_SST {
var err error
var str string
var size uint16
if wb.continue_utf16 >= 1 {
size = wb.continue_utf16
wb.continue_utf16 = 0
} else {
err = binary.Read(item, binary.LittleEndian, &size)
}
for err == nil && offset_pre < len(wb.sst) {
if size > 0 {
str, err = wb.parseString(item, size, "sst continue")
wb.sst[offset_pre] = wb.sst[offset_pre] + str
}
if err == io.EOF {
break
}
offset_pre++
err = binary.Read(item, binary.LittleEndian, &size)
}
}
offset = offset_pre
after = pre
after_using = b
case XLS_Type_SST:
info := new(SstInfo)
binary.Read(item, binary.LittleEndian, info)
wb.sst = make([]string, info.Count)
var err error
var str string
var size uint16
var i = 0
for ; i < int(info.Count); i++ {
if err = binary.Read(item, binary.LittleEndian, &size); err == nil {
str, err = wb.parseString(item, size, "sst")
wb.sst[i] = wb.sst[i] + str
}
if err == io.EOF {
break
}
}
offset = i
case XLS_Type_SHEET:
var bs = new(boundsheet)
binary.Read(item, binary.LittleEndian, bs)
// different for BIFF5 and BIFF8
wb.addSheet(bs, item)
case XLS_Type_EXTERNSHEET:
if !wb.Is5ver {
binary.Read(item, binary.LittleEndian, &wb.ref.Num)
wb.ref.Info = make([]ExtSheetInfo, wb.ref.Num)
binary.Read(item, binary.LittleEndian, &wb.ref.Info)
}
case XLS_Type_XF:
if wb.Is5ver {
xf := new(Xf5)
binary.Read(item, binary.LittleEndian, xf)
wb.addXf(xf)
} else {
xf := new(Xf8)
binary.Read(item, binary.LittleEndian, xf)
wb.addXf(xf)
}
case XLS_Type_FONT:
f := new(FontInfo)
binary.Read(item, binary.LittleEndian, f)
wb.addFont(f, item)
case XLS_Type_FORMAT:
format := new(Format)
binary.Read(item, binary.LittleEndian, &format.Head)
if raw, err := wb.parseString(item, format.Head.Size, "format"); nil == err && "" != raw {
format.Raw = strings.Split(raw, ";")
} else {
format.Raw = []string{}
}
wb.addFormat(format)
case XLS_Type_DATEMODE:
binary.Read(item, binary.LittleEndian, &wb.dateMode)
}
return
}
func (w *WorkBook) addXf(xf XF) {
w.Xfs = append(w.Xfs, xf)
}
func (w *WorkBook) addFont(font *FontInfo, buf io.ReadSeeker) {
name, _ := w.parseString(buf, uint16(font.NameB), "font")
w.Fonts = append(w.Fonts, Font{Info: font, Name: name})
}
func (w *WorkBook) addFormat(format *Format) {
w.Formats[format.Head.Index] = format
}
func (w *WorkBook) addSheet(sheet *boundsheet, buf io.ReadSeeker) {
name, _ := w.parseString(buf, uint16(sheet.Name), "sheet")
w.sheets = append(w.sheets, &WorkSheet{id: len(w.sheets), bs: sheet, Name: name, wb: w})
}
// prepare process workbook struct
func (w *WorkBook) prepare() {
for k, v := range builtInNumFmt {
if _, ok := w.Formats[k]; !ok {
w.Formats[k] = &Format{
Raw: strings.Split(v, ";"),
}
}
}
for _, v := range w.Formats {
v.Prepare()
}
}
//reading a sheet from the compress file to memory, you should call this before you try to get anything from sheet
func (w *WorkBook) prepareSheet(sheet *WorkSheet) {
w.rs.Seek(int64(sheet.bs.Filepos), 0)
sheet.parse(w.rs)
}
func (w *WorkBook) parseString(buf io.ReadSeeker, size uint16, from string) (res string, err error) {
if w.Is5ver {
var bts = make([]byte, size)
_, err = buf.Read(bts)
res = string(bts)
} else {
var richtext_num = uint16(0)
var phonetic_size = uint32(0)
var flag byte
err = binary.Read(buf, binary.LittleEndian, &flag)
// Rich-Text settings (richtext), 0 = Does not contain Rich-Text settings, 1 = Contains Rich-Text settings
if flag&0x8 != 0 {
err = binary.Read(buf, binary.LittleEndian, &richtext_num)
} else if w.continue_rich > 0 {
richtext_num = w.continue_rich
w.continue_rich = 0
}
// Asian phonetic settings, 0 = Does not contain Asian phonetic settings, 1 = Contains Asian phonetic settings
if flag&0x4 != 0 {
err = binary.Read(buf, binary.LittleEndian, &phonetic_size)
} else if w.continue_apsb > 0 {
phonetic_size = w.continue_apsb
w.continue_apsb = 0
}
// Character compression, 0 = Compressed (8-bit characters), 1 = Uncompressed (16-bit characters)
if flag&0x1 != 0 {
var bts = make([]uint16, size)
var i = uint16(0)
for ; i < size && err == nil; i++ {
err = binary.Read(buf, binary.LittleEndian, &bts[i])
}
if i < size {
w.continue_utf16 = size - i + 1
} else if i == size && err == io.EOF {
w.continue_utf16 = 1
}
if i > 1 && 0 == bts[i-1] {
i--
}
res = string(utf16.Decode(bts[:i]))
} else {
var n int
var bts = make([]byte, size)
n, err = buf.Read(bts)
if uint16(n) < size {
w.continue_utf16 = size - uint16(n)
err = io.EOF
}
if n > 1 && 0 == bts[n-1] {
n--
}
var bts1 = make([]uint16, n)
for k, v := range bts[:n] {
bts1[k] = uint16(v)
}
res = string(utf16.Decode(bts1))
}
if richtext_num > 0 {
var bts []byte
var ss int64
if w.Is5ver {
ss = int64(2 * richtext_num)
} else {
ss = int64(4 * richtext_num)
}
bts = make([]byte, ss)
err = binary.Read(buf, binary.LittleEndian, bts)
if err == io.EOF {
w.continue_rich = richtext_num
}
}
if phonetic_size > 0 {
var bts []byte
bts = make([]byte, phonetic_size)
err = binary.Read(buf, binary.LittleEndian, bts)
if err == io.EOF {
w.continue_apsb = phonetic_size
}
}
}
return
}
// Format format value to string
func (w *WorkBook) Format(xf uint16, v float64) (string, bool) {
var val string
var idx = int(xf)
if len(w.Xfs) > idx {
if formatter := w.Formats[w.Xfs[idx].FormatNo()]; nil != formatter {
return formatter.String(v), true
}
}
return val, false
}
//GetSheet get one sheet by its number
func (w *WorkBook) GetSheet(num int) *WorkSheet {
if num < len(w.sheets) {
s := w.sheets[num]
if !s.parsed {
w.prepareSheet(s)
}
return s
}
return nil
}
//NumSheets Get the number of all sheets, look into example
func (w *WorkBook) NumSheets() int {
return len(w.sheets)
}
//ReadAllCells helper function to read all cells from file
//Notice: the max value is the limit of the max capacity of lines.
//Warning: the helper function will need big memory if file is large.
func (w *WorkBook) ReadAllCells(max int) (res [][]string) {
res = make([][]string, 0)
for _, sheet := range w.sheets {
if len(res) < max {
max = max - len(res)
w.prepareSheet(sheet)
if sheet.MaxRow != 0 {
length := int(sheet.MaxRow) + 1
if max < length {
length = max
}
temp := make([][]string, length)
for k, row := range sheet.rows {
data := make([]string, 0)
if len(row.cols) > 0 {
for _, col := range row.cols {
if uint16(len(data)) <= col.LastCol() {
data = append(data, make([]string, col.LastCol()-uint16(len(data))+1)...)
}
str := col.String(w)
for i := uint16(0); i < col.LastCol()-col.FirstCol()+1; i++ {
data[col.FirstCol()+i] = str[i]
}
}
if length > int(k) {
temp[k] = data
}
}
}
res = append(res, temp...)
}
}
}
return
}