Skip to content

feat: add UseSharedStrings option for streaming writes (D1)#2326

Open
AdamDrewsTR wants to merge 2 commits into
qax-os:masterfrom
AdamDrewsTR:feat/shared-strings-streaming
Open

feat: add UseSharedStrings option for streaming writes (D1)#2326
AdamDrewsTR wants to merge 2 commits into
qax-os:masterfrom
AdamDrewsTR:feat/shared-strings-streaming

Conversation

@AdamDrewsTR
Copy link
Copy Markdown
Contributor

Summary

Add UseSharedStrings option for streaming writes. When enabled, the stream writer stores unique string values once in xl/sharedStrings.xml and references them by index in cell elements (t="s" with <v>idx</v>), instead of writing inline strings for every cell.

This can significantly reduce output file size when many cells contain duplicate string values.

Implementation

  • Add UseSharedStrings bool to Options struct
  • Add useSharedStrings, sharedStrings, sharedStringsMap fields to StreamWriter; initialized from Options in NewStreamWriter
  • After setCellValFunc produces an inlineStr cell with a simple <t> value, convert it to a shared string reference (t="s")
  • getOrAddSharedString: dedup via map, returns shared string index
  • writeSharedStrings: on Flush, writes xl/sharedStrings.xml with proper xml:space="preserve" for whitespace-padded strings, adds content type part and workbook relationship if not already present

Usage

f := excelize.NewFile()
f.SetOptions(excelize.Options{UseSharedStrings: true})
sw, err := f.NewStreamWriter("Sheet1")
// ... write rows as usual ...
sw.Flush()
f.SaveAs("output.xlsx")

Add UseSharedStrings bool to Options struct. When enabled, the stream
writer stores unique string values once in xl/sharedStrings.xml and
references them by index in cell elements (t="s" with <v>idx</v>),
instead of writing inline strings for every cell.

This can significantly reduce output file size when many cells contain
duplicate string values.

Implementation:
- Add useSharedStrings, sharedStrings, sharedStringsMap fields to
  StreamWriter; initialized from Options in NewStreamWriter
- After setCellValFunc produces an inlineStr cell with a simple <t>
  value, convert it to a shared string reference (t="s")
- getOrAddSharedString: dedup via map, returns shared string index
- writeSharedStrings: on Flush, writes xl/sharedStrings.xml with proper
  xml:space="preserve" for whitespace, adds content type part and
  workbook relationship if not already present
@AdamDrewsTR AdamDrewsTR changed the title feat: add UseSharedStrings option for streaming writes feat: add UseSharedStrings option for streaming writes (D1) May 12, 2026
@codecov
Copy link
Copy Markdown

codecov Bot commented May 12, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.60%. Comparing base (4bebb61) to head (bfedfc5).

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #2326   +/-   ##
=======================================
  Coverage   99.60%   99.60%           
=======================================
  Files          32       32           
  Lines       26791    26838   +47     
=======================================
+ Hits        26685    26732   +47     
  Misses         55       55           
  Partials       51       51           
Flag Coverage Δ
unittests 99.60% <100.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@xuri xuri added the size/M Denotes a PR that changes 30-99 lines, ignoring generated files. label May 13, 2026
Cover all diff lines including getOrAddSharedString deduplication,
writeSharedStrings XML generation with xml:space="preserve",
content type/relationship registration, and error paths.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size/M Denotes a PR that changes 30-99 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants