Class StreamWorkbook
- All Implemented Interfaces:
AutoCloseable
Use this when authoring a large export. The ordinary
Workbook path builds the whole workbook in
memory before writing a byte, which is the right trade when you need to read,
edit, recalculate or lay out — and the wrong one when you are pouring a
result set into a file. Each row here is serialized and deflated as it
arrives; the engine holds one row at a time. Writing 1,000,000 rows of five
columns costs the same memory as writing ten.
What you give up, in exchange:
- Rows go out in ascending order and never come back. Writing row 5 after row 9 throws — a streaming writer cannot rewind the file.
- Values only — cell values, number formats, bold / italic. No
charts, images, merges, conditional formats, comments or validation;
those need the whole-workbook model. Reach for
Workbookwhen you need them. - No recalculation. Formulas are written verbatim; supply the
cached value with
StreamRow.setFormula(int, String, double, int)if the consumer must see a number before opening in Excel. - Text is stored inline rather than in a shared string table, so heavily repeating text yields a larger file than the modeled writer produces. The table is what a streaming writer cannot afford: it must stay resident until the last sheet closes.
The file is complete only after close() (or finish());
abandoning the writer leaves a partial file behind, so use
try-with-resources:
try (StreamWorkbook wb = StreamWorkbook.create(Path.of("report.xlsx"))) {
int header = wb.addStyle(StreamStyle.defaults().withBold());
int money = wb.addStyle(StreamStyle.defaults().withNumberFormat("#,##0.00"));
wb.startSheet("Sales");
wb.row(0).addText("Customer", header).addText("Amount", header).commit();
int r = 1;
for (Order o : orders) {
wb.row(r++).addText(o.customer()).addNumber(o.amount(), money).commit();
}
}
Not thread-safe: rows are ordered, so one writer belongs to one thread.
-
Method Summary
Modifier and TypeMethodDescriptionintaddStyle(StreamStyle style) Registers a style and returns the index cells reference.voidclose()Finishes the workbook if it has not been finished already, so a normal try-with-resources exit produces a complete file.static StreamWorkbookOpens a streaming writer onfile, replacing it if it exists.voidfinish()Completes the package and closes the file.booleanWhether the package has been completed.row(int index) Begins rowindex(0-based).voidstartSheet(String name) Closes any open sheet and starts a new one.
-
Method Details
-
create
Opens a streaming writer onfile, replacing it if it exists.Under license enforcement this counts as a file operation and the output is stamped as evaluation material, exactly as an ordinary save is.
-
addStyle
Registers a style and returns the index cells reference. Identical styles return the same index, so calling this per row is free. -
startSheet
Closes any open sheet and starts a new one. Sheet names follow Excel's rules (31 characters, none of[]:*?/\); the engine writes what it is given. -
row
Begins rowindex(0-based). The returned builder is reused across rows — fill it and callStreamRow.commit()before asking for the next row. -
finish
public void finish()Completes the package and closes the file. Called automatically byclose(); call it directly only to surface a write failure at a specific point. -
close
public void close()Finishes the workbook if it has not been finished already, so a normal try-with-resources exit produces a complete file.If the block exits by exception,
finishmay itself throw (for instance with no sheet ever started). The native writer is released in either case, so no handle leaks.- Specified by:
closein interfaceAutoCloseable
-
isFinished
public boolean isFinished()Whether the package has been completed.
-