Skip to content

Commit 8fd061b

Browse files
committed
- Fix SheetCount count error;
- Optimize deserialization operations; - README updated, add go version required notice
1 parent 330c7a0 commit 8fd061b

File tree

5 files changed

+99
-85
lines changed

5 files changed

+99
-85
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
## Introduction
1313

14-
Excelize is a library written in pure Golang and providing a set of functions that allow you to write to and read from XLSX files. Support reads and writes XLSX file generated by Office Excel 2007 and later. Support save file without losing original charts of XLSX. The full API docs can be seen using go's built-in documentation tool, or online at [godoc.org](https://godoc.org/github.com/Luxurioust/excelize).
14+
Excelize is a library written in pure Golang and providing a set of functions that allow you to write to and read from XLSX files. Support reads and writes XLSX file generated by Office Excel 2007 and later. Support save file without losing original charts of XLSX. This library needs Go version 1.8 or later. The full API docs can be seen using go's built-in documentation tool, or online at [godoc.org](https://godoc.org/github.com/Luxurioust/excelize).
1515

1616
## Basic Usage
1717

excelize.go

+10-8
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,14 @@ import (
1313

1414
// File define a populated XLSX file struct.
1515
type File struct {
16-
checked map[string]bool
17-
XLSX map[string]string
18-
Path string
19-
Sheet map[string]*xlsxWorksheet
20-
SheetCount int
16+
checked map[string]bool
17+
ContentTypes *xlsxTypes
18+
Path string
19+
Sheet map[string]*xlsxWorksheet
20+
SheetCount int
21+
WorkBook *xlsxWorkbook
22+
WorkBookRels *xlsxWorkbookRels
23+
XLSX map[string]string
2124
}
2225

2326
// OpenFile take the name of an XLSX file and returns a populated XLSX file
@@ -53,11 +56,10 @@ func OpenReader(r io.Reader) (*File, error) {
5356
return nil, err
5457
}
5558
return &File{
56-
Sheet: make(map[string]*xlsxWorksheet),
5759
checked: make(map[string]bool),
58-
XLSX: file,
59-
Path: "",
60+
Sheet: make(map[string]*xlsxWorksheet),
6061
SheetCount: sheetCount,
62+
XLSX: file,
6163
}, nil
6264
}
6365

file.go

+5-12
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package excelize
33
import (
44
"archive/zip"
55
"bytes"
6-
"encoding/xml"
76
"fmt"
87
"io"
98
"os"
@@ -26,8 +25,8 @@ func CreateFile() *File {
2625
file["xl/workbook.xml"] = templateWorkbook
2726
file["[Content_Types].xml"] = templateContentTypes
2827
return &File{
29-
XLSX: file,
3028
Sheet: make(map[string]*xlsxWorksheet),
29+
XLSX: file,
3130
}
3231
}
3332

@@ -54,16 +53,10 @@ func (f *File) WriteTo(name string) error {
5453
func (f *File) Write(w io.Writer) error {
5554
buf := new(bytes.Buffer)
5655
zw := zip.NewWriter(buf)
57-
for path, sheet := range f.Sheet {
58-
if sheet == nil {
59-
continue
60-
}
61-
output, err := xml.Marshal(sheet)
62-
if err != nil {
63-
return err
64-
}
65-
f.saveFileList(path, replaceWorkSheetsRelationshipsNameSpace(string(output)))
66-
}
56+
f.contentTypesWriter()
57+
f.workbookWriter()
58+
f.workbookRelsWriter()
59+
f.worksheetWriter()
6760
for path, content := range f.XLSX {
6861
fi, err := zw.Create(path)
6962
if err != nil {

picture.go

+2-10
Original file line numberDiff line numberDiff line change
@@ -303,8 +303,7 @@ func (f *File) addMedia(file string, ext string) {
303303
// for relationship parts and the Main Document part.
304304
func (f *File) setContentTypePartImageExtensions() {
305305
var imageTypes = map[string]bool{"jpeg": false, "png": false, "gif": false}
306-
var content xlsxTypes
307-
xml.Unmarshal([]byte(f.readXML("[Content_Types].xml")), &content)
306+
content := f.contentTypesReader()
308307
for _, v := range content.Defaults {
309308
_, ok := imageTypes[v.Extension]
310309
if ok {
@@ -319,30 +318,23 @@ func (f *File) setContentTypePartImageExtensions() {
319318
})
320319
}
321320
}
322-
output, _ := xml.Marshal(content)
323-
f.saveFileList("[Content_Types].xml", string(output))
324321
}
325322

326323
// addDrawingContentTypePart provides function to add image part relationships
327324
// in http://purl.oclc.org/ooxml/officeDocument/relationships/image and
328325
// appropriate content type.
329326
func (f *File) addDrawingContentTypePart(index int) {
330327
f.setContentTypePartImageExtensions()
331-
var content xlsxTypes
332-
xml.Unmarshal([]byte(f.readXML("[Content_Types].xml")), &content)
328+
content := f.contentTypesReader()
333329
for _, v := range content.Overrides {
334330
if v.PartName == "/xl/drawings/drawing"+strconv.Itoa(index)+".xml" {
335-
output, _ := xml.Marshal(content)
336-
f.saveFileList(`[Content_Types].xml`, string(output))
337331
return
338332
}
339333
}
340334
content.Overrides = append(content.Overrides, xlsxOverride{
341335
PartName: "/xl/drawings/drawing" + strconv.Itoa(index) + ".xml",
342336
ContentType: "application/vnd.openxmlformats-officedocument.drawing+xml",
343337
})
344-
output, _ := xml.Marshal(content)
345-
f.saveFileList("[Content_Types].xml", string(output))
346338
}
347339

348340
// getSheetRelationshipsTargetByID provides function to get Target attribute

0 commit comments

Comments
 (0)