Skip to content
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
4 changes: 2 additions & 2 deletions src/MiniExcel.Core/Abstractions/IMiniExcelWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
public partial interface IMiniExcelWriter
{
[CreateSyncVersion]
Task<int[]> SaveAsAsync(CancellationToken cancellationToken = default);
Task<int[]> SaveAsAsync(IProgress<int>? progress = null, CancellationToken cancellationToken = default);

[CreateSyncVersion]
Task<int> InsertAsync(bool overwriteSheet = false, CancellationToken cancellationToken = default);
Task<int> InsertAsync(bool overwriteSheet = false, IProgress<int>? progress = null, CancellationToken cancellationToken = default);
}
22 changes: 12 additions & 10 deletions src/MiniExcel.Core/Api/OpenXmlExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ internal OpenXmlExporter() { }


[CreateSyncVersion]
public async Task<int> InsertSheetAsync(string path, object value, string? sheetName = "Sheet1", bool printHeader = true, bool overwriteSheet = false, OpenXmlConfiguration? configuration = null, CancellationToken cancellationToken = default)
public async Task<int> InsertSheetAsync(string path, object value, string? sheetName = "Sheet1",
bool printHeader = true, bool overwriteSheet = false, OpenXmlConfiguration? configuration = null,
IProgress<int>? progress = null, CancellationToken cancellationToken = default)
{
if (Path.GetExtension(path).Equals(".xlsm", StringComparison.InvariantCultureIgnoreCase))
throw new NotSupportedException("MiniExcel's InsertExcelSheet does not support the .xlsm format");
throw new NotSupportedException("MiniExcel's InsertSheet does not support the .xlsm format");

if (!File.Exists(path))
{
Expand All @@ -19,13 +21,13 @@ public async Task<int> InsertSheetAsync(string path, object value, string? sheet
}

using var stream = new FileStream(path, FileMode.Open, FileAccess.ReadWrite, FileShare.Read, 4096, FileOptions.SequentialScan);
return await InsertSheetAsync(stream, value, sheetName, printHeader, overwriteSheet, configuration, cancellationToken).ConfigureAwait(false);
return await InsertSheetAsync(stream, value, sheetName, printHeader, overwriteSheet, configuration, progress, cancellationToken).ConfigureAwait(false);
}

[CreateSyncVersion]
public async Task<int> InsertSheetAsync(Stream stream, object value, string? sheetName = "Sheet1",
bool printHeader = true, bool overwriteSheet = false, OpenXmlConfiguration? configuration = null,
CancellationToken cancellationToken = default)
IProgress<int>? progress = null, CancellationToken cancellationToken = default)
{
stream.Seek(0, SeekOrigin.End);
configuration ??= new OpenXmlConfiguration { FastMode = true };
Expand All @@ -34,31 +36,31 @@ public async Task<int> InsertSheetAsync(Stream stream, object value, string? she
.CreateAsync(stream, value, sheetName, printHeader, configuration, cancellationToken)
.ConfigureAwait(false);

return await writer.InsertAsync(overwriteSheet, cancellationToken).ConfigureAwait(false);
return await writer.InsertAsync(overwriteSheet, cancellationToken: cancellationToken).ConfigureAwait(false);
}

[CreateSyncVersion]
public async Task<int[]> ExportAsync(string path, object value, bool printHeader = true,
string? sheetName = "Sheet1", bool overwriteFile = false, OpenXmlConfiguration? configuration = null,
CancellationToken cancellationToken = default)
IProgress<int>? progress = null, CancellationToken cancellationToken = default)
{
if (Path.GetExtension(path).Equals(".xlsm", StringComparison.InvariantCultureIgnoreCase))
throw new NotSupportedException("MiniExcel's ExportExcel does not support the .xlsm format");
throw new NotSupportedException("MiniExcel's Export does not support the .xlsm format");

var filePath = path.EndsWith(".xlsx", StringComparison.InvariantCultureIgnoreCase) ? path : $"{path}.xlsx" ;

using var stream = overwriteFile ? File.Create(filePath) : new FileStream(filePath, FileMode.CreateNew);
return await ExportAsync(stream, value, printHeader, sheetName, configuration, cancellationToken).ConfigureAwait(false);
return await ExportAsync(stream, value, printHeader, sheetName, configuration, progress, cancellationToken).ConfigureAwait(false);
}

