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
3 changes: 2 additions & 1 deletion V2-Upgrade-Notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@
- If the full MiniExcel package is downloaded, the previous namespace will coexist along the new one, containing the original static methods' signatures, which have become a facade for the aferomentioned providers.
- `IConfiguration` is now `IMiniExcelConfiguration`, but most methods now require the proper implementation (`OpenXmlConfiguration` or `CsvConfiguration`) to be provided rather than the interface
- MiniExcel now fully supports asynchronous streaming the queries,
so the return type for `OpenXmlImporter.QueryAsync` is `IAsyncEnumerable<T>` instead of `Task<IEnumerable<T>>`
so the return type for `OpenXmlImporter.QueryAsync` is `IAsyncEnumerable<T>` instead of `Task<IEnumerable<T>>`
- When applying a template, unlike version 1.x, the flag for overwriting an already existing file must be provided explicitly.
12 changes: 6 additions & 6 deletions src/MiniExcel.OpenXml/Api/OpenXmlTemplater.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,18 @@ public async Task AddPictureAsync(Stream excelStream, CancellationToken cancella
}

[CreateSyncVersion]
public async Task ApplyTemplateAsync(string path, string templatePath, object value,
public async Task ApplyTemplateAsync(string path, string templatePath, object value, bool overwriteFile = false,
OpenXmlConfiguration? configuration = null, CancellationToken cancellationToken = default)
{
using var stream = File.Create(path);
using var stream = overwriteFile ? File.Create(path) : File.Open(path, FileMode.CreateNew);
await ApplyTemplateAsync(stream, templatePath, value, configuration, cancellationToken).ConfigureAwait(false);
}

[CreateSyncVersion]
public async Task ApplyTemplateAsync(string path, Stream templateStream, object value,
public async Task ApplyTemplateAsync(string path, Stream templateStream, object value, bool overwriteFile = false,
OpenXmlConfiguration? configuration = null, CancellationToken cancellationToken = default)
{
using var stream = File.Create(path);
using var stream = overwriteFile ? File.Create(path) : File.Open(path, FileMode.CreateNew);
var template = GetOpenXmlTemplate(stream, configuration);
await template.SaveAsByTemplateAsync(templateStream, value, cancellationToken).ConfigureAwait(false);
}
Expand All @@ -57,10 +57,10 @@ public async Task ApplyTemplateAsync(Stream stream, Stream templateStream, objec
}

[CreateSyncVersion]
public async Task ApplyTemplateAsync(string path, byte[] templateBytes, object value,
public async Task ApplyTemplateAsync(string path, byte[] templateBytes, object value, bool overwriteFile = false,
OpenXmlConfiguration? configuration = null, CancellationToken cancellationToken = default)
{
using var stream = File.Create(path);
using var stream = overwriteFile ? File.Create(path) : File.Open(path, FileMode.CreateNew);
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

For consistency with the other overloads in this file (e.g., lines 30 and 38), it would be better to use new FileStream(path, FileMode.CreateNew) instead of File.Open(path, FileMode.CreateNew). While functionally similar, using the same approach across all methods improves code readability and maintainability.

        using var stream = overwriteFile ? File.Create(path) : new FileStream(path, FileMode.CreateNew);

await ApplyTemplateAsync(stream, templateBytes, value, configuration, cancellationToken).ConfigureAwait(false);
}

Expand Down
6 changes: 3 additions & 3 deletions src/MiniExcel/MiniExcel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -221,11 +221,11 @@ public static IAsyncEnumerable<dynamic> QueryRangeAsync(this Stream stream, bool

[CreateSyncVersion]
public static async Task SaveAsByTemplateAsync(string path, string templatePath, object value, IConfiguration? configuration = null, CancellationToken cancellationToken = default)
=> await ExcelTemplater.ApplyTemplateAsync(path, templatePath, value, configuration as OpenXmlConfiguration, cancellationToken).ConfigureAwait(false);
=> await ExcelTemplater.ApplyTemplateAsync(path, templatePath, value, true, configuration as OpenXmlConfiguration, cancellationToken).ConfigureAwait(false);

[CreateSyncVersion]
public static async Task SaveAsByTemplateAsync(string path, byte[] templateBytes, object value, IConfiguration? configuration = null)
=> await ExcelTemplater.ApplyTemplateAsync(path, templateBytes, value, configuration as OpenXmlConfiguration).ConfigureAwait(false);
=> await ExcelTemplater.ApplyTemplateAsync(path, templateBytes, value, true, configuration as OpenXmlConfiguration).ConfigureAwait(false);

[CreateSyncVersion]
public static async Task SaveAsByTemplateAsync(this Stream stream, string templatePath, object value, IConfiguration? configuration = null)
Expand All @@ -237,7 +237,7 @@ public static async Task SaveAsByTemplateAsync(this Stream stream, byte[] templa

[CreateSyncVersion]
public static async Task SaveAsByTemplateAsync(string path, Stream templateStream, object value, IConfiguration? configuration = null)
=> await ExcelTemplater.ApplyTemplateAsync(path, templateStream, value, configuration as OpenXmlConfiguration).ConfigureAwait(false);
=> await ExcelTemplater.ApplyTemplateAsync(path, templateStream, value, true, configuration as OpenXmlConfiguration).ConfigureAwait(false);

[CreateSyncVersion]
public static async Task SaveAsByTemplateAsync(this Stream stream, Stream templateStream, object value, IConfiguration? configuration = null)
Expand Down
Loading
Loading