Interface IWorkbookEditor
- Namespace
- DocToolkit.Extensions.DependencyInjection
- Assembly
- DocToolkit.Extensions.DependencyInjection.dll
Creates, reads and edits Excel (.xlsx) workbooks. Registered by AddDocToolkit(IServiceCollection, Action<DocToolkitOptions>?).
public interface IWorkbookEditor
Methods
AddChart(byte[], string, string, ChartType, ChartData, string, int, int)
Adds a chart to sheetName, anchored at cellRef, and
returns the updated workbook.
byte[] AddChart(byte[] xlsx, string sheetName, string cellRef, ChartType type, ChartData data, string title = "", int widthPixels = 640, int heightPixels = 360)
Parameters
xlsxbyte[]The workbook to add the chart to. It is not modified.
sheetNamestringThe sheet to add the chart to.
cellRefstringAn A1-style cell reference for the chart's top-left corner, e.g.
"B2".typeChartTypeThe chart's shape.
dataChartDataThe chart's categories and value series.
titlestringThe chart's title. Empty for no title.
widthPixelsintThe chart's width, in pixels.
heightPixelsintThe chart's height, in pixels.
Returns
- byte[]
Exceptions
- ArgumentNullException
xlsx,dataor another required argument is null.- ArgumentException
xlsxis empty, orsheetName/cellRefis blank.- DocumentConversionException
The workbook could not be opened, the sheet does not exist, or the reference is not valid.
AddChartAsync(Stream, string, string, ChartType, ChartData, Stream, string, int, int, CancellationToken)
Reads a workbook from source, adds a chart, and writes the result to
destination — see AddChart(byte[], string, string, ChartType, ChartData, string, int, int) for the parameters.
source is read to its end and destination is
written; neither is disposed, closed or sought, and neither has to be seekable.
Task AddChartAsync(Stream source, string sheetName, string cellRef, ChartType type, ChartData data, Stream destination, string title = "", int widthPixels = 640, int heightPixels = 360, CancellationToken ct = default)
Parameters
sourceStreamThe stream the workbook is read from.
sheetNamestringThe sheet to add the chart to.
cellRefstringAn A1-style cell reference for the chart's top-left corner, e.g.
"B2".typeChartTypeThe chart's shape.
dataChartDataThe chart's categories and value series.
destinationStreamThe stream the updated workbook is written to.
titlestringThe chart's title. Empty for no title.
widthPixelsintThe chart's width, in pixels.
heightPixelsintThe chart's height, in pixels.
ctCancellationTokenCancels the read, the edit and the write.
Returns
Exceptions
- ArgumentNullException
source,destinationordatais null.- ArgumentException
sourceis not readable or held no bytes, a name is blank, ordestinationis not writable.- OperationCanceledException
ctwas cancelled.- DocumentConversionException
The workbook could not be opened, the sheet does not exist, or the reference is not valid.
AddDefinedName(byte[], string, string, string)
Adds a workbook-scoped defined name pointing at range on
sheetName, and returns the updated workbook.
byte[] AddDefinedName(byte[] xlsx, string name, string sheetName, string range)
Parameters
xlsxbyte[]The workbook to add the defined name to. It is not modified.
namestringThe defined name.
sheetNamestringThe sheet
rangeis on.rangestringThe cells the name refers to, such as
A1:B2.
Returns
- byte[]
Remarks
sheetName is always single-quoted in the reference this writes (e.g.
'Sales'!A1:B2), whether or not it needs to be. Measured directly: a sheet name
containing a space and left unquoted does not raise an error at write time — the defined
name simply is not present when the file is reopened, with nothing telling the caller why.
Exceptions
- ArgumentNullException
Any argument is null.
- ArgumentException
xlsxis empty, a name argument is blank, orrangenames a sheet.- DocumentConversionException
The workbook could not be opened, the sheet does not exist, or
nameis already in use.
AddDefinedNameAsync(Stream, string, string, string, Stream, CancellationToken)
Reads a workbook from source, adds a defined name, and writes the result
to destination — see AddDefinedName(byte[], string, string, string) for the parameters.
source is read to its end and destination is
written; neither is disposed, closed or sought, and neither has to be seekable.
Task AddDefinedNameAsync(Stream source, string name, string sheetName, string range, Stream destination, CancellationToken ct = default)
Parameters
sourceStreamThe stream the workbook is read from.
namestringThe defined name.
sheetNamestringThe sheet
rangeis on.rangestringThe cells the name refers to, such as
A1:B2.destinationStreamThe stream the updated workbook is written to.
ctCancellationTokenCancels the read, the edit and the write.
Returns
Exceptions
- ArgumentNullException
sourceordestinationis null.- ArgumentException
sourceis not readable or held no bytes, a name argument is blank,rangenames a sheet, ordestinationis not writable.- OperationCanceledException
ctwas cancelled.- DocumentConversionException
The workbook could not be opened, the sheet does not exist, or
nameis already in use.
AddImage(byte[], string, string, byte[], int?, int?)
Adds an image to sheetName, anchored at cellRef, and
returns the updated workbook.
byte[] AddImage(byte[] xlsx, string sheetName, string cellRef, byte[] image, int? widthPixels = null, int? heightPixels = null)
Parameters
xlsxbyte[]The workbook to add the image to. It is not modified.
sheetNamestringThe sheet to add the image to.
cellRefstringAn A1-style cell reference for the image's top-left corner, e.g.
"B2".imagebyte[]The image bytes. PNG and JPEG only, decided by magic bytes.
widthPixelsint?The image's width, in pixels. null uses the image's own intrinsic width, scaled to match
heightPixelsif that is given.heightPixelsint?The image's height, in pixels. null uses the image's own intrinsic height, scaled to match
widthPixelsif that is given.
Returns
- byte[]
Remarks
Sizes are in pixels, matching AddChart(byte[], string, string, ChartType, ChartData, string, int, int) rather than the points/EMU the DOCX and PPTX drawing model uses. The format is decided by magic bytes, never a filename.
Exceptions
- ArgumentNullException
xlsx,imageor another required argument is null.- ArgumentException
xlsxorimageis empty, orsheetName/cellRefis blank.- ArgumentOutOfRangeException
A supplied size is zero or negative.
- DocumentConversionException
The workbook could not be opened, the sheet does not exist, the reference is not valid, or the image is neither PNG nor JPEG.
AddImageAsync(Stream, string, string, byte[], Stream, int?, int?, CancellationToken)
Reads a workbook from source, adds an image, and writes the result to
destination — see AddImage(byte[], string, string, byte[], int?, int?) for the parameters.
source is read to its end and destination is
written; neither is disposed, closed or sought, and neither has to be seekable.
Task AddImageAsync(Stream source, string sheetName, string cellRef, byte[] image, Stream destination, int? widthPixels = null, int? heightPixels = null, CancellationToken ct = default)
Parameters
sourceStreamThe stream the workbook is read from.
sheetNamestringThe sheet to add the image to.
cellRefstringAn A1-style cell reference for the image's top-left corner, e.g.
"B2".imagebyte[]The image bytes. PNG and JPEG only, decided by magic bytes.
destinationStreamThe stream the updated workbook is written to.
widthPixelsint?The image's width, in pixels. See AddImage(byte[], string, string, byte[], int?, int?).
heightPixelsint?The image's height, in pixels. See AddImage(byte[], string, string, byte[], int?, int?).
ctCancellationTokenCancels the read, the edit and the write.
Returns
Exceptions
- ArgumentNullException
source,destinationorimageis null.- ArgumentException
sourceis not readable or held no bytes,imageis empty, a name is blank, ordestinationis not writable.- ArgumentOutOfRangeException
A supplied size is zero or negative.
- OperationCanceledException
ctwas cancelled.- DocumentConversionException
The workbook could not be opened, the sheet does not exist, the reference is not valid, or the image is neither PNG nor JPEG.
AddPivotTable(byte[], string, string, string, string, IEnumerable<string>, IEnumerable<PivotDataField>, IEnumerable<string>?, IEnumerable<string>?, bool, bool)
Adds a pivot table to sheetName and returns the updated workbook.
byte[] AddPivotTable(byte[] xlsx, string sheetName, string sourceRange, string destinationCell, string name, IEnumerable<string> rowFields, IEnumerable<PivotDataField> dataFields, IEnumerable<string>? columnFields = null, IEnumerable<string>? pageFields = null, bool showRowGrandTotals = true, bool showColumnGrandTotals = true)
Parameters
xlsxbyte[]The workbook to add the pivot table to. It is not modified.
sheetNamestringThe sheet to add the pivot table to.
sourceRangestringAn A1-style range naming the source data, e.g.
"A1:C10".destinationCellstringAn A1-style cell reference for the pivot table's top-left corner, e.g.
"E1".namestringThe pivot table's name.
rowFieldsIEnumerable<string>Source column headers to group by, down the rows. At least one.
dataFieldsIEnumerable<PivotDataField>The aggregated value columns. At least one.
columnFieldsIEnumerable<string>Source column headers to group by, across the columns. Optional.
pageFieldsIEnumerable<string>Source column headers used as report filters. Optional.
showRowGrandTotalsboolWhether to show a grand total row.
showColumnGrandTotalsboolWhether to show a grand total column.
Returns
- byte[]
Remarks
The result grid is empty until Excel opens and recalculates it. A pivot table's aggregated values are computed by whichever application opens the file — nothing that writes it (this method included) populates the grid. Open the result in Excel (or an equivalent) to see it populated.
Exceptions
- ArgumentNullException
xlsx,rowFields,dataFieldsor another required argument is null.- ArgumentException
xlsxis empty, a name argument is blank, orrowFields/dataFieldsis empty.- DocumentConversionException
The workbook could not be opened, the sheet does not exist, or
destinationCellis not a valid cell reference.
AddPivotTableAsync(Stream, string, string, string, string, IEnumerable<string>, IEnumerable<PivotDataField>, Stream, IEnumerable<string>?, IEnumerable<string>?, bool, bool, CancellationToken)
Reads a workbook from source, adds a pivot table, and writes the
result to destination — see AddPivotTable(byte[], string, string, string, string, IEnumerable<string>, IEnumerable<PivotDataField>, IEnumerable<string>?, IEnumerable<string>?, bool, bool) for the
parameters.
source is read to its end and destination is
written; neither is disposed, closed or sought, and neither has to be seekable.
Task AddPivotTableAsync(Stream source, string sheetName, string sourceRange, string destinationCell, string name, IEnumerable<string> rowFields, IEnumerable<PivotDataField> dataFields, Stream destination, IEnumerable<string>? columnFields = null, IEnumerable<string>? pageFields = null, bool showRowGrandTotals = true, bool showColumnGrandTotals = true, CancellationToken ct = default)
Parameters
sourceStreamThe stream the workbook is read from.
sheetNamestringThe sheet to add the pivot table to.
sourceRangestringAn A1-style range naming the source data, e.g.
"A1:C10".destinationCellstringAn A1-style cell reference for the pivot table's top-left corner, e.g.
"E1".namestringThe pivot table's name.
rowFieldsIEnumerable<string>Source column headers to group by, down the rows. At least one.
dataFieldsIEnumerable<PivotDataField>The aggregated value columns. At least one.
destinationStreamThe stream the updated workbook is written to.
columnFieldsIEnumerable<string>Source column headers to group by, across the columns. Optional.
pageFieldsIEnumerable<string>Source column headers used as report filters. Optional.
showRowGrandTotalsboolWhether to show a grand total row.
showColumnGrandTotalsboolWhether to show a grand total column.
ctCancellationTokenCancels the read, the edit and the write.
Returns
Exceptions
- ArgumentNullException
source,destination,rowFieldsordataFieldsis null.- ArgumentException
sourceis not readable or held no bytes, a name is blank, orrowFields/dataFieldsis empty.- OperationCanceledException
ctwas cancelled.- DocumentConversionException
The workbook could not be opened, the sheet does not exist, or
destinationCellis not a valid cell reference.
AppendRows(byte[], string, IEnumerable<IEnumerable<object?>>)
Appends rows to sheetName, after its last used row,
leaving every other sheet and all existing formatting as it was.
"Last used" counts a cell comment or a merged range even where the cell has no value, so a stray comment far below the data pushes the append below it.
byte[] AppendRows(byte[] xlsx, string sheetName, IEnumerable<IEnumerable<object?>> rows)
Parameters
xlsxbyte[]sheetNamestringrowsIEnumerable<IEnumerable<object>>
Returns
- byte[]
Exceptions
- ArgumentNullException
xlsxorrowsis null.- ArgumentException
xlsxis empty,sheetNameis blank, longer than 31 characters or contains one of: \ / ? * [ ], or an element ofrowsis null.- DocumentConversionException
The sheet was not found, or the package could not be opened or edited.
AppendRowsAsync(Stream, string, IEnumerable<IEnumerable<object?>>, Stream, CancellationToken)
Reads a workbook from source, appends rows to
sheetName, and writes the result to destination. See
AppendRows(byte[], string, IEnumerable<IEnumerable<object?>>) for the semantics.
source is read to its end and destination
is written; neither is disposed, closed nor sought.
Task AppendRowsAsync(Stream source, string sheetName, IEnumerable<IEnumerable<object?>> rows, Stream destination, CancellationToken ct = default)
Parameters
sourceStreamsheetNamestringrowsIEnumerable<IEnumerable<object>>destinationStreamctCancellationToken
Returns
Exceptions
- ArgumentNullException
rowsis null.- ArgumentException
sourceis not readable or held no bytes,destinationis not writable,sheetNameis invalid as above, or an element ofrowsis null.- OperationCanceledException
ctwas cancelled.- DocumentConversionException
The sheet was not found, or the package could not be opened or edited.
Create(IEnumerable<XlsxSheet>)
Builds a workbook from sheets, one worksheet each, in sequence order.
Content comes from data rather than a template, so there is no source file to edit.
A cell holding a XlsxFormula is written as a formula. No cached result is stored, so a reader that only reads cached values sees an empty cell until Excel has opened and saved the file; this package's own readers compute on read.
byte[] Create(IEnumerable<XlsxSheet> sheets)
Parameters
sheetsIEnumerable<XlsxSheet>
Returns
- byte[]
Exceptions
- ArgumentNullException
sheetsis null.- ArgumentException
sheetsis empty, contains a null element, or names the same sheet twice.- DocumentConversionException
The workbook could not be built.
Create(string, IEnumerable<IEnumerable<object?>>)
Creates a workbook with one sheet populated from rows.
byte[] Create(string sheetName, IEnumerable<IEnumerable<object?>> rows)
Parameters
sheetNamestringrowsIEnumerable<IEnumerable<object>>
Returns
- byte[]
Exceptions
- ArgumentNullException
rowsis null.- ArgumentException
sheetNameis blank, or a row is null.- DocumentConversionException
The workbook could not be built.
CreateAsync(IEnumerable<XlsxSheet>, Stream, CancellationToken)
Builds a workbook from sheets and writes it to
destination. See Create(IEnumerable<XlsxSheet>)
for the semantics.
destination is written and is neither disposed, closed nor
sought, so an HTTP response body is a valid destination.
Task CreateAsync(IEnumerable<XlsxSheet> sheets, Stream destination, CancellationToken ct = default)
Parameters
sheetsIEnumerable<XlsxSheet>destinationStreamctCancellationToken
Returns
Exceptions
- ArgumentNullException
Either argument is null.
- ArgumentException
sheetsis invalid as above, ordestinationis not writable.- OperationCanceledException
ctwas cancelled.- DocumentConversionException
The workbook could not be built or written.
CreateAsync(string, IEnumerable<IEnumerable<object?>>, Stream, CancellationToken)
Builds a workbook with one sheet populated from rows and writes it to
destination. See Create(string, IEnumerable<IEnumerable<object?>>) for the exact typing and culture
rules applied to each cell. destination is written and is not
disposed, closed or sought.
Task CreateAsync(string sheetName, IEnumerable<IEnumerable<object?>> rows, Stream destination, CancellationToken ct = default)
Parameters
sheetNamestringrowsIEnumerable<IEnumerable<object>>destinationStreamctCancellationToken
Returns
Exceptions
- ArgumentNullException
rowsordestinationis null.- ArgumentException
sheetNameis blank, a row is null, ordestinationis not writable.- OperationCanceledException
ctwas cancelled.- DocumentConversionException
The workbook could not be built or written.
EvaluateFormulas(byte[])
A copy of xlsx with every formula's computed value written into the
file, not just held in memory.
byte[] EvaluateFormulas(byte[] xlsx)
Parameters
xlsxbyte[]
Returns
- byte[]
Remarks
A formula XlsxFormulaInspection would report as unsupported is left exactly as it was — no plausible-looking value is invented for it. Call InspectFormulas(byte[]) first if that distinction matters to the caller.
Exceptions
- ArgumentNullException
xlsxis null.- ArgumentException
xlsxis empty.- DocumentConversionException
The workbook could not be read or written.
EvaluateFormulasAsync(Stream, Stream, CancellationToken)
A copy of xlsx with every formula's computed value written into the
file, not just held in memory.
Task EvaluateFormulasAsync(Stream source, Stream destination, CancellationToken ct = default)
Parameters
sourceStreamThe stream the workbook is read from.
destinationStreamThe stream the evaluated workbook is written to.
ctCancellationTokenCancels the read, the edit and the write.
Returns
Remarks
source is read to its end and destination is
written; neither is disposed, closed or sought, and neither has to be seekable.
Exceptions
- ArgumentNullException
An argument is null.
- ArgumentException
sourceis not readable or held no bytes, ordestinationis not writable.- OperationCanceledException
ctwas cancelled.- DocumentConversionException
The workbook could not be read or written.
Format(byte[], string, XlsxFormat)
Applies format to sheetName and returns the workbook.
byte[] Format(byte[] xlsx, string sheetName, XlsxFormat format)
Parameters
xlsxbyte[]sheetNamestringformatXlsxFormat
Returns
- byte[]
Remarks
Formatting is applied to an existing workbook rather than being an argument to
Create, so it composes with every way a workbook can arrive - built here, appended
to, or handed in by a caller who never used this library. See
XlsxFormat for the boundary: a CLOSED vocabulary rather than a
small one, and what is still deliberately outside it.
Exceptions
- ArgumentNullException
xlsxorformatis null.- ArgumentException
xlsxis empty, or the sheet name is blank.- DocumentConversionException
The workbook could not be opened, or the sheet does not exist.
FormatAsync(Stream, string, XlsxFormat, Stream, CancellationToken)
Reads a workbook from source, applies format, and
writes the result to destination. Neither stream is disposed, closed or
sought.
Task FormatAsync(Stream source, string sheetName, XlsxFormat format, Stream destination, CancellationToken ct = default)
Parameters
sourceStreamsheetNamestringformatXlsxFormatdestinationStreamctCancellationToken
Returns
Exceptions
- ArgumentNullException
formatis null, or a stream is null.- ArgumentException
A stream is unusable,
sourceheld no bytes, or the sheet name is blank.- OperationCanceledException
ctwas cancelled.- DocumentConversionException
The workbook could not be opened, or the sheet does not exist.
InspectFormulas(byte[])
Every formula xlsx carries, and whether each one is understood well
enough to trust its value. See XlsxFormulaInspection for why this
asks rather than assumes.
XlsxFormulaInspection InspectFormulas(byte[] xlsx)
Parameters
xlsxbyte[]
Returns
Exceptions
- ArgumentNullException
xlsxis null.- ArgumentException
xlsxis empty.- DocumentConversionException
The workbook could not be read.
InspectFormulasAsync(Stream, CancellationToken)
Every formula xlsx carries, and whether each one is understood well
enough to trust its value. See XlsxFormulaInspection for why this
asks rather than assumes.
Task<XlsxFormulaInspection> InspectFormulasAsync(Stream source, CancellationToken ct = default)
Parameters
sourceStreamThe stream the workbook is read from.
ctCancellationTokenCancels the read.
Returns
Remarks
source is read to its end and is neither disposed, closed nor sought.
Exceptions
- ArgumentNullException
sourceis null.- ArgumentException
sourceis not readable or held no bytes.- OperationCanceledException
ctwas cancelled.- DocumentConversionException
The workbook could not be read.
InspectSignatures(byte[])
Inspects xlsx for digital signatures — whether it carries one, how
many, and who claims to have signed it. Does not validate anything cryptographically; see
ValidateSignatures(byte[], DocumentSignatureValidationOptions?).
DocumentSignatureInfo InspectSignatures(byte[] xlsx)
Parameters
xlsxbyte[]
Returns
Exceptions
- ArgumentNullException
xlsxis null.- ArgumentException
xlsxis empty.- DocumentConversionException
The workbook could not be inspected.
InspectSignaturesAsync(Stream, CancellationToken)
Reads an .xlsx from source and inspects it for digital signatures — see
InspectSignatures(byte[]). source is read to its end and is
neither disposed, closed nor sought.
Task<DocumentSignatureInfo> InspectSignaturesAsync(Stream source, CancellationToken ct = default)
Parameters
sourceStreamctCancellationToken
Returns
Exceptions
- ArgumentNullException
sourceis null.- ArgumentException
sourceis not readable or held no bytes.- OperationCanceledException
ctwas cancelled.- DocumentConversionException
The workbook could not be inspected.
IsProtected(byte[])
Whether xlsx is encrypted - that is, whether the other members here
will refuse it. Reads the file signature; needs no password.
bool IsProtected(byte[] xlsx)
Parameters
xlsxbyte[]
Returns
Exceptions
- ArgumentNullException
xlsxis null.
IsProtectedAsync(Stream, CancellationToken)
Whether xlsx is encrypted - that is, whether the other members here
will refuse it. Reads the file signature; needs no password.
Task<bool> IsProtectedAsync(Stream source, CancellationToken ct = default)
Parameters
sourceStreamThe stream the workbook is read from.
ctCancellationTokenCancels the read.
Returns
Remarks
source is read to its end and is neither disposed, closed nor
sought. Unlike IsProtected(byte[]), which answers false
for an empty array, an empty source is rejected - every Stream
overload in this package treats a source that held no bytes as a caller error rather
than as content.
Exceptions
- ArgumentNullException
sourceis null.- ArgumentException
sourceis not readable or held no bytes.- OperationCanceledException
ctwas cancelled.
Protect(byte[], string)
A copy of xlsx encrypted with password.
byte[] Protect(byte[] xlsx, string password)
Parameters
Returns
- byte[]
Remarks
File encryption, not the "restrict editing" flag. The result is a compound file rather than a XLSX package, so every other member here refuses it - call Unprotect(byte[], string) first.
Exceptions
- ArgumentNullException
xlsxorpasswordis null.- ArgumentException
xlsxis empty, orpasswordis empty.- DocumentConversionException
It could not be read or encrypted.
ProtectAsync(Stream, Stream, string, CancellationToken)
Reads a workbook from source and writes the encrypted copy to
destination. Neither stream is disposed, closed or sought.
Task ProtectAsync(Stream source, Stream destination, string password, CancellationToken ct = default)
Parameters
sourceStreamdestinationStreampasswordstringctCancellationToken
Returns
Exceptions
- ArgumentNullException
Either stream is null, or
passwordis null.- ArgumentException
A stream is unusable, or
passwordis empty.- OperationCanceledException
ctwas cancelled.- DocumentConversionException
It could not be encrypted.
ReadCell(byte[], string, string)
Reads a cell as a string. cellRef is an A1-style reference.
string ReadCell(byte[] xlsx, string sheetName, string cellRef)
Parameters
Returns
Exceptions
- ArgumentNullException
Any argument is null.
- ArgumentException
xlsxis empty, or a name is blank.- DocumentConversionException
The workbook could not be opened, the sheet does not exist, or the reference is not valid.
ReadCellAsync(Stream, string, string, CancellationToken)
Reads a workbook from source and returns a cell as a string.
cellRef is an A1-style reference. source is
read to its end and is neither disposed, closed nor sought.
Task<string> ReadCellAsync(Stream source, string sheetName, string cellRef, CancellationToken ct = default)
Parameters
sourceStreamsheetNamestringcellRefstringctCancellationToken
Returns
Exceptions
- ArgumentNullException
sourceis null.- ArgumentException
sourceis not readable or held no bytes, or a name is blank.- OperationCanceledException
ctwas cancelled.- DocumentConversionException
The workbook could not be opened, the sheet does not exist, or the reference is not valid.
ReadMetadata(byte[])
The document properties xlsx carries.
DocumentMetadata ReadMetadata(byte[] xlsx)
Parameters
xlsxbyte[]
Returns
Exceptions
- ArgumentNullException
xlsxis null.- ArgumentException
xlsxis empty.- DocumentConversionException
The workbook could not be read.
ReadMetadataAsync(Stream, CancellationToken)
The document properties xlsx carries.
Task<DocumentMetadata> ReadMetadataAsync(Stream source, CancellationToken ct = default)
Parameters
sourceStreamThe stream the workbook is read from.
ctCancellationTokenCancels the read.
Returns
Remarks
source is read to its end and is neither disposed, closed nor sought.
Exceptions
- ArgumentNullException
sourceis null.- ArgumentException
sourceis not readable or held no bytes.- OperationCanceledException
ctwas cancelled.- DocumentConversionException
The workbook could not be read.
ReadSheet(byte[], string)
Reads a whole sheet as strings, anchored at A1: if the data starts at C3, its first value
is at rows[2][2]. Every row is padded to the last used column, so all rows have the
same length; blank cells — and entirely blank rows inside the range — come back as empty
strings rather than being dropped, which keeps rows[r][c] positionally meaningful.
Values are produced exactly as ReadCell(byte[], string, string) produces them, so the two can never disagree about what a cell says. A formula cell yields its cached value: nothing in this library evaluates formulas.
Text follows the calling thread's CurrentCulture
— the same rule ReadCell(byte[], string, string) uses, and asymmetric with Create(string, IEnumerable<IEnumerable<object?>>), which
deliberately writes with InvariantCulture so
the same code produces the same file everywhere. A number such as 1234.5 reads back
as "1234.5" under an invariant or en-US culture but "1234,5" under de-DE;
callers who parse the returned text as a number should account for that, e.g. by parsing
with an explicit CultureInfo rather than the default.
IReadOnlyList<IReadOnlyList<string>> ReadSheet(byte[] xlsx, string sheetName)
Parameters
Returns
- IReadOnlyList<IReadOnlyList<string>>
The sheet's used range, anchored at A1 and padded rectangular. Empty only if the sheet holds no values and no cell comments: formatting alone never widens the range, but a comment on an otherwise-blank cell does, because ClosedXML's
LastCellUsed()counts it as used.
Remarks
The whole range is materialised into memory at once, so its cost is proportional to
rows × columns, not to how much of that rectangle actually holds data. To keep
one far-flung stray value from exhausting memory, ReadSheet(byte[], string) throws
DocumentConversionException rather than allocate when the used
range exceeds 2,000,000 cells.
Exceptions
- ArgumentNullException
Any argument is null.
- ArgumentException
xlsxis empty, orsheetNameis blank.- DocumentConversionException
The workbook could not be opened, the sheet does not exist, or the sheet's used range exceeds the 2,000,000-cell limit ReadSheet(byte[], string) will materialise.
ReadSheetAsync(Stream, string, CancellationToken)
Reads a workbook from source and returns a whole sheet as strings. See
ReadSheet(byte[], string) for the anchoring, padding, culture and formula rules — this
overload applies the identical logic. source is read to its end
and is neither disposed, closed nor sought.
Task<IReadOnlyList<IReadOnlyList<string>>> ReadSheetAsync(Stream source, string sheetName, CancellationToken ct = default)
Parameters
sourceStreamThe stream the workbook is read from.
sheetNamestringThe sheet to read.
ctCancellationTokenCancels the read.
Returns
- Task<IReadOnlyList<IReadOnlyList<string>>>
The sheet's used range, anchored at A1 and padded rectangular. Empty only if the sheet holds no values and no cell comments — see ReadSheet(byte[], string).
Exceptions
- ArgumentNullException
sourceis null.- ArgumentException
sourceis not readable or held no bytes, orsheetNameis blank.- OperationCanceledException
ctwas cancelled.- DocumentConversionException
The workbook could not be opened, the sheet does not exist, or the sheet's used range exceeds the 2,000,000-cell limit ReadSheet(byte[], string) will materialise.
SetCell(byte[], string, string, object?)
Sets a cell and returns the updated workbook bytes.
byte[] SetCell(byte[] xlsx, string sheetName, string cellRef, object? value)
Parameters
Returns
- byte[]
Exceptions
- ArgumentNullException
Any argument other than
valueis null.- ArgumentException
xlsxis empty, or a name is blank.- DocumentConversionException
The workbook could not be opened, the sheet does not exist, or the reference is not valid.
SetCellAsync(Stream, string, string, object?, Stream, CancellationToken)
Reads a workbook from source, sets one cell, and writes the result to
destination. cellRef is an A1-style reference.
source is read to its end and destination is
written; neither is disposed, closed or sought.
Task SetCellAsync(Stream source, string sheetName, string cellRef, object? value, Stream destination, CancellationToken ct = default)
Parameters
Returns
Exceptions
- ArgumentNullException
sourceordestinationis null.- ArgumentException
sourceis not readable or held no bytes, a name is blank, ordestinationis not writable.- OperationCanceledException
ctwas cancelled.- DocumentConversionException
The workbook could not be opened, the sheet does not exist, or the reference is not valid.
SheetNames(byte[])
Lists every sheet in the workbook, in tab order, including hidden sheets — hiding a sheet is a presentation choice, not a privacy boundary, and a caller who cannot see a hidden sheet listed has no way to discover it exists.
IReadOnlyList<string> SheetNames(byte[] xlsx)
Parameters
xlsxbyte[]The workbook bytes.
Returns
- IReadOnlyList<string>
The sheet names, in tab order.
Exceptions
- ArgumentNullException
xlsxis null.- ArgumentException
xlsxis empty.- DocumentConversionException
The workbook could not be opened.
SheetNamesAsync(Stream, CancellationToken)
Reads a workbook from source and lists every sheet in tab order,
including hidden sheets. source is read to its end and is neither
disposed, closed nor sought.
Task<IReadOnlyList<string>> SheetNamesAsync(Stream source, CancellationToken ct = default)
Parameters
sourceStreamThe stream the workbook is read from.
ctCancellationTokenCancels the read.
Returns
- Task<IReadOnlyList<string>>
The sheet names, in tab order.
Exceptions
- ArgumentNullException
sourceis null.- ArgumentException
sourceis not readable or held no bytes.- OperationCanceledException
ctwas cancelled.- DocumentConversionException
The workbook could not be opened.
Unprotect(byte[], string)
A copy of xlsx with its encryption removed.
byte[] Unprotect(byte[] xlsx, string password)
Parameters
Returns
- byte[]
Exceptions
- ArgumentNullException
xlsxorpasswordis null.- ArgumentException
xlsxis empty, orpasswordis empty.- DocumentConversionException
The password was wrong, the workbook was not encrypted, or it could not be read.
UnprotectAsync(Stream, Stream, string, CancellationToken)
Reads an encrypted workbook from source and writes the unprotected copy to
destination. Neither stream is disposed, closed or sought.
Task UnprotectAsync(Stream source, Stream destination, string password, CancellationToken ct = default)
Parameters
sourceStreamdestinationStreampasswordstringctCancellationToken
Returns
Exceptions
- ArgumentNullException
Either stream is null, or
passwordis null.- ArgumentException
A stream is unusable, or
passwordis empty.- OperationCanceledException
ctwas cancelled.- DocumentConversionException
The password was wrong, or it could not be read.
ValidateSignatures(byte[], DocumentSignatureValidationOptions?)
Validates every digital signature xlsx carries, returning the
report-level tamper-detection verdict alongside each signature's own certificate chain
trust and revocation status. Never performs revocation checking or certificate downloads
over the network, regardless of options — see
DocumentSignatureValidationOptions's own remarks.
DocumentSignatureValidationReport ValidateSignatures(byte[] xlsx, DocumentSignatureValidationOptions? options = null)
Parameters
xlsxbyte[]optionsDocumentSignatureValidationOptions
Returns
Exceptions
- ArgumentNullException
xlsxis null.- ArgumentException
xlsxis empty.- DocumentConversionException
The workbook could not be validated.
ValidateSignaturesAsync(Stream, DocumentSignatureValidationOptions?, CancellationToken)
Reads an .xlsx from source and validates its digital signatures — see
ValidateSignatures(byte[], DocumentSignatureValidationOptions?). source is read to its end and is
neither disposed, closed nor sought.
Task<DocumentSignatureValidationReport> ValidateSignaturesAsync(Stream source, DocumentSignatureValidationOptions? options = null, CancellationToken ct = default)
Parameters
sourceStreamoptionsDocumentSignatureValidationOptionsctCancellationToken
Returns
Exceptions
- ArgumentNullException
sourceis null.- ArgumentException
sourceis not readable or held no bytes.- OperationCanceledException
ctwas cancelled.- DocumentConversionException
The workbook could not be validated.
WithMetadata(byte[], DocumentMetadata)
A copy of xlsx carrying metadata.
byte[] WithMetadata(byte[] xlsx, DocumentMetadata metadata)
Parameters
xlsxbyte[]metadataDocumentMetadata
Returns
- byte[]
Remarks
A null property leaves what the workbook already had in place, so stamping a title does not silently erase an author. Pass an empty string to clear one.
Exceptions
- ArgumentNullException
xlsxormetadatais null.- ArgumentException
xlsxis empty.- DocumentConversionException
The workbook could not be read or written.
WithMetadataAsync(Stream, DocumentMetadata, Stream, CancellationToken)
A copy of xlsx carrying metadata.
Task WithMetadataAsync(Stream source, DocumentMetadata metadata, Stream destination, CancellationToken ct = default)
Parameters
sourceStreamThe stream the workbook is read from.
metadataDocumentMetadataThe properties to stamp.
destinationStreamThe stream the updated workbook is written to.
ctCancellationTokenCancels the read, the edit and the write.
Returns
Remarks
source is read to its end and destination is
written; neither is disposed, closed or sought, and neither has to be seekable.
Exceptions
- ArgumentNullException
An argument is null.
- ArgumentException
sourceis not readable or held no bytes, ordestinationis not writable.- OperationCanceledException
ctwas cancelled.- DocumentConversionException
The workbook could not be read or written.