diff --git a/src/CsvHelper.Excel.Specs/Common/Helpers.cs b/src/CsvHelper.Excel.Specs/Common/Helpers.cs index 51ba1d4..caf9587 100644 --- a/src/CsvHelper.Excel.Specs/Common/Helpers.cs +++ b/src/CsvHelper.Excel.Specs/Common/Helpers.cs @@ -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) diff --git a/src/CsvHelper.Excel.Specs/CsvHelper.Excel.Specs.csproj b/src/CsvHelper.Excel.Specs/CsvHelper.Excel.Specs.csproj index 1cf116d..23bbbd0 100644 --- a/src/CsvHelper.Excel.Specs/CsvHelper.Excel.Specs.csproj +++ b/src/CsvHelper.Excel.Specs/CsvHelper.Excel.Specs.csproj @@ -1,19 +1,19 @@  - net6.0 + net6.0;net8.0 latest - - - - - - - - - - + + + + + + + + + + runtime; build; native; contentfiles; analyzers; buildtransitive all diff --git a/src/CsvHelper.Excel.Specs/Parser/ExcelParserAsyncSpec.cs b/src/CsvHelper.Excel.Specs/Parser/ExcelParserAsyncSpec.cs index 7e2066d..8066494 100644 --- a/src/CsvHelper.Excel.Specs/Parser/ExcelParserAsyncSpec.cs +++ b/src/CsvHelper.Excel.Specs/Parser/ExcelParserAsyncSpec.cs @@ -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); @@ -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); diff --git a/src/CsvHelper.Excel.Specs/Parser/ParseUsingPathSpecWithBlankRow.cs b/src/CsvHelper.Excel.Specs/Parser/ParseUsingPathSpecWithBlankRow.cs index 1a288e7..7465825 100644 --- a/src/CsvHelper.Excel.Specs/Parser/ParseUsingPathSpecWithBlankRow.cs +++ b/src/CsvHelper.Excel.Specs/Parser/ParseUsingPathSpecWithBlankRow.cs @@ -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); diff --git a/src/CsvHelper.Excel/CsvHelper.Excel.csproj b/src/CsvHelper.Excel/CsvHelper.Excel.csproj index e809ce9..4678b77 100644 --- a/src/CsvHelper.Excel/CsvHelper.Excel.csproj +++ b/src/CsvHelper.Excel/CsvHelper.Excel.csproj @@ -1,8 +1,8 @@ - + An implementation of ICsvParser and ICsvSerializer from CsvHelper that reads and writes using the ClosedXml library. CsvHelper for Excel - 27.2.1 + 33.0.0 Chris Young netstandard2.0;netstandard2.1 portable @@ -15,8 +15,8 @@ ./nupkg - - - + + + \ No newline at end of file diff --git a/src/CsvHelper.Excel/ExcelParser.cs b/src/CsvHelper.Excel/ExcelParser.cs index 4a95186..a151e1b 100644 --- a/src/CsvHelper.Excel/ExcelParser.cs +++ b/src/CsvHelper.Excel/ExcelParser.cs @@ -83,7 +83,7 @@ public ExcelParser(Stream stream, CultureInfo culture, bool leaveOpen = false) : /// The culture. /// true to leave the open after the object is disposed, otherwise false. public ExcelParser(Stream stream, string sheetName, CultureInfo culture, bool leaveOpen = false) : this(stream, - sheetName, new CsvConfiguration(culture) {LeaveOpen= leaveOpen}) + sheetName, new CsvConfiguration(culture), leaveOpen) { } @@ -104,9 +104,20 @@ public ExcelParser(string path, string sheetName, CsvConfiguration configuration /// The stream. /// The sheet name /// The configuration. - 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); + } + + /// + /// Initializes a new instance of the class. + /// + /// The stream. + /// The sheet name + /// The configuration. + /// true to leave the open after the object is disposed, otherwise false. + 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); @@ -123,7 +134,7 @@ public ExcelParser(Stream stream, string sheetName, CsvConfiguration configurati } Context = new CsvContext(this); - _leaveOpen = Configuration.LeaveOpen; + _leaveOpen = leaveOpen; } diff --git a/src/CsvHelper.Excel/ExcelWriter.cs b/src/CsvHelper.Excel/ExcelWriter.cs index a5b8c6a..8df580e 100644 --- a/src/CsvHelper.Excel/ExcelWriter.cs +++ b/src/CsvHelper.Excel/ExcelWriter.cs @@ -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 @@ -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; @@ -86,9 +87,9 @@ public ExcelWriter(Stream stream, CultureInfo culture, bool leaveOpen = false) : /// The culture. /// true to leave the open after the object is disposed, otherwise false. public ExcelWriter(Stream stream, string sheetName, CultureInfo culture, bool leaveOpen = false) : this(stream, - sheetName, new CsvConfiguration(culture) { LeaveOpen = leaveOpen }) + sheetName, new CsvConfiguration(culture), leaveOpen) { - + } /// @@ -97,22 +98,23 @@ public ExcelWriter(Stream stream, string sheetName, CultureInfo culture, bool le /// The stream. /// The sheet name /// The configuration. - private ExcelWriter(Stream stream, string sheetName, CsvConfiguration configuration) : base(TextWriter.Null, + /// true to leave the open after the object is disposed, otherwise false. + + 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; } /// public override void WriteField(string field, bool shouldQuote) { - if (_sanitizeForInjection) + if (Configuration.InjectionOptions != InjectionOptions.None) { field = SanitizeForInjection(field); }