-
Notifications
You must be signed in to change notification settings - Fork 3
/
stream_reader_test.go
112 lines (99 loc) · 2.54 KB
/
stream_reader_test.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
// Copyright (c) 2017 Andrey Gayvoronsky <[email protected]>
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package ooxml_test
import (
"archive/zip"
"encoding/xml"
"fmt"
"github.com/plandem/ooxml"
"github.com/plandem/ooxml/ml"
)
func ExampleStreamFileReader() {
type Cell struct {
Formula *ml.Reserved `xml:"f,omitempty"`
Value string `xml:"v,omitempty"`
InlineStr *ml.Reserved `xml:"is,omitempty"`
ExtLst *ml.Reserved `xml:"extLst,omitempty"`
Ref string `xml:"r,attr"`
Style int `xml:"s,attr,omitempty"`
Type string `xml:"t,attr,omitempty"`
Cm ml.OptionalIndex `xml:"cm,attr,omitempty"`
Vm ml.OptionalIndex `xml:"vm,attr,omitempty"`
Ph bool `xml:"ph,attr,omitempty"`
}
type Row struct {
Cells []*Cell `xml:"c"`
ExtLst *ml.Reserved `xml:"extLst,omitempty"`
Ref int `xml:"r,attr,omitempty"`
Spans string `xml:"spans,attr,omitempty"`
Style int `xml:"s,attr,omitempty"`
CustomFormat bool `xml:"customFormat,attr,omitempty"`
Height float32 `xml:"ht,attr,omitempty"`
Hidden bool `xml:"hidden,attr,omitempty"`
CustomHeight bool `xml:"customHeight,attr,omitempty"`
OutlineLevel uint8 `xml:"outlineLevel,attr,omitempty"`
Collapsed bool `xml:"collapsed,attr,omitempty"`
ThickTop bool `xml:"thickTop,attr,omitempty"`
ThickBot bool `xml:"thickBot,attr,omitempty"`
Phonetic bool `xml:"ph,attr,omitempty"`
}
openSheet := func(cb func(f *zip.File)) {
z, err := zip.OpenReader(`./test_files/example_simple.xlsx`)
if err != nil {
panic(err)
}
defer z.Close()
for _, f := range z.File {
if f.Name == `xl/worksheets/sheet1.xml` {
cb(f)
break
}
}
}
openSheet(func(f *zip.File) {
stream, _ := ooxml.NewStreamFileReader(f)
for {
t, _ := stream.Token()
if t == nil {
break
}
switch start := t.(type) {
case xml.StartElement:
if start.Name.Local == "row" {
var row Row
if stream.DecodeElement(&row, &start) == nil {
for _, c := range row.Cells {
if c.Type == "s" || c.Value == "" {
continue
}
fmt.Printf("%+v\n", c.Value)
}
}
}
}
}
_ = stream.Close()
})
//Output:
// 1
// 2
// 3
// 4
// 5
// 6
// 7
// 8
// 9
// 10
// 11
// 12
// 13
// 14
// 15
// 16
// 17
// 18
// 19
// 20
}