Fast Export Excel with Database Records in .NET C# || Quick Export to Excel || Fast Export to Excel
Export Records in Excel Very Quickly.
Step 1: Add Package of EPPlus
https://www.nuget.org/packages/EPPlus/
dotnet add package EPPlus --version 6.1.3
==================================================================
Step 2: Implementation on .cs page.
using OfficeOpenXml;
namespace ExportExcel
{
private async void Export()
{
// Collect data from database and store in DataTable object
System.Data.DataTable dt = await GetDataToExport();
using (var package = new ExcelPackage())
{
// Add a new worksheet to the package
var worksheet = package.Workbook.Worksheets.Add("Sheet1");
for (int i = 0; i < dt.Rows.Count; i++)
{
await Task.Run(() =>
{
worksheet.Cells[i + 1, 1].Value = dt.Rows[i][0];
worksheet.Cells[i + 1, 2].Value = dt.Rows[i][1];
worksheet.Cells[i + 1, 3].Value = dt.Rows[i][2];
worksheet.Cells[i + 1, 4].Value = dt.Rows[i][3];
});
}
DependencyMethods methods = new DependencyMethods();
// get file location using wpf tool
methods.SaveAppSettings("TOOL_EXPORT_PATH", txtExportDirectoryPath.Text.Trim());
string path = System.IO.Path.Combine(txtExportDirectoryPath.Text, txtFileName.Text);
// Save the package to a file
//package.SaveAs(new FileInfo("output.xlsx"));
package.SaveAs(path);
}
}
}
==================================================================
Step 3: Add Below line under appsettings tag into App.Config file.
<add key="EPPlus:ExcelPackage.LicenseContext" value="NonCommercial" />
0 comments: