S95.Blazor.Export
1.0.3
dotnet add package S95.Blazor.Export --version 1.0.3
NuGet\Install-Package S95.Blazor.Export -Version 1.0.3
<PackageReference Include="S95.Blazor.Export" Version="1.0.3" />
<PackageVersion Include="S95.Blazor.Export" Version="1.0.3" />
<PackageReference Include="S95.Blazor.Export" />
paket add S95.Blazor.Export --version 1.0.3
#r "nuget: S95.Blazor.Export, 1.0.3"
#:package S95.Blazor.Export@1.0.3
#addin nuget:?package=S95.Blazor.Export&version=1.0.3
#tool nuget:?package=S95.Blazor.Export&version=1.0.3
Blazor.Export
Blazor.Export is a lightweight and flexible NuGet package that enables exporting table data from Blazor applications into various formats.
The package is designed to work seamlessly with both Blazor Server and Blazor WebAssembly projects. It allows developers to easily convert and export data from any table component with minimal configuration and high performance. Blazor.Export helps you integrate professional document export functionality into your Blazor apps with just a few lines of code.
Features
- Export to: CSV (.csv), Excel (.xlsx), PDF (.pdf) & Word (.docx).
- Blazor Server and WebAssembly support
- Localized columns: Customize column names as you wish.
- Minimal dependencies
How to use
- Install the Blazor.Export NuGet package in your Blazor project.
- In Program.cs (Server) add:
builder.Services.AddExportServices(); - Add this JavaScript code:
function saveAsFile(filename, bytesBase64, contentType) {
const link = document.createElement('a');
link.download = filename;
link.href = `data:${contentType};base64,${bytesBase64}`;
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
}
- Inject IExportService into your component and call the desired export method.
private async Task ExportAsync(ExportFormat format)
{
var data = new List<T> { };
var bytes = format switch
{
ExportFormat.Csv => ExportService.ExportToCsv(data),
ExportFormat.Excel => ExportService.ExportToExcel(data),
ExportFormat.Pdf => ExportService.ExportToPdf(data),
ExportFormat.Word => ExportService.ExportToWord(data),
_ => ExportService.ExportToCsv(data)
};
var (fileName, contentType) = format switch
{
ExportFormat.Csv => ($"File_{DateTime.Now:yyyy-MM-dd_HH-mm}.csv", "text/csv"),
ExportFormat.Excel => ($"File_{DateTime.Now:yyyy-MM-dd_HH-mm}.xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"),
ExportFormat.Pdf => ($"File_{DateTime.Now:yyyy-MM-dd_HH-mm}.pdf", "application/pdf"),
ExportFormat.Word => ($"File_{DateTime.Now:yyyy-MM-dd_HH-mm}.docx", "application/vnd.openxmlformats-officedocument.wordprocessingml.document"),
_ => ($"File_{DateTime.Now:yyyy-MM-dd_HH-mm}.csv", "text/csv")
};
await JSRuntime.InvokeVoidAsync("saveAsFile", fileName, Convert.ToBase64String(bytes), contentType);
}
- If you using WebAssembly then add ExportController on Server:
public class ExportController(IExportService exportService) : ControllerBase
{
private readonly IExportService ExportService = exportService;
[HttpPost("csv")]
public FileContentResult ExportToCsv([FromBody] List<ExportData> data)
{
var bytes = ExportService.ExportToCsv<ExportData>(data);
return File(bytes, "text/csv", "export.csv");
}
[HttpPost("excel")]
public FileContentResult ExportToExcel([FromBody] List<ExportData> data)
{
var bytes = ExportService.ExportToExcel<ExportData>(data);
return File(bytes,
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
"export.xlsx");
}
[HttpPost("pdf")]
public FileContentResult ExportToPdf([FromBody] List<ExportData> data)
{
var bytes = ExportService.ExportToPdf<ExportData>(data);
return File(bytes, "application/pdf", "export.pdf");
}
[HttpPost("word")]
public FileContentResult ExportToWord([FromBody] List<ExportData> data)
{
var bytes = ExportService.ExportToWord<ExportData>(data);
return File(bytes,
"application/vnd.openxmlformats-officedocument.wordprocessingml.document",
"export.docx");
}
}
Licence
Project is licensed under the MIT License
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net7.0 is compatible. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 is compatible. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.