Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,25 @@ Cells covered by a shared formula report `HasFormula` as `true` but leave `Formu
Font colors can also be theme colors (`Theme` and `Tint`), indexed colors (`Indexed`) or automatic (`Auto`).
`NumberFormat` is null for cells in the General format.

#### 14. Limit on empty cells

Rows that are missing from the worksheet are returned as empty rows. To protect against worksheets that declare a huge range with little data, a query throws an `InvalidDataException` once the empty cells it would have to fill in exceed `MaxSynthesizedCells` (100,000 by default).
Each empty row counts as the number of columns being read, so a `QueryRange` with a wide `endColumnIndex` reaches the limit after a few empty rows (e.g. 148 empty rows × 1000 columns).

To read such worksheets raise the limit, or set it to `null` to disable it:

```csharp
var config = new OpenXmlConfiguration
{
MaxSynthesizedCells = 10_000_000 // or null to disable the limit
};

var importer = MiniExcelV2.Importers.GetOpenXmlImporter();
var rows = importer.QueryRange(path, startColumnIndex: 1, endColumnIndex: 1000, configuration: config);
```

The limit does not apply when `IgnoreEmptyRows` is enabled, since empty rows are then skipped.


### Create/Export Excel <a name="docs-export" />

Expand Down
1 change: 1 addition & 0 deletions V2-Upgrade-Notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ so the return type for `OpenXmlImporter.QueryAsync` is `IAsyncEnumerable<T>` ins
- A `leaveOpen` parameter has been added to most methods that take a stream as input in both `OpenXmlImporter` and `CsvImporter` to configure whether the stream must be disposed after the operation performed is completed.
- The `useHeaderRow` parameter in multiple `OpenXmlImporter` and `CsvImporter` methods has been renamed to `hasHeaderRow` for making its usage clearer.
- `CsvExporter.Export` API methods, not being required to return the same type of `OpenXmlExporter.Export`, now return `int` instead of `int[]`.
- Queries now throw an `InvalidDataException` when the empty rows they have to fill in exceed `OpenXmlConfiguration.MaxSynthesizedCells` (100,000 cells by default). Each empty row counts as the number of columns being read, so a `QueryRange` with a wide `endColumnIndex` can hit the limit on a small worksheet. Raise the value or set it to `null` to read such worksheets as in 1.x; see [Limit on empty cells](README.md#14-limit-on-empty-cells).
5 changes: 4 additions & 1 deletion src/MiniExcel.OpenXml/OpenXmlConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ public class OpenXmlConfiguration : MiniExcelBaseConfiguration

/// <summary>
/// Maximum number of cells that may be synthesized for implicit empty rows during a query.
/// Each synthesized row counts as at least one cell. Set to <see langword="null" /> to disable the limit.
/// Each synthesized row counts as one cell per column being read: the header columns when a header row is used,
/// otherwise every column up to the last column of the sheet or the requested <c>endColumnIndex</c>, so a wide
/// <c>QueryRange</c> reaches the limit after a few empty rows.
/// The limit does not apply when <see cref="IgnoreEmptyRows" /> is enabled. Set to <see langword="null" /> to disable the limit.
/// </summary>
public long? MaxSynthesizedCells { get; set; } = 100_000;

Expand Down
4 changes: 3 additions & 1 deletion src/MiniExcel.OpenXml/Reader/OpenXmlReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,9 @@ internal static async Task<OpenXmlReader> CreateAsync(Stream stream, IMiniExcelC
synthesizedCellCount += (long)(emptyRowCount + selfClosingRowCount) * columnCount;

if (synthesizedCellCount > maxSynthesizedCells)
throw new InvalidDataException($"The worksheet exceeds the configured limit of {maxSynthesizedCells} synthesized empty cells.");
throw new InvalidDataException(
$"The worksheet exceeds the configured limit of {maxSynthesizedCells} synthesized empty cells. " +
$"Increase {nameof(OpenXmlConfiguration)}.{nameof(OpenXmlConfiguration.MaxSynthesizedCells)} or set it to null to disable the limit.");
}

var query = QueryRowAsync(reader, isFirstRow, startRowIndex, nextRowIndex, rowIndex,
Expand Down
33 changes: 33 additions & 0 deletions tests/MiniExcel.OpenXml.Tests/Issues/MiniExcelGithubIssuesTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,39 @@ public void QueryRejectsWorksheetExceedingSynthesizedCellLimit()
Assert.Contains("synthesized empty cells", exception.Message);
}

/// <summary>
/// https://github.com/mini-software/MiniExcel/issues/1075
/// </summary>
[Fact]
public void QueryRangeSynthesizedCellLimitNamesTheSettingAndCanBeDisabled()
{
using var path = AutoDeletingPath.Create();
_excelExporter.Export(path.FilePath, new[] { new { Value = "control" } });
SheetHelper.ReplaceFirstSheetXml(path.FilePath, """
<?xml version="1.0" encoding="utf-8"?>
<worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">
<sheetData>
<row r="1"><c r="A1" t="str"><v>top</v></c></row>
<row r="150"><c r="A150" t="str"><v>bottom</v></c></row>
</sheetData>
</worksheet>
""");

// Each of the 148 empty rows counts as the 1000 requested columns, which exceeds the default limit
var exception = Assert.Throws<InvalidDataException>(() =>
_excelImporter.QueryRange(path.FilePath, startColumnIndex: 1, endColumnIndex: 1000).ToList());
Assert.Contains(nameof(OpenXmlConfiguration.MaxSynthesizedCells), exception.Message);

var config = new OpenXmlConfiguration { MaxSynthesizedCells = null };
var rows = _excelImporter.QueryRange(path.FilePath, startColumnIndex: 1, endColumnIndex: 1000, configuration: config).ToList();
Assert.Equal(150, rows.Count);
Assert.Equal("bottom", (string)rows[149].A);

var ignoreEmptyRowsConfig = new OpenXmlConfiguration { IgnoreEmptyRows = true };
var nonEmptyRows = _excelImporter.QueryRange(path.FilePath, startColumnIndex: 1, endColumnIndex: 1000, configuration: ignoreEmptyRowsConfig).ToList();
Assert.Equal(2, nonEmptyRows.Count);
}

[Theory]
[InlineData("1048577")]
[InlineData("2000000000")]
Expand Down
Loading