From 8fd061b98f660f5b67380bda13a5424f3a8164c1 Mon Sep 17 00:00:00 2001 From: Ri Xu Date: Sat, 1 Apr 2017 13:56:39 +0800 Subject: [PATCH] - Fix SheetCount count error; - Optimize deserialization operations; - README updated, add go version required notice --- README.md | 2 +- excelize.go | 18 ++++--- file.go | 17 ++----- picture.go | 12 +---- sheet.go | 137 +++++++++++++++++++++++++++++++--------------------- 5 files changed, 100 insertions(+), 86 deletions(-) diff --git a/README.md b/README.md index 351f46e..6ac668e 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,7 @@ ## Introduction -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). +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). ## Basic Usage diff --git a/excelize.go b/excelize.go index e53d9f0..39adb87 100644 --- a/excelize.go +++ b/excelize.go @@ -13,11 +13,14 @@ import ( // File define a populated XLSX file struct. type File struct { - checked map[string]bool - XLSX map[string]string - Path string - Sheet map[string]*xlsxWorksheet - SheetCount int + checked map[string]bool + ContentTypes *xlsxTypes + Path string + Sheet map[string]*xlsxWorksheet + SheetCount int + WorkBook *xlsxWorkbook + WorkBookRels *xlsxWorkbookRels + XLSX map[string]string } // 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) { return nil, err } return &File{ - Sheet: make(map[string]*xlsxWorksheet), checked: make(map[string]bool), - XLSX: file, - Path: "", + Sheet: make(map[string]*xlsxWorksheet), SheetCount: sheetCount, + XLSX: file, }, nil } diff --git a/file.go b/file.go index aea7f8e..d0f1c51 100644 --- a/file.go +++ b/file.go @@ -3,7 +3,6 @@ package excelize import ( "archive/zip" "bytes" - "encoding/xml" "fmt" "io" "os" @@ -26,8 +25,8 @@ func CreateFile() *File { file["xl/workbook.xml"] = templateWorkbook file["[Content_Types].xml"] = templateContentTypes return &File{ - XLSX: file, Sheet: make(map[string]*xlsxWorksheet), + XLSX: file, } } @@ -54,16 +53,10 @@ func (f *File) WriteTo(name string) error { func (f *File) Write(w io.Writer) error { buf := new(bytes.Buffer) zw := zip.NewWriter(buf) - for path, sheet := range f.Sheet { - if sheet == nil { - continue - } - output, err := xml.Marshal(sheet) - if err != nil { - return err - } - f.saveFileList(path, replaceWorkSheetsRelationshipsNameSpace(string(output))) - } + f.contentTypesWriter() + f.workbookWriter() + f.workbookRelsWriter() + f.worksheetWriter() for path, content := range f.XLSX { fi, err := zw.Create(path) if err != nil { diff --git a/picture.go b/picture.go index e7951dd..3df8e98 100644 --- a/picture.go +++ b/picture.go @@ -303,8 +303,7 @@ func (f *File) addMedia(file string, ext string) { // for relationship parts and the Main Document part. func (f *File) setContentTypePartImageExtensions() { var imageTypes = map[string]bool{"jpeg": false, "png": false, "gif": false} - var content xlsxTypes - xml.Unmarshal([]byte(f.readXML("[Content_Types].xml")), &content) + content := f.contentTypesReader() for _, v := range content.Defaults { _, ok := imageTypes[v.Extension] if ok { @@ -319,8 +318,6 @@ func (f *File) setContentTypePartImageExtensions() { }) } } - output, _ := xml.Marshal(content) - f.saveFileList("[Content_Types].xml", string(output)) } // addDrawingContentTypePart provides function to add image part relationships @@ -328,12 +325,9 @@ func (f *File) setContentTypePartImageExtensions() { // appropriate content type. func (f *File) addDrawingContentTypePart(index int) { f.setContentTypePartImageExtensions() - var content xlsxTypes - xml.Unmarshal([]byte(f.readXML("[Content_Types].xml")), &content) + content := f.contentTypesReader() for _, v := range content.Overrides { if v.PartName == "/xl/drawings/drawing"+strconv.Itoa(index)+".xml" { - output, _ := xml.Marshal(content) - f.saveFileList(`[Content_Types].xml`, string(output)) return } } @@ -341,8 +335,6 @@ func (f *File) addDrawingContentTypePart(index int) { PartName: "/xl/drawings/drawing" + strconv.Itoa(index) + ".xml", ContentType: "application/vnd.openxmlformats-officedocument.drawing+xml", }) - output, _ := xml.Marshal(content) - f.saveFileList("[Content_Types].xml", string(output)) } // getSheetRelationshipsTargetByID provides function to get Target attribute diff --git a/sheet.go b/sheet.go index aa653f8..0c16295 100644 --- a/sheet.go +++ b/sheet.go @@ -4,7 +4,6 @@ import ( "bytes" "encoding/xml" "errors" - "fmt" "os" "path" "strconv" @@ -25,21 +24,67 @@ func (f *File) NewSheet(index int, name string) { rID := f.addXlsxWorkbookRels(index) // Update xl/workbook.xml f.setWorkbook(name, rID) + f.SheetCount++ +} + +// contentTypesReader provides function to get the pointer to the +// [Content_Types].xml structure after deserialization. +func (f *File) contentTypesReader() *xlsxTypes { + if f.ContentTypes == nil { + var content xlsxTypes + xml.Unmarshal([]byte(f.readXML("[Content_Types].xml")), &content) + f.ContentTypes = &content + } + return f.ContentTypes +} + +// contentTypesWriter provides function to save [Content_Types].xml after +// serialize structure. +func (f *File) contentTypesWriter() { + if f.ContentTypes != nil { + output, _ := xml.Marshal(f.ContentTypes) + f.saveFileList("[Content_Types].xml", string(output)) + } +} + +// workbookReader provides function to get the pointer to the xl/workbook.xml +// structure after deserialization. +func (f *File) workbookReader() *xlsxWorkbook { + if f.WorkBook == nil { + var content xlsxWorkbook + xml.Unmarshal([]byte(f.readXML("xl/workbook.xml")), &content) + f.WorkBook = &content + } + return f.WorkBook +} + +// workbookWriter provides function to save xl/workbook.xml after serialize +// structure. +func (f *File) workbookWriter() { + if f.WorkBook != nil { + output, _ := xml.Marshal(f.WorkBook) + f.saveFileList("xl/workbook.xml", replaceRelationshipsNameSpace(string(output))) + } +} + +// worksheetWriter provides function to save xl/worksheets/sheet%d.xml after +// serialize structure. +func (f *File) worksheetWriter() { + for path, sheet := range f.Sheet { + if sheet != nil { + output, _ := xml.Marshal(sheet) + f.saveFileList(path, replaceWorkSheetsRelationshipsNameSpace(string(output))) + } + } } // Read and update property of contents type of XLSX. func (f *File) setContentTypes(index int) { - var content xlsxTypes - xml.Unmarshal([]byte(f.readXML("[Content_Types].xml")), &content) + content := f.contentTypesReader() content.Overrides = append(content.Overrides, xlsxOverride{ PartName: "/xl/worksheets/sheet" + strconv.Itoa(index) + ".xml", ContentType: "application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml", }) - output, err := xml.Marshal(content) - if err != nil { - fmt.Println(err) - } - f.saveFileList("[Content_Types].xml", string(output)) } // Update sheet property by given index. @@ -56,35 +101,42 @@ func (f *File) setSheet(index int) { // setWorkbook update workbook property of XLSX. Maximum 31 characters are // allowed in sheet title. func (f *File) setWorkbook(name string, rid int) { - var content xlsxWorkbook r := strings.NewReplacer(":", "", "\\", "", "/", "", "?", "", "*", "", "[", "", "]", "") name = r.Replace(name) if len(name) > 31 { name = name[0:31] } - xml.Unmarshal([]byte(f.readXML("xl/workbook.xml")), &content) + content := f.workbookReader() content.Sheets.Sheet = append(content.Sheets.Sheet, xlsxSheet{ Name: name, SheetID: strconv.Itoa(rid), ID: "rId" + strconv.Itoa(rid), }) - output, err := xml.Marshal(content) - if err != nil { - fmt.Println(err) - } - f.saveFileList("xl/workbook.xml", replaceRelationshipsNameSpace(string(output))) } -// readXlsxWorkbookRels read and unmarshal workbook relationships of XLSX file. -func (f *File) readXlsxWorkbookRels() xlsxWorkbookRels { - var content xlsxWorkbookRels - xml.Unmarshal([]byte(f.readXML("xl/_rels/workbook.xml.rels")), &content) - return content +// workbookRelsReader provides function to read and unmarshal workbook +// relationships of XLSX file. +func (f *File) workbookRelsReader() *xlsxWorkbookRels { + if f.WorkBookRels == nil { + var content xlsxWorkbookRels + xml.Unmarshal([]byte(f.readXML("xl/_rels/workbook.xml.rels")), &content) + f.WorkBookRels = &content + } + return f.WorkBookRels +} + +// workbookRelsWriter provides function to save xl/_rels/workbook.xml.rels after +// serialize structure. +func (f *File) workbookRelsWriter() { + if f.WorkBookRels != nil { + output, _ := xml.Marshal(f.WorkBookRels) + f.saveFileList("xl/_rels/workbook.xml.rels", string(output)) + } } // addXlsxWorkbookRels update workbook relationships property of XLSX. func (f *File) addXlsxWorkbookRels(sheet int) int { - content := f.readXlsxWorkbookRels() + content := f.workbookRelsReader() rID := 0 for _, v := range content.Relationships { t, _ := strconv.Atoi(strings.TrimPrefix(v.ID, "rId")) @@ -105,11 +157,6 @@ func (f *File) addXlsxWorkbookRels(sheet int) int { Target: target.String(), Type: SourceRelationshipWorkSheet, }) - output, err := xml.Marshal(content) - if err != nil { - fmt.Println(err) - } - f.saveFileList("xl/_rels/workbook.xml.rels", string(output)) return rID } @@ -134,12 +181,11 @@ func replaceRelationshipsNameSpace(workbookMarshal string) string { // SetActiveSheet provides function to set default active sheet of XLSX by given // index. func (f *File) SetActiveSheet(index int) { - var content xlsxWorkbook if index < 1 { index = 1 } index-- - xml.Unmarshal([]byte(f.readXML("xl/workbook.xml")), &content) + content := f.workbookReader() if len(content.BookViews.WorkBookView) > 0 { content.BookViews.WorkBookView[0].ActiveTab = index } else { @@ -148,11 +194,6 @@ func (f *File) SetActiveSheet(index int) { }) } sheets := len(content.Sheets.Sheet) - output, err := xml.Marshal(content) - if err != nil { - fmt.Println(err) - } - f.saveFileList("xl/workbook.xml", replaceRelationshipsNameSpace(string(output))) index++ for i := 0; i < sheets; i++ { sheetIndex := i + 1 @@ -177,9 +218,8 @@ func (f *File) SetActiveSheet(index int) { // GetActiveSheetIndex provides function to get active sheet of XLSX. If not // found the active sheet will be return integer 0. func (f *File) GetActiveSheetIndex() int { - content := xlsxWorkbook{} buffer := bytes.Buffer{} - xml.Unmarshal([]byte(f.readXML("xl/workbook.xml")), &content) + content := f.workbookReader() for _, v := range content.Sheets.Sheet { xlsx := xlsxWorksheet{} buffer.WriteString("xl/worksheets/sheet") @@ -203,27 +243,23 @@ func (f *File) GetActiveSheetIndex() int { // name in the formula or reference associated with the cell. So there may be // problem formula error or reference missing. func (f *File) SetSheetName(oldName, newName string) { - var content = xlsxWorkbook{} r := strings.NewReplacer(":", "", "\\", "", "/", "", "?", "", "*", "", "[", "", "]", "") newName = r.Replace(newName) if len(newName) > 31 { newName = newName[0:31] } - xml.Unmarshal([]byte(f.readXML("xl/workbook.xml")), &content) + content := f.workbookReader() for k, v := range content.Sheets.Sheet { if v.Name == oldName { content.Sheets.Sheet[k].Name = newName } } - output, _ := xml.Marshal(content) - f.saveFileList("xl/workbook.xml", replaceRelationshipsNameSpace(string(output))) } // GetSheetName provides function to get sheet name of XLSX by given sheet // index. If given sheet index is invalid, will return an empty string. func (f *File) GetSheetName(index int) string { - var content = xlsxWorkbook{} - xml.Unmarshal([]byte(f.readXML("xl/workbook.xml")), &content) + content := f.workbookReader() for _, v := range content.Sheets.Sheet { if v.ID == "rId"+strconv.Itoa(index) { return v.Name @@ -244,9 +280,8 @@ func (f *File) GetSheetName(index int) string { // } // func (f *File) GetSheetMap() map[int]string { - content := xlsxWorkbook{} + content := f.workbookReader() sheetMap := map[int]string{} - xml.Unmarshal([]byte(f.readXML("xl/workbook.xml")), &content) for _, v := range content.Sheets.Sheet { id, _ := strconv.Atoi(strings.TrimPrefix(v.ID, "rId")) sheetMap[id] = v.Name @@ -280,15 +315,12 @@ func (f *File) SetSheetBackground(sheet, picture string) error { // value of the deleted worksheet, it will cause a file error when you open it. // This function will be invalid when only the one worksheet is left. func (f *File) DeleteSheet(name string) { - var content xlsxWorkbook - xml.Unmarshal([]byte(f.readXML("xl/workbook.xml")), &content) + content := f.workbookReader() for k, v := range content.Sheets.Sheet { if v.Name != name || len(content.Sheets.Sheet) < 2 { continue } content.Sheets.Sheet = append(content.Sheets.Sheet[:k], content.Sheets.Sheet[k+1:]...) - output, _ := xml.Marshal(content) - f.saveFileList("xl/workbook.xml", replaceRelationshipsNameSpace(string(output))) sheet := "xl/worksheets/sheet" + strings.TrimPrefix(v.ID, "rId") + ".xml" rels := "xl/worksheets/_rels/sheet" + strings.TrimPrefix(v.ID, "rId") + ".xml.rels" target := f.deteleSheetFromWorkbookRels(v.ID) @@ -305,6 +337,7 @@ func (f *File) DeleteSheet(name string) { if ok { delete(f.Sheet, sheet) } + f.SheetCount-- } } @@ -312,15 +345,12 @@ func (f *File) DeleteSheet(name string) { // relationships by given relationships ID in the file // xl/_rels/workbook.xml.rels. func (f *File) deteleSheetFromWorkbookRels(rID string) string { - var content xlsxWorkbookRels - xml.Unmarshal([]byte(f.readXML("xl/_rels/workbook.xml.rels")), &content) + content := f.workbookRelsReader() for k, v := range content.Relationships { if v.ID != rID { continue } content.Relationships = append(content.Relationships[:k], content.Relationships[k+1:]...) - output, _ := xml.Marshal(content) - f.saveFileList("xl/_rels/workbook.xml.rels", string(output)) return v.Target } return "" @@ -329,14 +359,11 @@ func (f *File) deteleSheetFromWorkbookRels(rID string) string { // deteleSheetFromContentTypes provides function to remove worksheet // relationships by given target name in the file [Content_Types].xml. func (f *File) deteleSheetFromContentTypes(target string) { - var content xlsxTypes - xml.Unmarshal([]byte(f.readXML("[Content_Types].xml")), &content) + content := f.contentTypesReader() for k, v := range content.Overrides { if v.PartName != "/xl/"+target { continue } content.Overrides = append(content.Overrides[:k], content.Overrides[k+1:]...) - output, _ := xml.Marshal(content) - f.saveFileList("[Content_Types].xml", string(output)) } }