Skip to content
Open
4 changes: 2 additions & 2 deletions src/CsvHelper.Excel.Specs/Common/Helpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ public static XLWorkbook GetOrCreateWorkbook(string path, string worksheetName)
{
if (!File.Exists(path))
{
var workbook = new XLWorkbook(XLEventTracking.Disabled);
var workbook = new XLWorkbook();
workbook.GetOrAddWorksheet(worksheetName);
workbook.SaveAs(path);
return workbook;
}
return new XLWorkbook(path, XLEventTracking.Disabled);
return new XLWorkbook(path);
}

public static IXLWorksheet GetOrAddWorksheet(this XLWorkbook workbook, string sheetName)
Expand Down
4 changes: 2 additions & 2 deletions src/CsvHelper.Excel.Specs/Common/Person.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ namespace CsvHelper.Excel.Specs.Common
{
public class Person
{
public string Name { get; set; }
public string? Name { get; set; }

public int Age { get; set; }
public int? Age { get; set; }
}
}
15 changes: 7 additions & 8 deletions src/CsvHelper.Excel.Specs/CsvHelper.Excel.Specs.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@
<LangVersion>latest</LangVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="ClosedXML" Version="0.95.4" />
<PackageReference Include="CsvHelper" Version="27.2.1" />
<PackageReference Include="DocumentFormat.OpenXml" Version="2.15.0" />
<PackageReference Include="FluentAssertions" Version="6.3.0" />
<PackageReference Include="Microsoft.Bcl.AsyncInterfaces" Version="6.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.0.0" />
<PackageReference Include="Microsoft.TestPlatform.ObjectModel" Version="17.0.0" />
<PackageReference Include="System.Linq.Async" Version="5.1.0" />
<PackageReference Include="ClosedXML" Version="0.102.2" />
<PackageReference Include="CsvHelper" Version="31.0.3" />
<PackageReference Include="FluentAssertions" Version="6.12.0" />
<PackageReference Include="Microsoft.Bcl.AsyncInterfaces" Version="8.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
<PackageReference Include="Microsoft.TestPlatform.ObjectModel" Version="17.9.0" />
<PackageReference Include="System.Linq.Async" Version="6.0.1" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand Down
9 changes: 6 additions & 3 deletions src/CsvHelper.Excel.Specs/Parser/ExcelParserAsyncSpec.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
Expand Down Expand Up @@ -83,16 +84,18 @@ protected async Task RunAsync()
{
var csvConfiguration = new CsvConfiguration(CultureInfo.InvariantCulture)
{
ShouldSkipRecord = record => record.Record.All(string.IsNullOrEmpty)
ShouldSkipRecord = x => x.Row.Parser.Record?.All(field => string.IsNullOrWhiteSpace(field)) ?? false
};
using var parser = new ExcelParser(Path, WorksheetName, csvConfiguration);
using var reader = new CsvReader(parser);

reader.Context.AutoMap<Person>();

var records = reader.GetRecordsAsync<Person>();

Results = await records.ToArrayAsync();
}

