-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstreamxlsx.go
253 lines (227 loc) · 5.99 KB
/
streamxlsx.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
// Package streamxlsx implements a streaming .xlsx (Excel spreadsheat) file writer.
//
// The focus is to easily generate tabular data files.
package streamxlsx
import (
"archive/zip"
"fmt"
"io"
)
type StreamXLSX struct {
zip *zip.Writer
openSheet *sheetEncoder
finishedSheets []string
// The stylesheet will be written on Close(). You generally won't want to
// use this directly, but via `Format()`.
Styles *Stylesheet
styleCache map[string]int
error error // returned with Close()
}
// New creates a new file. Do Close() it afterwards. No need to check every
// write for errors, Close() will return the last error.
//
// A StreamXLSX is not safe to use with multiple go routines at the same time.
func New(w io.Writer) *StreamXLSX {
s := &StreamXLSX{
zip: zip.NewWriter(w),
Styles: &Stylesheet{},
styleCache: map[string]int{},
}
// empty style. Not 100% it's needed
styleID := s.Styles.GetCellStyleID(Xf{})
s.Styles.GetCellID(Xf{XfID: &styleID})
s.writeRelations()
return s
}
// Write a row to the current sheet.
// No values is a valid (empty) row, and not every row needs to have the same
// number of elements.
//
// Supported cell datatypes:
// all ints and uints, floats, string, bool
// Additional special cases:
// []byte: will be base64 encoded
// time.Time: handled, but you need to Format() it. For example: s.Format("mm-dd-yy", aTimeTime)
// Hyperlink{}: will make the cell a hyperlink
// Cell{}: if you want to set everything manually
//
// See Format() to apply number formatting to cells.
func (s *StreamXLSX) WriteRow(vs ...interface{}) error {
if s.error != nil {
return s.error
}
sh, err := s.sheet()
if err != nil {
s.error = err
return err
}
if err := sh.writeRow(vs...); err != nil {
s.error = err
return err
}
return nil
}
// WriteSheet closes the currenly open sheet, with the given title.
// The process is you first do all the `WriteRow()`s for a sheet, followed by
// its WriteSheet(). There is always an open sheet. You don't have to close
// the final sheet, but it'll give you a boring name ("sheet N").
func (s *StreamXLSX) WriteSheet(title string) error {
if s.error != nil {
return s.error
}
// make sure there is a sheet open
if _, err := s.sheet(); err != nil {
s.error = err
return err
}
s.openSheet.Close()
if err := s.writeSheetRelations(); err != nil { // for hyperlink refs
s.error = err
return err
}
s.openSheet = nil
s.finishedSheets = append(s.finishedSheets, title)
return nil
}
// Adds a number format to a cell. Examples or formats are "0.00", "0%", ...
// This is used to wrap a value in a WriteRow().
func (s *StreamXLSX) Format(code string, cell interface{}) Cell {
if xfID, ok := s.styleCache[code]; ok {
c, err := applyStyle(xfID, cell)
if err != nil {
s.error = err
}
return c
}
numFmtID := s.Styles.GetNumFmtID(code)
cellStyleID := s.Styles.GetCellStyleID(Xf{})
styleFx := Xf{
NumFmtID: numFmtID,
ApplyNumberFormat: 1,
XfID: &cellStyleID,
}
xfID := s.Styles.GetCellID(styleFx)
s.styleCache[code] = xfID
c, err := applyStyle(xfID, cell)
if err != nil {
s.error = err
}
return c
}
// Adds a hyperlink in a cell. You can use these as a value in WriteRow().
// (implementation detail: parts of the hyperlink datastructure is
// only written when closing a sheet, so they are buffered)
type Hyperlink struct {
URL, Title, Tooltip string
}
// Finish writing the spreadsheet.
func (s *StreamXLSX) Close() error {
if s.error != nil {
return s.error
}
if len(s.finishedSheets) == 0 {
// there seems to be a requirement of at least 1 sheet
if _, err := s.sheet(); err != nil {
return err
}
}
if s.openSheet != nil {
if err := s.WriteSheet(fmt.Sprintf("sheet %d", len(s.finishedSheets)+1)); err != nil {
return err
}
}
if err := s.writeWorkbook(); err != nil {
return err
}
if err := s.writeStylesheet(); err != nil {
return err
}
if err := s.writeSharedStrings(); err != nil {
return err
}
if err := s.writeWorkbookRelations(); err != nil {
return err
}
if err := s.writeContentTypes(); err != nil {
return err
}
if err := s.zip.Close(); err != nil {
return err
}
return nil
}
func (s *StreamXLSX) sheet() (*sheetEncoder, error) {
if s.openSheet != nil {
return s.openSheet, nil
}
filename := fmt.Sprintf("xl/worksheets/sheet%d.xml", len(s.finishedSheets)+1)
fh, err := s.zip.Create(filename) // no need to close!
if err != nil {
return nil, err
}
enc, err := newSheetEncoder(fh)
if err != nil {
return nil, err
}
s.openSheet = enc
return s.openSheet, nil
}
func (s *StreamXLSX) writeWorkbook() error {
filename := "xl/workbook.xml"
fh, err := s.zip.Create(filename)
if err != nil {
return err
}
return writeWorkbook(fh, s.finishedSheets)
}
func (s *StreamXLSX) writeStylesheet() error {
filename := "xl/styles.xml"
fh, err := s.zip.Create(filename)
if err != nil {
return err
}
return writeStylesheet(fh, s.Styles)
}
func (s *StreamXLSX) writeSharedStrings() error {
filename := "xl/sharedStrings.xml"
fh, err := s.zip.Create(filename)
if err != nil {
return err
}
return writeSharedStrings(fh)
}
func (s *StreamXLSX) writeRelations() error {
filename := "_rels/.rels"
fh, err := s.zip.Create(filename)
if err != nil {
return err
}
return writeRelations(fh)
}
func (s *StreamXLSX) writeWorkbookRelations() error {
filename := "xl/_rels/workbook.xml.rels"
fh, err := s.zip.Create(filename)
if err != nil {
return err
}
return writeWorkbookRelations(fh, s.finishedSheets)
}
func (s *StreamXLSX) writeSheetRelations() error {
if len(s.openSheet.relations) == 0 {
return nil
}
filename := fmt.Sprintf("xl/worksheets/_rels/sheet%d.xml.rels", len(s.finishedSheets)+1)
fh, err := s.zip.Create(filename)
if err != nil {
return err
}
return writeRelations_(fh, s.openSheet.relations)
}
func (s *StreamXLSX) writeContentTypes() error {
filename := "[Content_Types].xml"
fh, err := s.zip.Create(filename)
if err != nil {
return err
}
return writeContentTypes(fh, len(s.finishedSheets))
}