Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix empty data reader issue. #629

Merged
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
5 changes: 4 additions & 1 deletion src/MiniExcel/OpenXml/ExcelOpenXmlSheetWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,10 @@ private void GenerateSheetByIDataReader(MiniExcelStreamWriter writer, IDataReade
if (_printHeader)
{
PrintHeader(writer, props);
yIndex++;
if (props.Count > 0)
{
yIndex++;
}
}

while (reader.Read())
Expand Down
14 changes: 14 additions & 0 deletions tests/MiniExcelTests/MiniExcelIssueAsyncTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using MiniExcelLibs.OpenXml;
using MiniExcelLibs.Tests.Utils;
using Newtonsoft.Json;
using NSubstitute;
using OfficeOpenXml;
using System;
using System.Collections.Generic;
Expand Down Expand Up @@ -355,7 +356,7 @@
public async Task Issue233()
{
var path = PathHelper.GetFile("xlsx/TestIssue233.xlsx");
var dt = await MiniExcel.QueryAsDataTableAsync(path);

Check warning on line 359 in tests/MiniExcelTests/MiniExcelIssueAsyncTests.cs

View workflow job for this annotation

GitHub Actions / build

'MiniExcel.QueryAsDataTableAsync(string, bool, string, ExcelType, string, IConfiguration, CancellationToken)' is obsolete: 'QueryAsDataTable is not recommended, because it'll load all data into memory.'
var rows = dt.Rows;

Assert.Equal(0.55, rows[0]["Size"]);
Expand Down Expand Up @@ -484,7 +485,7 @@
public async Task Issue229()
{
var path = PathHelper.GetFile("xlsx/TestIssue229.xlsx");
var dt = await MiniExcel.QueryAsDataTableAsync(path);

Check warning on line 488 in tests/MiniExcelTests/MiniExcelIssueAsyncTests.cs

View workflow job for this annotation

GitHub Actions / build

'MiniExcel.QueryAsDataTableAsync(string, bool, string, ExcelType, string, IConfiguration, CancellationToken)' is obsolete: 'QueryAsDataTable is not recommended, because it'll load all data into memory.'
foreach (DataColumn column in dt.Columns)
{
var v = dt.Rows[3][column];
Expand Down Expand Up @@ -608,7 +609,7 @@
var path = PathHelper.GetTempPath();
MiniExcel.SaveAs(path, value);

var dt = await MiniExcel.QueryAsDataTableAsync(path);

Check warning on line 612 in tests/MiniExcelTests/MiniExcelIssueAsyncTests.cs

View workflow job for this annotation

GitHub Actions / build

'MiniExcel.QueryAsDataTableAsync(string, bool, string, ExcelType, string, IConfiguration, CancellationToken)' is obsolete: 'QueryAsDataTable is not recommended, because it'll load all data into memory.'
var columns = dt.Columns;
Assert.Equal(typeof(object), columns[0].DataType);
Assert.Equal(typeof(object), columns[1].DataType);
Expand Down Expand Up @@ -705,6 +706,19 @@
}
}

[Fact]
public async Task EmptyDataReaderIssue()
{
var path = PathHelper.GetTempPath();

var reader = Substitute.For<IDataReader>();
MiniExcel.SaveAs(path, reader, overwriteFile: true);

var q = await MiniExcel.QueryAsync(path, true);
var rows = q.ToList();
Assert.Empty(rows);
}

/// <summary>
/// [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)
/// </summary>
Expand All @@ -716,7 +730,7 @@
MiniExcel.SaveAs(path, value);

{
var table = await MiniExcel.QueryAsDataTableAsync(path);

Check warning on line 733 in tests/MiniExcelTests/MiniExcelIssueAsyncTests.cs

View workflow job for this annotation

GitHub Actions / build

'MiniExcel.QueryAsDataTableAsync(string, bool, string, ExcelType, string, IConfiguration, CancellationToken)' is obsolete: 'QueryAsDataTable is not recommended, because it'll load all data into memory.'
var columns = table.Columns;
Assert.Equal("Test1", table.Columns[0].ColumnName);
Assert.Equal("Test2", table.Columns[1].ColumnName);
Expand All @@ -727,7 +741,7 @@
}

{
var dt = await MiniExcel.QueryAsDataTableAsync(path, false);

Check warning on line 744 in tests/MiniExcelTests/MiniExcelIssueAsyncTests.cs

View workflow job for this annotation

GitHub Actions / build

'MiniExcel.QueryAsDataTableAsync(string, bool, string, ExcelType, string, IConfiguration, CancellationToken)' is obsolete: 'QueryAsDataTable is not recommended, because it'll load all data into memory.'
Assert.Equal("Test1", dt.Rows[0]["A"]);
Assert.Equal("Test2", dt.Rows[0]["B"]);
Assert.Equal("1", dt.Rows[1]["A"]);
Expand Down
5 changes: 5 additions & 0 deletions tests/MiniExcelTests/MiniExcelTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@
<PackageReference Include="ExcelDataReader.DataSet" Version="3.6.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.2" />
<PackageReference Include="NPOI" Version="2.7.0" />
<PackageReference Include="NSubstitute" Version="5.1.0" />
<PackageReference Include="NSubstitute.Analyzers.CSharp" Version="1.0.17">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="System.Data.SQLite.Core" Version="1.0.115.5" />
<PackageReference Include="System.Text.Encoding.CodePages" Version="5.0.0" />

Expand Down
Loading