Archive validation and resource lifetime
On this page
Validate external archives
XLSX is a ZIP container. Limit uploaded bytes first, then bound entry count, per-entry expanded bytes, total expanded bytes and compression ratio. These limits are illustrative application choices:
using TExcel;
using var content = File.OpenRead("upload.xlsx");
var limits = new ExcelArchiveLimits(
Entries: 2000,
EntryBytes: 64L * 1024 * 1024,
ExpandedBytes: 256L * 1024 * 1024,
CompressionRatio: 200);
await ExcelArchiveValidator.ValidateAsync(content, limits);
using var reader = Excel.CreateReader(content, leaveOpen: true);
foreach (var row in reader.ReadSheetRows(includeCellFacts: true))
{
Console.WriteLine(row.Number);
}ValidateAsync checks metadata and actual decompressed bytes, retains ownership and resets the stream to position 0. Supply a readable, seekable stream.
ValidateMetadata checks the directory of a trusted, locally generated archive. It cannot replace actual expanded-byte validation for external uploads.
Validate business content
Archive validation does not establish correct sheets, headers, required fields, identity precision, unique names, name relationships or permissions. Validate first, aggregate errors and only then persist within the application’s transaction boundary.
Use ReadSheetRows for original row numbers and enable includeCellFacts to inspect original cell types and formulas.
Resource ownership
| Resource | Caller responsibility |
|---|---|
Lazy Query<T> result | Finish or dispose enumeration; avoid a preliminary full enumeration to count rows |
| Explicit reader | Keep alive throughout all dependent enumeration |
| Excel / CSV writer | Write sequentially and complete before consuming output |
| Caller-provided stream | Choose leaveOpen explicitly and dispose retained streams later |
MemoryStream | Account for the complete output remaining in memory |
Do not use one reader or writer concurrently. Dispose resources on cancellation and exceptions; failed output is not a successful export.