Skip to content

Commit 562fc33

Browse files
Added explicit flag for overwriting result file when applying a template
1 parent 4bba63c commit 562fc33

4 files changed

Lines changed: 216 additions & 216 deletions

File tree

V2-Upgrade-Notes.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,5 @@
1111
- 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.
1212
- `IConfiguration` is now `IMiniExcelConfiguration`, but most methods now require the proper implementation (`OpenXmlConfiguration` or `CsvConfiguration`) to be provided rather than the interface
1313
- MiniExcel now fully supports asynchronous streaming the queries,
14-
so the return type for `OpenXmlImporter.QueryAsync` is `IAsyncEnumerable<T>` instead of `Task<IEnumerable<T>>`
14+
so the return type for `OpenXmlImporter.QueryAsync` is `IAsyncEnumerable<T>` instead of `Task<IEnumerable<T>>`
15+
- When applying a template, unlike version 1.x, the flag for overwriting an already existing file must be provided explicitly.

src/MiniExcel.OpenXml/Api/OpenXmlTemplater.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,18 @@ public async Task AddPictureAsync(Stream excelStream, CancellationToken cancella
2424
}
2525

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

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

5959
[CreateSyncVersion]
60-
public async Task ApplyTemplateAsync(string path, byte[] templateBytes, object value,
60+
public async Task ApplyTemplateAsync(string path, byte[] templateBytes, object value, bool overwriteFile = false,
6161
OpenXmlConfiguration? configuration = null, CancellationToken cancellationToken = default)
6262
{
63-
using var stream = File.Create(path);
63+
using var stream = overwriteFile ? File.Create(path) : File.Open(path, FileMode.CreateNew);
6464
await ApplyTemplateAsync(stream, templateBytes, value, configuration, cancellationToken).ConfigureAwait(false);
6565
}
6666

src/MiniExcel/MiniExcel.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -221,11 +221,11 @@ public static IAsyncEnumerable<dynamic> QueryRangeAsync(this Stream stream, bool
221221

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

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

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

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

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

0 commit comments

Comments
 (0)