Skip to content

Commit 9d47bf5

Browse files
Added overload for exporting by path instead of stream
1 parent 4049700 commit 9d47bf5

File tree

3 files changed

+22
-8
lines changed

3 files changed

+22
-8
lines changed

README-V2.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1327,7 +1327,7 @@ registry.Configure<Person>(cfg =>
13271327
});
13281328

13291329
var exporter = MiniExcel.Exporters.GetMappingExporter(registry);
1330-
await exporter.SaveAsAsync(stream, people);
1330+
await exporter.ExportAsync(stream, people);
13311331
```
13321332

13331333
#### 2. Reading with Fluent Mappings
@@ -1411,8 +1411,8 @@ var data = new TestEntity
14111411
Points = 123
14121412
};
14131413

1414-
var exporter = MiniExcel.Exporters.GetMappingExporter(registry);
1415-
await exporter.ApplyTemplateAsync(outputPath, templatePath, new[] { data });
1414+
var termplater = MiniExcel.Templaters.GetMappingExporter(registry);
1415+
await termplater.ApplyTemplateAsync(outputPath, templatePath, new[] { data });
14161416
```
14171417

14181418
#### 6. Advanced: Nested Collections with Item Mapping

src/MiniExcel.Core/Api/OpenXmlExporter.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ internal OpenXmlExporter() { }
1010
public async Task<int> InsertSheetAsync(string path, object value, string? sheetName = "Sheet1", bool printHeader = true, bool overwriteSheet = false, OpenXmlConfiguration? configuration = null, CancellationToken cancellationToken = default)
1111
{
1212
if (Path.GetExtension(path).Equals(".xlsm", StringComparison.InvariantCultureIgnoreCase))
13-
throw new NotSupportedException("MiniExcel's InsertExcelSheet does not support the .xlsm format");
13+
throw new NotSupportedException("MiniExcel's InsertSheet does not support the .xlsm format");
1414

1515
if (!File.Exists(path))
1616
{
@@ -43,7 +43,7 @@ public async Task<int[]> ExportAsync(string path, object value, bool printHeader
4343
CancellationToken cancellationToken = default)
4444
{
4545
if (Path.GetExtension(path).Equals(".xlsm", StringComparison.InvariantCultureIgnoreCase))
46-
throw new NotSupportedException("MiniExcel's ExportExcel does not support the .xlsm format");
46+
throw new NotSupportedException("MiniExcel's Export does not support the .xlsm format");
4747

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

src/MiniExcel.Core/Mapping/MappingExporter.cs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,28 @@
11
namespace MiniExcelLib.Core.Mapping;
22

3-
public sealed partial class MappingExporter()
3+
public sealed partial class MappingExporter
44
{
5-
private readonly MappingRegistry _registry = new();
5+
private readonly MappingRegistry _registry;
66

7-
public MappingExporter(MappingRegistry registry) : this()
7+
public MappingExporter()
8+
{
9+
_registry = new MappingRegistry();
10+
}
11+
12+
public MappingExporter(MappingRegistry registry)
813
{
914
_registry = registry ?? throw new ArgumentNullException(nameof(registry));
1015
}
1116

17+
[CreateSyncVersion]
18+
public async Task ExportAsync<T>(string path, IEnumerable<T>? values, bool overwriteFile = false, CancellationToken cancellationToken = default) where T : class
19+
{
20+
var filePath = path.EndsWith(".xlsx", StringComparison.InvariantCultureIgnoreCase) ? path : $"{path}.xlsx" ;
21+
22+
using var stream = overwriteFile ? File.Create(filePath) : new FileStream(filePath, FileMode.CreateNew);
23+
await ExportAsync(stream, values, cancellationToken).ConfigureAwait(false);
24+
}
25+
1226
[CreateSyncVersion]
1327
public async Task ExportAsync<T>(Stream? stream, IEnumerable<T>? values, CancellationToken cancellationToken = default) where T : class
1428
{

0 commit comments

Comments
 (0)