From 7363c1e3337c5f0d9c70cc8af7504b3f8c092ab4 Mon Sep 17 00:00:00 2001 From: xuri Date: Thu, 13 Oct 2022 00:02:53 +0800 Subject: Go 1.16 and later required, migration of deprecation package `ioutil` - Improving performance for stream writer `SetRow` function, reduces memory usage over and speedup about 19% - Update dependencies module - Update GitHub workflow --- stream.go | 68 +++++++++++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 49 insertions(+), 19 deletions(-) (limited to 'stream.go') diff --git a/stream.go b/stream.go index 9f47762..62470b5 100644 --- a/stream.go +++ b/stream.go @@ -16,7 +16,6 @@ import ( "encoding/xml" "fmt" "io" - "io/ioutil" "os" "reflect" "strconv" @@ -30,7 +29,7 @@ type StreamWriter struct { Sheet string SheetID int sheetWritten bool - cols string + cols strings.Builder worksheet *xlsxWorksheet rawData bufferedWriter mergeCellsCount int @@ -310,24 +309,32 @@ type RowOpts struct { } // marshalAttrs prepare attributes of the row. -func (r *RowOpts) marshalAttrs() (attrs string, err error) { +func (r *RowOpts) marshalAttrs() (strings.Builder, error) { + var ( + err error + attrs strings.Builder + ) if r == nil { - return + return attrs, err } if r.Height > MaxRowHeight { err = ErrMaxRowHeight - return + return attrs, err } if r.StyleID > 0 { - attrs += fmt.Sprintf(` s="%d" customFormat="true"`, r.StyleID) + attrs.WriteString(` s="`) + attrs.WriteString(strconv.Itoa(r.StyleID)) + attrs.WriteString(`" customFormat="1"`) } if r.Height > 0 { - attrs += fmt.Sprintf(` ht="%v" customHeight="true"`, r.Height) + attrs.WriteString(` ht="`) + attrs.WriteString(strconv.FormatFloat(r.Height, 'f', -1, 64)) + attrs.WriteString(`" customHeight="1"`) } if r.Hidden { - attrs += ` hidden="true"` + attrs.WriteString(` hidden="1"`) } - return + return attrs, err } // parseRowOpts provides a function to parse the optional settings for @@ -357,7 +364,11 @@ func (sw *StreamWriter) SetRow(cell string, values []interface{}, opts ...RowOpt if err != nil { return err } - _, _ = fmt.Fprintf(&sw.rawData, ``, row, attrs) + sw.rawData.WriteString(``) for i, val := range values { if val == nil { continue @@ -405,7 +416,14 @@ func (sw *StreamWriter) SetColWidth(min, max int, width float64) error { if min > max { min, max = max, min } - sw.cols += fmt.Sprintf(``, min, max, width) + + sw.cols.WriteString(``) return nil } @@ -515,14 +533,24 @@ func setCellIntFunc(c *xlsxC, val interface{}) (err error) { func writeCell(buf *bufferedWriter, c xlsxC) { _, _ = buf.WriteString(``) if c.F != nil { @@ -549,8 +577,10 @@ func writeCell(buf *bufferedWriter, c xlsxC) { func (sw *StreamWriter) writeSheetData() { if !sw.sheetWritten { bulkAppendFields(&sw.rawData, sw.worksheet, 4, 5) - if len(sw.cols) > 0 { - _, _ = sw.rawData.WriteString("" + sw.cols + "") + if sw.cols.Len() > 0 { + _, _ = sw.rawData.WriteString("") + _, _ = sw.rawData.WriteString(sw.cols.String()) + _, _ = sw.rawData.WriteString("") } _, _ = sw.rawData.WriteString(``) sw.sheetWritten = true @@ -642,7 +672,7 @@ func (bw *bufferedWriter) Sync() (err error) { return nil } if bw.tmp == nil { - bw.tmp, err = ioutil.TempFile(os.TempDir(), "excelize-") + bw.tmp, err = os.CreateTemp(os.TempDir(), "excelize-") if err != nil { // can not use local storage return nil -- cgit v1.2.1