forked from tealeg/xlsx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
file.go
277 lines (246 loc) · 7.05 KB
/
file.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
package xlsx
import (
"archive/zip"
"encoding/xml"
"fmt"
"io"
"os"
"strconv"
"strings"
)
// File is a high level structure providing a slice of Sheet structs
// to the user.
type File struct {
worksheets map[string]*zip.File
referenceTable *RefTable
Date1904 bool
styles *xlsxStyleSheet
Sheets []*Sheet
Sheet map[string]*Sheet
theme *theme
}
// Create a new File
func NewFile() (file *File) {
file = &File{}
file.Sheet = make(map[string]*Sheet)
file.Sheets = make([]*Sheet, 0)
return
}
// OpenFile() take the name of an XLSX file and returns a populated
// xlsx.File struct for it.
func OpenFile(filename string) (file *File, err error) {
var f *zip.ReadCloser
f, err = zip.OpenReader(filename)
if err != nil {
return nil, err
}
file, err = ReadZip(f)
return
}
// A convenient wrapper around File.ToSlice, FileToSlice will
// return the raw data contained in an Excel XLSX file as three
// dimensional slice. The first index represents the sheet number,
// the second the row number, and the third the cell number.
//
// For example:
//
// var mySlice [][][]string
// var value string
// mySlice = xlsx.FileToSlice("myXLSX.xlsx")
// value = mySlice[0][0][0]
//
// Here, value would be set to the raw value of the cell A1 in the
// first sheet in the XLSX file.
func FileToSlice(path string) ([][][]string, error) {
f, err := OpenFile(path)
if err != nil {
return nil, err
}
return f.ToSlice()
}
// Save the File to an xlsx file at the provided path.
func (f *File) Save(path string) (err error) {
var target *os.File
target, err = os.Create(path)
if err != nil {
return
}
err = f.Write(target)
if err != nil {
return
}
return target.Close()
}
// Write the File to io.Writer as xlsx
func (f *File) Write(writer io.Writer) (err error) {
var parts map[string]string
var zipWriter *zip.Writer
parts, err = f.MarshallParts()
if err != nil {
return
}
zipWriter = zip.NewWriter(writer)
for partName, part := range parts {
var writer io.Writer
writer, err = zipWriter.Create(partName)
if err != nil {
return
}
_, err = writer.Write([]byte(part))
if err != nil {
return
}
}
err = zipWriter.Close()
return
}
// Add a new Sheet, with the provided name, to a File
func (f *File) AddSheet(sheetName string) (sheet *Sheet) {
sheet = &Sheet{Name: sheetName, File: f}
f.Sheet[sheetName] = sheet
f.Sheets = append(f.Sheets, sheet)
return sheet
}
func (f *File) makeWorkbook() xlsxWorkbook {
var workbook xlsxWorkbook
workbook = xlsxWorkbook{}
workbook.FileVersion = xlsxFileVersion{}
workbook.FileVersion.AppName = "Go XLSX"
workbook.WorkbookPr = xlsxWorkbookPr{
BackupFile: false,
ShowObjects: "all"}
workbook.BookViews = xlsxBookViews{}
workbook.BookViews.WorkBookView = make([]xlsxWorkBookView, 1)
workbook.BookViews.WorkBookView[0] = xlsxWorkBookView{
ActiveTab: 0,
FirstSheet: 0,
ShowHorizontalScroll: true,
ShowSheetTabs: true,
ShowVerticalScroll: true,
TabRatio: 204,
WindowHeight: 8192,
WindowWidth: 16384,
XWindow: "0",
YWindow: "0"}
workbook.Sheets = xlsxSheets{}
workbook.Sheets.Sheet = make([]xlsxSheet, len(f.Sheets))
workbook.CalcPr.IterateCount = 100
workbook.CalcPr.RefMode = "A1"
workbook.CalcPr.Iterate = false
workbook.CalcPr.IterateDelta = 0.001
return workbook
}
// Construct a map of file name to XML content representing the file
// in terms of the structure of an XLSX file.
func (f *File) MarshallParts() (map[string]string, error) {
var parts map[string]string
var refTable *RefTable = NewSharedStringRefTable()
refTable.isWrite = true
var workbookRels WorkBookRels = make(WorkBookRels)
var err error
var workbook xlsxWorkbook
var types xlsxTypes = MakeDefaultContentTypes()
marshal := func(thing interface{}) (string, error) {
body, err := xml.Marshal(thing)
if err != nil {
return "", err
}
return xml.Header + string(body), nil
}
parts = make(map[string]string)
workbook = f.makeWorkbook()
sheetIndex := 1
if f.styles == nil {
f.styles = newXlsxStyleSheet(f.theme)
}
f.styles.reset()
for _, sheet := range f.Sheets {
xSheet := sheet.makeXLSXSheet(refTable, f.styles)
rId := fmt.Sprintf("rId%d", sheetIndex)
sheetId := strconv.Itoa(sheetIndex)
sheetPath := fmt.Sprintf("worksheets/sheet%d.xml", sheetIndex)
partName := "xl/" + sheetPath
types.Overrides = append(
types.Overrides,
xlsxOverride{
PartName: "/" + partName,
ContentType: "application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml"})
workbookRels[rId] = sheetPath
workbook.Sheets.Sheet[sheetIndex-1] = xlsxSheet{
Name: sheet.Name,
SheetId: sheetId,
Id: rId,
State: "visible"}
parts[partName], err = marshal(xSheet)
if err != nil {
return parts, err
}
sheetIndex++
}
parts["xl/workbook.xml"], err = marshal(workbook)
if err != nil {
return parts, err
}
// Make it work with Mac Numbers.
// Dirty hack to fix issues #63 and #91; encoding/xml currently
// "doesn't allow for additional namespaces to be defined in the root element of the document,"
// as described by @tealeg in the comments for #63.
oldXmlns := `<workbook xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">`
newXmlns := `<workbook xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships">`
parts["xl/workbook.xml"] = strings.Replace(parts["xl/workbook.xml"], oldXmlns, newXmlns, 1)
parts["_rels/.rels"] = TEMPLATE__RELS_DOT_RELS
parts["docProps/app.xml"] = TEMPLATE_DOCPROPS_APP
// TODO - do this properly, modification and revision information
parts["docProps/core.xml"] = TEMPLATE_DOCPROPS_CORE
parts["xl/theme/theme1.xml"] = TEMPLATE_XL_THEME_THEME
xSST := refTable.makeXLSXSST()
parts["xl/sharedStrings.xml"], err = marshal(xSST)
if err != nil {
return parts, err
}
xWRel := workbookRels.MakeXLSXWorkbookRels()
parts["xl/_rels/workbook.xml.rels"], err = marshal(xWRel)
if err != nil {
return parts, err
}
parts["[Content_Types].xml"], err = marshal(types)
if err != nil {
return parts, err
}
parts["xl/styles.xml"], err = f.styles.Marshal()
if err != nil {
return parts, err
}
return parts, nil
}
// Return the raw data contained in the File as three
// dimensional slice. The first index represents the sheet number,
// the second the row number, and the third the cell number.
//
// For example:
//
// var mySlice [][][]string
// var value string
// mySlice = xlsx.FileToSlice("myXLSX.xlsx")
// value = mySlice[0][0][0]
//
// Here, value would be set to the raw value of the cell A1 in the
// first sheet in the XLSX file.
func (file *File) ToSlice() (output [][][]string, err error) {
output = [][][]string{}
for _, sheet := range file.Sheets {
s := [][]string{}
for _, row := range sheet.Rows {
if row == nil {
continue
}
r := []string{}
for _, cell := range row.Cells {
r = append(r, cell.String())
}
s = append(s, r)
}
output = append(output, s)
}
return output, nil
}