Writing 100,000-row Excel files in .NET with 27 MB allocated
说明与用途
SheetStream 以仅 27 MB 内存和 534 ms 的速度流式导出 100,000 行 Excel,优于其他占用数百 MB 的 .NET 库。
问题
Most .NET Excel libraries build the whole workbook in memory, causing hundreds of megabytes to gigabytes of allocation for a 100,000‑row export.
结果
SheetStream writes 100,000 rows × 10 formatted columns in 534 ms using only 27 MB of memory, compared to 343 MB–1 104 MB for other libraries.
工作流
- 1. Create a ZipArchive entry for "xl/worksheets/sheet1.xml". 2. Stream XML via XmlWriter into the entry. 3. Write layout elements (cols, frozen panes) before data. 4. Collect post‑sheetData features (merged cells, conditional formatting, etc.) while writing rows. 5. Emit collected features in schema order when sealing the sheet. 6. Write sharedStrings.xml and styles.xml after all sheets, keeping only small in‑memory dictionaries. 7. For AutoFit, spill rows to a temp file, measure widths, write header, then copy rows back in 64 KB chunks. 8. For template filling, stream template XML, adjust style and string numbering, stretch tables, drop calcChain.xml, and let Excel recalc on open.
证据
Most .NET Excel libraries build the whole workbook in memory and save it at the end. That's convenient, and it's why a 100,000-row export can allocate hundreds of megabytes to over a gigabyte.
100,000 rows × 10 formatted columns, BenchmarkDotNet, .NET 8: SheetStream (fastest compression) 534 ms 27 MB; MiniExcel 596 ms 343 MB; EPPlus 4.5.3.3 2,287 ms 390 MB; ClosedXML 3,103 ms 1,104 MB.
SheetStream throws a clear exception if you set them late.
- 记录日期: 2026-10-14T11:40:57.748Z