Skip to content
Open
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
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
22 changes: 11 additions & 11 deletions src/CsvHelper.Excel.Specs/CsvHelper.Excel.Specs.csproj
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFrameworks>net6.0;net8.0</TargetFrameworks>
<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="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<PackageReference Include="ClosedXML" Version="0.102.3" />
<PackageReference Include="CsvHelper" Version="33.0.1" />
<PackageReference Include="DocumentFormat.OpenXml" Version="2.20.0" />
<PackageReference Include="FluentAssertions" Version="6.12.1" />
<PackageReference Include="Microsoft.Bcl.AsyncInterfaces" Version="8.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
<PackageReference Include="Microsoft.TestPlatform.ObjectModel" Version="17.11.1" />
<PackageReference Include="System.Linq.Async" Version="6.0.1" />
<PackageReference Include="xunit" Version="2.9.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
Expand Down
10 changes: 5 additions & 5 deletions src/CsvHelper.Excel.Specs/Parser/ExcelParserAsyncSpec.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ protected async Task RunAsync()
{
var csvConfiguration = new CsvConfiguration(CultureInfo.InvariantCulture)
{
ShouldSkipRecord = record => record.Record.All(string.IsNullOrEmpty)
ShouldSkipRecord = args => args.Row.Parser.Record?.All(string.IsNullOrWhiteSpace) ?? false,
};
using var parser = new ExcelParser(Path, WorksheetName, csvConfiguration);
using var reader = new CsvReader(parser);
Expand All @@ -94,25 +94,25 @@ protected async Task RunAsync()
}

[Fact]
public async void TheResultsAreNotNull()
public async Task TheResultsAreNotNull()
{
await RunAsync();
Results.Should().NotBeNull();
}

[Fact]
public async void TheResultsAreCorrect()
public async Task TheResultsAreCorrect()
{
await RunAsync();
Values.Should().BeEquivalentTo(Results, options => options.IncludingProperties());
}

[Fact]
public async void RowsHaveData()
public async Task RowsHaveData()
{
var csvConfiguration = new CsvConfiguration(CultureInfo.InvariantCulture)
{
ShouldSkipRecord = record => record.Record.All(string.IsNullOrEmpty)
ShouldSkipRecord = args => args.Row.Parser.Record?.All(string.IsNullOrWhiteSpace) ?? false,
};
using var parser = new ExcelParser(Path, WorksheetName, csvConfiguration );
using var reader = new CsvReader(parser);
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 = args => args.Row.Parser.Record?.All(string.IsNullOrWhiteSpace) ?? false,
};
using var parser = new ExcelParser(Path, null, csvConfiguration);
Run(parser);
Expand Down
10 changes: 5 additions & 5 deletions src/CsvHelper.Excel/CsvHelper.Excel.csproj
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<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>33.0.0</VersionPrefix>
<Authors>Chris Young</Authors>
<TargetFrameworks>netstandard2.0;netstandard2.1</TargetFrameworks>
<DebugType>portable</DebugType>
Expand All @@ -15,8 +15,8 @@
<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.3" />
<PackageReference Include="CsvHelper" Version="33.0.1" />
<PackageReference Include="DocumentFormat.OpenXml" Version="2.20.0" />
</ItemGroup>
</Project>
19 changes: 15 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,20 @@ 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) : this(stream, sheetName, configuration, false)
{
var workbook = new XLWorkbook(stream, XLEventTracking.Disabled);
}

/// <summary>
/// Initializes a new instance of the <see cref="ExcelParser"/> class.
/// </summary>
/// <param name="stream">The stream.</param>
/// <param name="sheetName">The sheet name</param>
/// <param name="configuration">The configuration.</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, CsvConfiguration configuration, bool leaveOpen)
{
var workbook = new XLWorkbook(stream);

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

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

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


Expand Down
18 changes: 10 additions & 8 deletions src/CsvHelper.Excel/ExcelWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
using System.IO;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;

using ClosedXML.Excel;

using CsvHelper.Configuration;

#pragma warning disable 649
Expand All @@ -16,7 +18,6 @@ namespace CsvHelper.Excel
public class ExcelWriter : CsvWriter
{
private readonly bool _leaveOpen;
private readonly bool _sanitizeForInjection;

private bool _disposed;
private int _row = 1;
Expand Down Expand Up @@ -86,9 +87,9 @@ 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)
{

}

/// <summary>
Expand All @@ -97,22 +98,23 @@ 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,
/// <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>

private ExcelWriter(Stream stream, string sheetName, CsvConfiguration configuration, bool leaveOpen = false) : base(TextWriter.Null,
configuration)
{
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;
}


/// <inheritdoc/>
public override void WriteField(string field, bool shouldQuote)
{
if (_sanitizeForInjection)
if (Configuration.InjectionOptions != InjectionOptions.None)
{
field = SanitizeForInjection(field);
}
Expand Down