From 7ecf0ffe698218ae6c2f911b238c11eb89e0976d Mon Sep 17 00:00:00 2001 From: duszekmestre Date: Sat, 13 Jul 2024 03:21:49 +0200 Subject: [PATCH] Fix empty data reader issue. (#629) Co-authored-by: Lukasz Arciszewski --- src/MiniExcel/OpenXml/ExcelOpenXmlSheetWriter.cs | 5 ++++- tests/MiniExcelTests/MiniExcelIssueAsyncTests.cs | 14 ++++++++++++++ tests/MiniExcelTests/MiniExcelTests.csproj | 5 +++++ 3 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/MiniExcel/OpenXml/ExcelOpenXmlSheetWriter.cs b/src/MiniExcel/OpenXml/ExcelOpenXmlSheetWriter.cs index 35d1e430..d43fd029 100644 --- a/src/MiniExcel/OpenXml/ExcelOpenXmlSheetWriter.cs +++ b/src/MiniExcel/OpenXml/ExcelOpenXmlSheetWriter.cs @@ -143,7 +143,10 @@ private void GenerateSheetByIDataReader(MiniExcelStreamWriter writer, IDataReade if (_printHeader) { PrintHeader(writer, props); - yIndex++; + if (props.Count > 0) + { + yIndex++; + } } while (reader.Read()) diff --git a/tests/MiniExcelTests/MiniExcelIssueAsyncTests.cs b/tests/MiniExcelTests/MiniExcelIssueAsyncTests.cs index a2e15d50..23efd893 100644 --- a/tests/MiniExcelTests/MiniExcelIssueAsyncTests.cs +++ b/tests/MiniExcelTests/MiniExcelIssueAsyncTests.cs @@ -3,6 +3,7 @@ using MiniExcelLibs.OpenXml; using MiniExcelLibs.Tests.Utils; using Newtonsoft.Json; +using NSubstitute; using OfficeOpenXml; using System; using System.Collections.Generic; @@ -705,6 +706,19 @@ public async Task Issue211() } } + [Fact] + public async Task EmptyDataReaderIssue() + { + var path = PathHelper.GetTempPath(); + + var reader = Substitute.For(); + MiniExcel.SaveAs(path, reader, overwriteFile: true); + + var q = await MiniExcel.QueryAsync(path, true); + var rows = q.ToList(); + Assert.Empty(rows); + } + /// /// [When reading Excel, can return IDataReader and DataTable to facilitate the import of database. Like ExcelDataReader provide reader.AsDataSet() · Issue #216 · shps951023/MiniExcel](https://github.com/shps951023/MiniExcel/issues/216) /// diff --git a/tests/MiniExcelTests/MiniExcelTests.csproj b/tests/MiniExcelTests/MiniExcelTests.csproj index 471d9992..c47a8dbf 100644 --- a/tests/MiniExcelTests/MiniExcelTests.csproj +++ b/tests/MiniExcelTests/MiniExcelTests.csproj @@ -24,6 +24,11 @@ + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive +