From 0833a9d5dab846ed01be52fa59c97ede36ee4a93 Mon Sep 17 00:00:00 2001 From: Ri Xu Date: Sun, 12 Feb 2017 19:03:24 +0800 Subject: - Improved performance when reading large files, call Token to read tokens one by one instead Unmarshal. Related issue #20 ; - Fix go test typo; - Update README --- excelize.go | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) (limited to 'excelize.go') diff --git a/excelize.go b/excelize.go index ed36589..bc37c0a 100644 --- a/excelize.go +++ b/excelize.go @@ -10,6 +10,7 @@ import ( // File define a populated XLSX file struct. type File struct { + checked map[string]bool XLSX map[string]string Path string SheetCount int @@ -21,6 +22,7 @@ func OpenFile(filename string) (*File, error) { var f *zip.ReadCloser var err error file := make(map[string]string) + c := make(map[string]bool) sheetCount := 0 f, err = zip.OpenReader(filename) if err != nil { @@ -28,6 +30,7 @@ func OpenFile(filename string) (*File, error) { } file, sheetCount, _ = ReadZip(f) return &File{ + checked: c, XLSX: file, Path: filename, SheetCount: sheetCount, @@ -66,6 +69,15 @@ func (f *File) SetCellInt(sheet string, axis string, value int) { var xlsx xlsxWorksheet name := "xl/worksheets/" + strings.ToLower(sheet) + ".xml" xml.Unmarshal([]byte(f.readXML(name)), &xlsx) + if f.checked == nil { + f.checked = make(map[string]bool) + } + ok := f.checked[name] + if !ok { + xlsx = checkRow(xlsx) + f.checked[name] = true + } + if xlsx.MergeCells != nil { for i := 0; i < len(xlsx.MergeCells.Cells); i++ { if checkCellInArea(axis, xlsx.MergeCells.Cells[i].Ref) { @@ -98,6 +110,14 @@ func (f *File) SetCellStr(sheet string, axis string, value string) { var xlsx xlsxWorksheet name := "xl/worksheets/" + strings.ToLower(sheet) + ".xml" xml.Unmarshal([]byte(f.readXML(name)), &xlsx) + if f.checked == nil { + f.checked = make(map[string]bool) + } + ok := f.checked[name] + if !ok { + xlsx = checkRow(xlsx) + f.checked[name] = true + } if xlsx.MergeCells != nil { for i := 0; i < len(xlsx.MergeCells.Cells); i++ { if checkCellInArea(axis, xlsx.MergeCells.Cells[i].Ref) { @@ -133,6 +153,14 @@ func (f *File) SetCellDefault(sheet string, axis string, value string) { var xlsx xlsxWorksheet name := "xl/worksheets/" + strings.ToLower(sheet) + ".xml" xml.Unmarshal([]byte(f.readXML(name)), &xlsx) + if f.checked == nil { + f.checked = make(map[string]bool) + } + ok := f.checked[name] + if !ok { + xlsx = checkRow(xlsx) + f.checked[name] = true + } if xlsx.MergeCells != nil { for i := 0; i < len(xlsx.MergeCells.Cells); i++ { if checkCellInArea(axis, xlsx.MergeCells.Cells[i].Ref) { -- cgit v1.2.1