[Fact]
public async void TheResultsAreNotNull()
{
Expand All @@ -112,7 +115,7 @@ public async void RowsHaveData()
{
var csvConfiguration = new CsvConfiguration(CultureInfo.InvariantCulture)
{
ShouldSkipRecord = record => record.Record.All(string.IsNullOrEmpty)
ShouldSkipRecord = x => x.Row.Parser.Record?.All(field => string.IsNullOrWhiteSpace(field)) ?? false
};
using var parser = new ExcelParser(Path, WorksheetName, csvConfiguration );
using var reader = new CsvReader(parser);
Expand Down
2 changes: 1 addition & 1 deletion src/CsvHelper.Excel.Specs/Parser/ExcelParserSpec.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ protected void Run(ExcelParser parser)
using var reader = new CsvReader(parser);

reader.Context.AutoMap<Person>();
var records = reader.GetRecords<Person>();
var records = reader.GetRecords<Person>().ToList();
Results = records.ToArray();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public ParseUsingPathSpecWithBlankRow() : base("parse_by_path_with_blank_row", i
{
var csvConfiguration = new CsvConfiguration(CultureInfo.InvariantCulture)
{
ShouldSkipRecord = record => record.Record.All(string.IsNullOrEmpty)
ShouldSkipRecord = x => x.Row.Parser.Record?.All(field => string.IsNullOrWhiteSpace(field)) ?? false
};
using var parser = new ExcelParser(Path, null, csvConfiguration);
Run(parser);
Expand Down
4 changes: 0 additions & 4 deletions src/CsvHelper.Excel.sln.DotSettings.user

This file was deleted.

7 changes: 3 additions & 4 deletions src/CsvHelper.Excel/CsvHelper.Excel.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<PropertyGroup>
<Description>An implementation of ICsvParser and ICsvSerializer from CsvHelper that reads and writes using the ClosedXml library.</Description>
<AssemblyTitle>CsvHelper for Excel</AssemblyTitle>
<VersionPrefix>27.2.1</VersionPrefix>
<VersionPrefix>31.0.3</VersionPrefix>
<Authors>Chris Young</Authors>
<TargetFrameworks>netstandard2.0;netstandard2.1</TargetFrameworks>
<DebugType>portable</DebugType>
Expand All @@ -15,8 +15,7 @@
<PackageOutputPath>./nupkg</PackageOutputPath>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="ClosedXML" Version="0.95.4" />
<PackageReference Include="CsvHelper" Version="27.2.1" />
<PackageReference Include="DocumentFormat.OpenXml" Version="2.15.0" />
<PackageReference Include="ClosedXML" Version="0.102.2" />
<PackageReference Include="CsvHelper" Version="31.0.3" />
</ItemGroup>
</Project>
8 changes: 4 additions & 4 deletions src/CsvHelper.Excel/ExcelParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public ExcelParser(Stream stream, CultureInfo culture, bool leaveOpen = false) :
/// <param name="culture">The culture.</param>
/// <param name="leaveOpen"><c>true</c> to leave the <see cref="TextWriter"/> open after the <see cref="ExcelParser"/> object is disposed, otherwise <c>false</c>.</param>
public ExcelParser(Stream stream, string sheetName, CultureInfo culture, bool leaveOpen = false) : this(stream,
sheetName, new CsvConfiguration(culture) {LeaveOpen= leaveOpen})
sheetName, new CsvConfiguration(culture), leaveOpen)
{
}

Expand All @@ -104,9 +104,9 @@ public ExcelParser(string path, string sheetName, CsvConfiguration configuration
/// <param name="stream">The stream.</param>
/// <param name="sheetName">The sheet name</param>
/// <param name="configuration">The configuration.</param>
public ExcelParser(Stream stream, string sheetName, CsvConfiguration configuration)
public ExcelParser(Stream stream, string sheetName, CsvConfiguration configuration, bool leaveOpen = false)
{
var workbook = new XLWorkbook(stream, XLEventTracking.Disabled);
var workbook = new XLWorkbook(stream);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the input file is not an Excel file, but csv file. it will throw an exception
I thought the orignal idea was to support both csv and excel

Copy link
Author

@hurles hurles Apr 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The original would have thrown an exception there as well.
This issue is probably better suited for a different PR. This PR merely updates the ClosedXml and CsvHelper libraries, with fixes to some breaking changes.


_worksheet = string.IsNullOrEmpty(sheetName) ? workbook.Worksheet(1) : workbook.Worksheet(sheetName);

Expand All @@ -123,7 +123,7 @@ public ExcelParser(Stream stream, string sheetName, CsvConfiguration configurati
}

Context = new CsvContext(this);
_leaveOpen = Configuration.LeaveOpen;
_leaveOpen = leaveOpen;
}


Expand Down
12 changes: 6 additions & 6 deletions src/CsvHelper.Excel/ExcelWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public ExcelWriter(Stream stream, CultureInfo culture, bool leaveOpen = false) :
/// <param name="culture">The culture.</param>
/// <param name="leaveOpen"><c>true</c> to leave the <see cref="TextWriter"/> open after the <see cref="ExcelWriter"/> object is disposed, otherwise <c>false</c>.</param>
public ExcelWriter(Stream stream, string sheetName, CultureInfo culture, bool leaveOpen = false) : this(stream,
sheetName, new CsvConfiguration(culture) { LeaveOpen = leaveOpen })
sheetName, new CsvConfiguration(culture), leaveOpen)
{

}
Expand All @@ -97,15 +97,15 @@ public ExcelWriter(Stream stream, string sheetName, CultureInfo culture, bool le
/// <param name="stream">The stream.</param>
/// <param name="sheetName">The sheet name</param>
/// <param name="configuration">The configuration.</param>
private ExcelWriter(Stream stream, string sheetName, CsvConfiguration configuration) : base(TextWriter.Null,
configuration)
private ExcelWriter(Stream stream, string sheetName, CsvConfiguration configuration, bool leaveOpen = false) : base(TextWriter.Null,
configuration, leaveOpen)
{
configuration.Validate();
_worksheet = new XLWorkbook(XLEventTracking.Disabled).AddWorksheet(sheetName);
_worksheet = new XLWorkbook().AddWorksheet(sheetName);
this._stream = stream;

_leaveOpen = configuration.LeaveOpen;
_sanitizeForInjection = configuration.SanitizeForInjection;
_leaveOpen = leaveOpen;
_sanitizeForInjection = configuration.InjectionOptions == InjectionOptions.Escape;
}


Expand Down