[CreateSyncVersion]
public async Task<int[]> ExportAsync(Stream stream, object value, bool printHeader = true, string? sheetName = "Sheet1",
OpenXmlConfiguration? configuration = null, CancellationToken cancellationToken = default)
OpenXmlConfiguration? configuration = null, IProgress<int>? progress = null, CancellationToken cancellationToken = default)
{
var writer = await OpenXmlWriter
.CreateAsync(stream, value, sheetName, printHeader, configuration, cancellationToken)
.ConfigureAwait(false);

return await writer.SaveAsAsync(cancellationToken).ConfigureAwait(false);
return await writer.SaveAsAsync(progress, cancellationToken).ConfigureAwait(false);
}
}
18 changes: 10 additions & 8 deletions src/MiniExcel.Core/OpenXml/OpenXmlWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ internal static Task<OpenXmlWriter> CreateAsync(Stream stream, object? value, st
}

[CreateSyncVersion]
public async Task<int[]> SaveAsAsync(CancellationToken cancellationToken = default)
public async Task<int[]> SaveAsAsync(IProgress<int>? progress = null, CancellationToken cancellationToken = default)
{
try
{
Expand All @@ -69,7 +69,7 @@ public async Task<int[]> SaveAsAsync(CancellationToken cancellationToken = defau

_sheets.Add(sheet.Item1); //TODO:remove
_currentSheetIndex = sheet.Item1.SheetIdx;
var rows = await CreateSheetXmlAsync(sheet.Item2, sheet.Item1.Path, cancellationToken).ConfigureAwait(false);
var rows = await CreateSheetXmlAsync(sheet.Item2, sheet.Item1.Path, progress, cancellationToken).ConfigureAwait(false);
rowsWritten.Add(rows);
}

Expand All @@ -87,7 +87,7 @@ public async Task<int[]> SaveAsAsync(CancellationToken cancellationToken = defau
}

[CreateSyncVersion]
public async Task<int> InsertAsync(bool overwriteSheet = false, CancellationToken cancellationToken = default)
public async Task<int> InsertAsync(bool overwriteSheet = false, IProgress<int>? progress = null, CancellationToken cancellationToken = default)
{
try
{
Expand Down Expand Up @@ -123,13 +123,13 @@ public async Task<int> InsertAsync(bool overwriteSheet = false, CancellationToke
var insertSheetInfo = GetSheetInfos(_defaultSheetName);
var insertSheetDto = insertSheetInfo.ToDto(_currentSheetIndex);
_sheets.Add(insertSheetDto);
rowsWritten = await CreateSheetXmlAsync(_value, insertSheetDto.Path, cancellationToken).ConfigureAwait(false);
rowsWritten = await CreateSheetXmlAsync(_value, insertSheetDto.Path, progress, cancellationToken).ConfigureAwait(false);
}
else
{
_currentSheetIndex = existSheetDto.SheetIdx;
_archive.Entries.Single(s => s.FullName == existSheetDto.Path).Delete();
rowsWritten = await CreateSheetXmlAsync(_value, existSheetDto.Path, cancellationToken).ConfigureAwait(false);
rowsWritten = await CreateSheetXmlAsync(_value, existSheetDto.Path, progress, cancellationToken).ConfigureAwait(false);
}

await AddFilesToZipAsync(cancellationToken).ConfigureAwait(false);
Expand Down Expand Up @@ -177,7 +177,7 @@ internal async Task GenerateDefaultOpenXmlAsync(CancellationToken cancellationTo
}

[CreateSyncVersion]
private async Task<int> CreateSheetXmlAsync(object? values, string sheetPath, CancellationToken cancellationToken)
private async Task<int> CreateSheetXmlAsync(object? values, string sheetPath, IProgress<int>? progress, CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();

Expand All @@ -197,7 +197,7 @@ private async Task<int> CreateSheetXmlAsync(object? values, string sheetPath, Ca
}
else
{
rowsWritten = await WriteValuesAsync(writer, values, cancellationToken).ConfigureAwait(false);
rowsWritten = await WriteValuesAsync(writer, values, cancellationToken, progress).ConfigureAwait(false);
}

_zipDictionary.Add(sheetPath, new ZipPackageInfo(entry, ExcelContentTypes.Worksheet));
Expand Down Expand Up @@ -232,7 +232,7 @@ private static async Task WriteDimensionAsync(SafeStreamWriter writer, int maxRo
}

[CreateSyncVersion]
private async Task<int> WriteValuesAsync(SafeStreamWriter writer, object values, CancellationToken cancellationToken)
private async Task<int> WriteValuesAsync(SafeStreamWriter writer, object values, CancellationToken cancellationToken, IProgress<int>? progress = null)
{
cancellationToken.ThrowIfCancellationRequested();

Expand Down Expand Up @@ -313,6 +313,7 @@ private async Task<int> WriteValuesAsync(SafeStreamWriter writer, object values,
{
cancellationToken.ThrowIfCancellationRequested();
await WriteCellAsync(writer, currentRowIndex, cellValue.CellIndex, cellValue.Value, cellValue.Prop, widths).ConfigureAwait(false);
progress?.Report(1);
}
await writer.WriteAsync(WorksheetXml.EndRow, cancellationToken).ConfigureAwait(false);
}
Expand All @@ -328,6 +329,7 @@ private async Task<int> WriteValuesAsync(SafeStreamWriter writer, object values,
await foreach (var cellValue in row.ConfigureAwait(false).WithCancellation(cancellationToken))
{
await WriteCellAsync(writer, currentRowIndex, cellValue.CellIndex, cellValue.Value, cellValue.Prop, widths).ConfigureAwait(false);
progress?.Report(1);
}
await writer.WriteAsync(WorksheetXml.EndRow, cancellationToken).ConfigureAwait(false);
}
Expand Down
22 changes: 11 additions & 11 deletions src/MiniExcel.Csv/Api/CsvExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,43 +12,43 @@ internal CsvExporter() { }

[CreateSyncVersion]
public async Task<int> AppendAsync(string path, object value, bool printHeader = true,
CsvConfiguration? configuration = null, CancellationToken cancellationToken = default)
CsvConfiguration? configuration = null, IProgress<int>? progress = null, CancellationToken cancellationToken = default)
{
if (!File.Exists(path))
{
var rowsWritten = await ExportAsync(path, value, printHeader, false, configuration, cancellationToken: cancellationToken).ConfigureAwait(false);
var rowsWritten = await ExportAsync(path, value, printHeader, false, configuration, progress, cancellationToken).ConfigureAwait(false);
return rowsWritten.FirstOrDefault();
}

using var stream = new FileStream(path, FileMode.Append, FileAccess.Write, FileShare.Read, 4096, FileOptions.SequentialScan);
return await AppendAsync(stream, value, configuration, cancellationToken).ConfigureAwait(false);
return await AppendAsync(stream, value, configuration, progress, cancellationToken).ConfigureAwait(false);
}

[CreateSyncVersion]
public async Task<int> AppendAsync(Stream stream, object value, CsvConfiguration? configuration = null, CancellationToken cancellationToken = default)
public async Task<int> AppendAsync(Stream stream, object value, CsvConfiguration? configuration = null, IProgress<int>? progress = null, CancellationToken cancellationToken = default)
{
stream.Seek(0, SeekOrigin.End);

var newValue = value is IEnumerable or IDataReader ? value : new[] { value };

using var writer = new CsvWriter(stream, newValue, false, configuration);
return await writer.InsertAsync(false, cancellationToken).ConfigureAwait(false);
return await writer.InsertAsync(false, progress, cancellationToken).ConfigureAwait(false);
}

[CreateSyncVersion]
public async Task<int[]> ExportAsync(string path, object value, bool printHeader = true, bool overwriteFile = false,
CsvConfiguration? configuration = null, CancellationToken cancellationToken = default)
public async Task<int[]> ExportAsync(string path, object value, bool printHeader = true, bool overwriteFile = false,
CsvConfiguration? configuration = null, IProgress<int>? progress = null, CancellationToken cancellationToken = default)
{
using var stream = overwriteFile ? File.Create(path) : new FileStream(path, FileMode.CreateNew);
return await ExportAsync(stream, value, printHeader, configuration, cancellationToken).ConfigureAwait(false);
return await ExportAsync(stream, value, printHeader, configuration, progress, cancellationToken).ConfigureAwait(false);
}

[CreateSyncVersion]
public async Task<int[]> ExportAsync(Stream stream, object value, bool printHeader = true,
CsvConfiguration? configuration = null, CancellationToken cancellationToken = default)
public async Task<int[]> ExportAsync(Stream stream, object value, bool printHeader = true,
CsvConfiguration? configuration = null, IProgress<int>? progress = null, CancellationToken cancellationToken = default)
{
using var writer = new CsvWriter(stream, value, printHeader, configuration);
return await writer.SaveAsAsync(cancellationToken).ConfigureAwait(false);
return await writer.SaveAsAsync(progress, cancellationToken).ConfigureAwait(false);
}

#endregion
Expand Down
13 changes: 8 additions & 5 deletions src/MiniExcel.Csv/CsvWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ private static void RemoveTrailingSeparator(StringBuilder rowBuilder)
}

[CreateSyncVersion]
private async Task<int> WriteValuesAsync(StreamWriter writer, object values, string separator, string newLine, CancellationToken cancellationToken = default)
private async Task<int> WriteValuesAsync(StreamWriter writer, object values, string separator, string newLine,
IProgress<int>? progress = null, CancellationToken cancellationToken = default)
{
cancellationToken.ThrowIfCancellationRequested();

Expand Down Expand Up @@ -96,6 +97,7 @@ await _writer.WriteAsync(newLine
{
cancellationToken.ThrowIfCancellationRequested();
AppendColumn(rowBuilder, column);
progress?.Report(1);
}

RemoveTrailingSeparator(rowBuilder);
Expand Down Expand Up @@ -124,6 +126,7 @@ await _writer.WriteAsync(newLine
await foreach (var column in row.WithCancellation(cancellationToken).ConfigureAwait(false))
{
AppendColumn(rowBuilder, column);
progress?.Report(1);
}

RemoveTrailingSeparator(rowBuilder);
Expand All @@ -146,7 +149,7 @@ await _writer.WriteAsync(newLine
}

[CreateSyncVersion]
public async Task<int[]> SaveAsAsync(CancellationToken cancellationToken = default)
public async Task<int[]> SaveAsAsync(IProgress<int>? progress = null, CancellationToken cancellationToken = default)
{
cancellationToken.ThrowIfCancellationRequested();

Expand All @@ -168,7 +171,7 @@ await _writer.FlushAsync(
return [];
}

var rowsWritten = await WriteValuesAsync(_writer, _value, seperator, newLine, cancellationToken).ConfigureAwait(false);
var rowsWritten = await WriteValuesAsync(_writer, _value, seperator, newLine, progress, cancellationToken).ConfigureAwait(false);
await _writer.FlushAsync(
#if NET5_0_OR_GREATER
cancellationToken
Expand All @@ -179,9 +182,9 @@ await _writer.FlushAsync(
}

[CreateSyncVersion]
public async Task<int> InsertAsync(bool overwriteSheet = false, CancellationToken cancellationToken = default)
public async Task<int> InsertAsync(bool overwriteSheet = false, IProgress<int>? progress = null, CancellationToken cancellationToken = default)
{
var rowsWritten = await SaveAsAsync(cancellationToken).ConfigureAwait(false);
var rowsWritten = await SaveAsAsync(progress, cancellationToken).ConfigureAwait(false);
return rowsWritten.FirstOrDefault();
}

Expand Down
Loading
Loading