From 0636784aad039c83c258d44063a445d994b94dac Mon Sep 17 00:00:00 2001 From: Michele Bastione Date: Mon, 23 Mar 2026 23:44:14 +0100 Subject: [PATCH] Fixed ignore property set on dynamic columns still printing headers when exporting using SaveAsAsync --- .../OpenXml/ExcelOpenXmlSheetWriter.Async.cs | 19 +++++----- .../AsyncEnumerableWriteAdapter.cs | 16 ++++---- .../MiniExcelIssueAsyncTests.cs | 38 ++++++++++++++++++- 3 files changed, 54 insertions(+), 19 deletions(-) diff --git a/src/MiniExcel/OpenXml/ExcelOpenXmlSheetWriter.Async.cs b/src/MiniExcel/OpenXml/ExcelOpenXmlSheetWriter.Async.cs index 049e2e46..c7d3bc11 100644 --- a/src/MiniExcel/OpenXml/ExcelOpenXmlSheetWriter.Async.cs +++ b/src/MiniExcel/OpenXml/ExcelOpenXmlSheetWriter.Async.cs @@ -362,22 +362,21 @@ private static async Task WriteColumnsWidthsAsync(MiniExcelAsyncStreamWriter wri private async Task PrintHeaderAsync(MiniExcelAsyncStreamWriter writer, List props, CancellationToken cancellationToken = default) { - var xIndex = 1; - var yIndex = 1; + const int yIndex = 1; await writer.WriteAsync(WorksheetXml.StartRow(yIndex)); + var xIndex = 1; foreach (var p in props) { - cancellationToken.ThrowIfCancellationRequested(); - - if (p == null) + //reason : https://github.com/mini-software/MiniExcel/issues/142 + if (p != null) { - xIndex++; //reason : https://github.com/mini-software/MiniExcel/issues/142 - continue; - } + if (p.ExcelIgnore) + continue; - var r = ExcelOpenXmlUtils.ConvertXyToCell(xIndex, yIndex); - await WriteCellAsync(writer, r, columnName: p.ExcelColumnName); + var r = ExcelOpenXmlUtils.ConvertXyToCell(xIndex, yIndex); + await WriteCellAsync(writer, r, columnName: p.ExcelColumnName); + } xIndex++; } diff --git a/src/MiniExcel/WriteAdapter/AsyncEnumerableWriteAdapter.cs b/src/MiniExcel/WriteAdapter/AsyncEnumerableWriteAdapter.cs index 1f3e0d53..6949e4b4 100644 --- a/src/MiniExcel/WriteAdapter/AsyncEnumerableWriteAdapter.cs +++ b/src/MiniExcel/WriteAdapter/AsyncEnumerableWriteAdapter.cs @@ -73,16 +73,16 @@ private static async IAsyncEnumerable GetRowValuesAsync(T current foreach (var prop in props) { column++; - - if (prop is null) - continue; - yield return currentValue switch + if (prop is { ExcelIgnore: false }) { - IDictionary genericDictionary => new CellWriteInfo(genericDictionary[prop.Key.ToString()], column, prop), - IDictionary dictionary => new CellWriteInfo(dictionary[prop.Key], column, prop), - _ => new CellWriteInfo(prop.Property.GetValue(currentValue), column, prop) - }; + yield return currentValue switch + { + IDictionary genericDictionary => new CellWriteInfo(genericDictionary[prop.Key.ToString()], column, prop), + IDictionary dictionary => new CellWriteInfo(dictionary[prop.Key], column, prop), + _ => new CellWriteInfo(prop.Property.GetValue(currentValue), column, prop) + }; + } } } diff --git a/tests/MiniExcelTests/MiniExcelIssueAsyncTests.cs b/tests/MiniExcelTests/MiniExcelIssueAsyncTests.cs index 67c25566..c33efa33 100644 --- a/tests/MiniExcelTests/MiniExcelIssueAsyncTests.cs +++ b/tests/MiniExcelTests/MiniExcelIssueAsyncTests.cs @@ -1853,4 +1853,40 @@ private class Issue138ExcelRow public double? 波段 { get; set; } public double? 當沖 { get; set; } } -} \ No newline at end of file + + [Fact] + public async Task TestIssue584() + { + var excelconfig = new OpenXmlConfiguration + { + DynamicColumns = + [ + new DynamicExcelColumn("Id") { Ignore = true } + ] + }; + + await using var conn = Db.GetConnection(); + conn.Open(); + + await using var cmd = conn.CreateCommand(); + cmd.CommandText = + """ + WITH test('Id', 'Name') AS ( + VALUES + (1, 'test1'), + (2, 'test2'), + (3, 'test3') + ) + SELECT * FROM test; + """; + + await using var reader = cmd.ExecuteReader(); + + using var path = AutoDeletingPath.Create(); + await MiniExcel.SaveAsAsync(path.FilePath, reader, configuration: excelconfig, overwriteFile: true); + + var rows = (await MiniExcel.QueryAsync(path.FilePath)).ToList(); + Assert.All(rows, x => Assert.Single(x)); + Assert.Equal("Name", rows[0].A); + } +}