Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@

<ItemGroup>
<PackageReference Include="BlazorComponentBus" Version="2.2.0" />
<PackageReference Include="Blazored.LocalStorage" Version="4.5.0" />
<PackageReference Include="Blazored.LocalStorage" Version="4.5.0" />
<PackageReference Include="CsvHelper" Version="33.1.0" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net8.0'">
Expand Down
20 changes: 20 additions & 0 deletions src/AstroPanda.Blazor.Toolkit/Services/CSVDownloadService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System.Text;

namespace AstroPanda.Blazor.Toolkit.Services;

public class CSVDownloadService<T>(IDownloadService _downloads) : ICSVDownloadService<T>
{
public async Task DownloadLocalAsync(string fileName, IEnumerable<T> collection)
{
using var stringWriter = new StringWriter();
using var csvWriter = new CsvHelper.CsvWriter(stringWriter, System.Globalization.CultureInfo.InvariantCulture, true);

csvWriter.WriteHeader<T>();
csvWriter.NextRecord();
csvWriter.WriteRecords(collection);

using var receivingStream = new MemoryStream(Encoding.UTF8.GetBytes(stringWriter.ToString()));

await _downloads.DownloadLocalAsync(fileName, receivingStream);
}
}
6 changes: 6 additions & 0 deletions src/AstroPanda.Blazor.Toolkit/Services/ICSVDownloadService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace AstroPanda.Blazor.Toolkit.Services;

public interface ICSVDownloadService<T>
{
public Task DownloadLocalAsync(string fileName, IEnumerable<T> collection);
}
Loading