Skip to content

Commit 0813c69

Browse files
authored
Merge pull request #160 from WeihanLi/dev
3.0.0 preview 2
2 parents 508e8d5 + d1e28bb commit 0813c69

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

samples/DotNetCoreSample/Program.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
Enabled = true
3636
},
3737
};
38+
await collection1.ToCsvFileAsync($"{nameof(collection1)}.csv");
3839
var collection2 = new[]
3940
{
4041
new TestEntity2()

src/WeihanLi.Npoi/CsvHelper.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -610,6 +610,34 @@ public static bool ToCsvFile<TEntity>(this IEnumerable<TEntity> entities, string
610610
return true;
611611
}
612612

613+
public static async Task<bool> ToCsvFileAsync<TEntity>(this IEnumerable<TEntity> entities, string filePath, CsvOptions? csvOptions = null)
614+
{
615+
if (entities is null)
616+
{
617+
throw new ArgumentNullException(nameof(entities));
618+
}
619+
620+
csvOptions ??= CsvOptions.Default;
621+
622+
InternalHelper.EnsureFileIsNotReadOnly(filePath);
623+
var dir = Path.GetDirectoryName(filePath);
624+
if (dir.IsNotNullOrEmpty())
625+
{
626+
if (!Directory.Exists(dir))
627+
{
628+
Directory.CreateDirectory(dir);
629+
}
630+
}
631+
632+
var lines = GetCsvLines(entities, csvOptions);
633+
using var file = File.CreateText(filePath);
634+
foreach (var line in lines)
635+
{
636+
await file.WriteLineAsync(line).ConfigureAwait(false);
637+
}
638+
return true;
639+
}
640+
613641
/// <summary>
614642
/// to csv bytes
615643
/// </summary>

0 commit comments

Comments
 (0)