Class XlsxSheetWriter
Writes a single worksheet's XML into an XLSX ZIP archive, buffering rows and flushing them to the entry stream once they cross a size threshold.
public sealed class XlsxSheetWriter : ITableSheetWriter<XlsxRowWriter>, ISheetWriter<XlsxRowWriter>, IDisposable, IAsyncDisposable
- Inheritance
-
XlsxSheetWriter
- Implements
- Inherited Members
- Extension Methods
Methods
BeginTable(string, IReadOnlyList<string>, ExcelTableOptions?)
Writes a header row with columns at the sheet's next row and opens a table there.
The table covers every row written until EndTable() or the end of the sheet.
public void BeginTable(string name, IReadOnlyList<string> columns, ExcelTableOptions? options = null)
Parameters
namestringThe table name: unique in the workbook, ignoring case, and valid as an Excel name.
columnsIReadOnlyList<string>The column names, written as the header row.
optionsExcelTableOptionsPosition and style. Defaults to Default.
Exceptions
- ArgumentNullException
nameorcolumnsis null.- ArgumentException
The name, a column name or the style is not valid, or the name is already used in this workbook.
- ArgumentOutOfRangeException
FirstColumn is negative.
- InvalidOperationException
A table is already open on this sheet, or a row is still active.
- ObjectDisposedException
The sheet has already been ended.
- ExcelLimitExceededException
The sheet has no room left for a header row and a data row.
BeginTableAsync(string, IReadOnlyList<string>, ExcelTableOptions?, CancellationToken)
Writes a header row with columns at the sheet's next row and opens a table there.
The table covers every row written until EndTable() or the end of the sheet.
public ValueTask BeginTableAsync(string name, IReadOnlyList<string> columns, ExcelTableOptions? options = null, CancellationToken ct = default)
Parameters
namestringThe table name: unique in the workbook, ignoring case, and valid as an Excel name.
columnsIReadOnlyList<string>The column names, written as the header row.
optionsExcelTableOptionsPosition and style. Defaults to Default.
ctCancellationTokenA token to cancel the header write.
Returns
Exceptions
- ArgumentNullException
nameorcolumnsis null.- ArgumentException
The name, a column name or the style is not valid, or the name is already used in this workbook.
- ArgumentOutOfRangeException
FirstColumn is negative.
- InvalidOperationException
A table is already open on this sheet, or a row is still active.
- ObjectDisposedException
The sheet has already been ended.
- ExcelLimitExceededException
The sheet has no room left for a header row and a data row.
Dispose()
Synchronous counterpart to DisposeAsync(), for native/unmanaged callers whose ABI is synchronous.
public void Dispose()
DisposeAsync()
Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources asynchronously.
public ValueTask DisposeAsync()
Returns
- ValueTask
A task that represents the asynchronous dispose operation.
End()
Synchronous counterpart to EndAsync(CancellationToken), for native/unmanaged callers whose ABI is synchronous.
public void End()
Exceptions
- ObjectDisposedException
The sheet has already been ended.
- InvalidOperationException
The active XlsxRowWriter has not been disposed.
EndAsync(CancellationToken)
Finalizes the sheet, writing any trailing structure so the sheet is complete within the workbook. The active row writer, if any, must already be disposed. No further rows may be started afterward.
public ValueTask EndAsync(CancellationToken ct = default)
Parameters
ctCancellationTokenA token to cancel the operation.
Returns
Exceptions
- ObjectDisposedException
The sheet has already been ended.
- InvalidOperationException
The active XlsxRowWriter has not been disposed.
EndTable()
Closes the open table at the last row written. Writes nothing; the table is stored when the sheet ends.
public void EndTable()
Exceptions
- InvalidOperationException
No table is open on this sheet.
- ObjectDisposedException
The sheet has already been ended.
SetColumnStyle(int, int)
Applies styleId to every cell of column columnIndex
that does not carry its own row style. Must be called before the first row is started.
public void SetColumnStyle(int columnIndex, int styleId)
Parameters
columnIndexintThe 0-based column index.
styleIdintThe style to apply, from AddStyle(CellStyle).
Exceptions
- ArgumentOutOfRangeException
columnIndexis negative or beyond the format's last column, orstyleIdis negative or was never returned by AddStyle(CellStyle).- InvalidOperationException
The first row has already been started.
- ArgumentOutOfRangeException
columnIndexis negative, orstyleIdis negative or was never returned by AddStyle(CellStyle).- InvalidOperationException
The first row has already been started.
SetColumnWidth(int, double)
Sets the display width, in characters, of column columnIndex. Must be
called before the first row is started.
public void SetColumnWidth(int columnIndex, double width)
Parameters
Exceptions
- ArgumentOutOfRangeException
columnIndexis negative or beyond the format's last column, orwidthis negative or greater than 255.- InvalidOperationException
The first row has already been started.
- ArgumentOutOfRangeException
columnIndexorwidthis negative.- InvalidOperationException
The first row has already been started.
StartRow()
Synchronous counterpart to StartRowAsync(CancellationToken). Row buffering is synchronous either way, so a caller that never awaits mid-row can skip the per-row ValueTask machinery.
public XlsxRowWriter StartRow()
Returns
- XlsxRowWriter
The reusable XlsxRowWriter for the new row.
Exceptions
- ObjectDisposedException
The sheet has already been ended.
- InvalidOperationException
The previous XlsxRowWriter has not been disposed.
- ExcelLimitExceededException
The worksheet's 1,048,576-row limit has been reached.
StartRow(int)
Synchronous counterpart to StartRowAsync(int, CancellationToken).
public XlsxRowWriter StartRow(int styleId)
Parameters
styleIdintThe style to apply to every cell of this row.
Returns
- XlsxRowWriter
The reusable XlsxRowWriter for the new row.
Exceptions
- ArgumentOutOfRangeException
styleIdis negative or was never returned by AddStyle(CellStyle).- ObjectDisposedException
The sheet has already been ended.
- InvalidOperationException
The previous XlsxRowWriter has not been disposed.
- ExcelLimitExceededException
The worksheet's 1,048,576-row limit has been reached.
StartRowAsync(int, CancellationToken)
Begins the next row, applying styleId (from AddStyle(CellStyle))
to its cells.
public ValueTask<XlsxRowWriter> StartRowAsync(int styleId, CancellationToken ct = default)
Parameters
styleIdintThe style to apply to every cell of this row.
ctCancellationTokenA token to cancel the operation.
Returns
- ValueTask<XlsxRowWriter>
The writer for the new row.
Exceptions
- ArgumentOutOfRangeException
styleIdis negative or was never returned by AddStyle(CellStyle).- ArgumentOutOfRangeException
styleIdis negative or was never returned by AddStyle(CellStyle).- ObjectDisposedException
The sheet has already been ended.
- InvalidOperationException
The previous XlsxRowWriter has not been disposed.
- ExcelLimitExceededException
The worksheet's 1,048,576-row limit has been reached.
StartRowAsync(CancellationToken)
Begins the next row, in order starting from the first, and returns its writer. Only one row may be open (started and not yet disposed) at a time; the previous row's writer must be disposed before starting the next one.
public ValueTask<XlsxRowWriter> StartRowAsync(CancellationToken ct = default)
Parameters
ctCancellationTokenA token to cancel the operation.
Returns
- ValueTask<XlsxRowWriter>
The writer for the new row.
Exceptions
- ObjectDisposedException
The sheet has already been ended.
- InvalidOperationException
The previous XlsxRowWriter has not been disposed.
- ExcelLimitExceededException
The worksheet's 1,048,576-row limit has been reached.