Skip to content

Commit

Permalink
feat: add ToCsvFileAsync by TextWriter
Browse files Browse the repository at this point in the history
  • Loading branch information
WeihanLi committed Dec 18, 2024
1 parent 12c085d commit b7dc9f1
Showing 1 changed file with 28 additions and 0 deletions.
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);
}
return true;
}

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

0 comments on commit b7dc9f1

Please sign in to comment.