Skip to content

Commit

Permalink
Merge pull request #160 from WeihanLi/dev
Browse files Browse the repository at this point in the history
3.0.0 preview 2
  • Loading branch information
WeihanLi authored Dec 18, 2024
2 parents 508e8d5 + d1e28bb commit 0813c69
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
1 change: 1 addition & 0 deletions samples/DotNetCoreSample/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
Enabled = true
},
};
await collection1.ToCsvFileAsync($"{nameof(collection1)}.csv");
var collection2 = new[]
{
new TestEntity2()
Expand Down
28 changes: 28 additions & 0 deletions src/WeihanLi.Npoi/CsvHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -610,6 +610,34 @@ public static bool ToCsvFile<TEntity>(this IEnumerable<TEntity> entities, string
return true;
}

public static async Task<bool> ToCsvFileAsync<TEntity>(this IEnumerable<TEntity> entities, string filePath, CsvOptions? csvOptions = null)
{
if (entities is null)
{
throw new ArgumentNullException(nameof(entities));
}

csvOptions ??= CsvOptions.Default;

InternalHelper.EnsureFileIsNotReadOnly(filePath);
var dir = Path.GetDirectoryName(filePath);
if (dir.IsNotNullOrEmpty())
{
if (!Directory.Exists(dir))
{
Directory.CreateDirectory(dir);
}
}

var lines = GetCsvLines(entities, csvOptions);
using var file = File.CreateText(filePath);
foreach (var line in lines)
{
await file.WriteLineAsync(line).ConfigureAwait(false);
}
return true;
}

/// <summary>
/// to csv bytes
/// </summary>
Expand Down

0 comments on commit 0813c69

Please sign in to comment.