Skip to content

Commit

Permalink
Fix cpu usage problem of stream writer when merging cells (qax-os#1359)
Browse files Browse the repository at this point in the history
Co-authored-by: zzt <[email protected]>
  • Loading branch information
2 people authored and xuri committed Oct 9, 2022
1 parent 36dd0bb commit d373f5a
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ type StreamWriter struct {
worksheet *xlsxWorksheet
rawData bufferedWriter
mergeCellsCount int
mergeCells string
mergeCells strings.Builder
tableParts string
}

Expand Down Expand Up @@ -417,7 +417,11 @@ func (sw *StreamWriter) MergeCell(hCell, vCell string) error {
return err
}
sw.mergeCellsCount++
sw.mergeCells += fmt.Sprintf(`<mergeCell ref="%s:%s"/>`, hCell, vCell)
_, _ = sw.mergeCells.WriteString(`<mergeCell ref="`)
_, _ = sw.mergeCells.WriteString(hCell)
_, _ = sw.mergeCells.WriteString(`:`)
_, _ = sw.mergeCells.WriteString(vCell)
_, _ = sw.mergeCells.WriteString(`"/>`)
return nil
}

Expand Down Expand Up @@ -548,10 +552,15 @@ func (sw *StreamWriter) Flush() error {
}
_, _ = sw.rawData.WriteString(`</sheetData>`)
bulkAppendFields(&sw.rawData, sw.worksheet, 8, 15)
mergeCells := strings.Builder{}
if sw.mergeCellsCount > 0 {
sw.mergeCells = fmt.Sprintf(`<mergeCells count="%d">%s</mergeCells>`, sw.mergeCellsCount, sw.mergeCells)
_, _ = mergeCells.WriteString(`<mergeCells count="`)
_, _ = mergeCells.WriteString(strconv.Itoa(sw.mergeCellsCount))
_, _ = mergeCells.WriteString(`">`)
_, _ = mergeCells.WriteString(sw.mergeCells.String())
_, _ = mergeCells.WriteString(`</mergeCells>`)
}
_, _ = sw.rawData.WriteString(sw.mergeCells)
_, _ = sw.rawData.WriteString(mergeCells.String())
bulkAppendFields(&sw.rawData, sw.worksheet, 17, 38)
_, _ = sw.rawData.WriteString(sw.tableParts)
bulkAppendFields(&sw.rawData, sw.worksheet, 40, 40)
Expand Down

0 comments on commit d373f5a

Please sign in to comment.