|
|||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
IntroductionExporting data from a datatable to Excel or CSV is one of the most common functionality required in ASP.NET pages. Users can download the data from the datagrid into an Excel spreadsheet or CSV file for offline verification and/or computation. This article includes the source code for such functionality. The common problems in exporting data from datagrid are
To remedy all these issues I came up with the a simpler and more adaptable way to export the datagrid itself to Excel or CSV file. I kept the concept to a dll so that it could easily be used throughout an application. Using the ExportData class libraryThis class library is fully implemented in C#.NET. This dll doesn't need to be registered. Simply copy the assembly "RKLib.ExportData.dll" into your project folder and add it to references. You can also include the "ExportData" project and reference it in your project. This can be used in WebForms as well as in WinForms by simply passing a parameter to the export object's constructor.
The The following are the overload types:
Have a glance at the parameters
You can call this method in the: The following is the code block that
demonstrates "exporting specified columns" of a [Code Behind]
private void btnExport2_Click(object sender, System.EventArgs e)
{
// Export the details of specified columns
try
{
// Get the datatable to export
DataTable dtEmployee = ((DataSet)
Session["dsEmployee"]).Tables["Employee"].Copy();
// Specify the column list to export
int[] iColumns = {1,2,3,5,6};
// Export the details of specified columns to Excel
RKLib.ExportData.Export objExport = new
RKLib.ExportData.Export("Web");
objExport.ExportDetails(dtEmployee,
iColumns, Export.ExportFormat.Excel, "EmployeesInfo2.xls");
}
catch(Exception Ex)
{
lblError.Text = Ex.Message;
}
}
The following is the code block that
demonstrates "exporting specified columns" of a private void btnExportCSV_Click(object sender, System.EventArgs e)
{
// Export the details of specified columns
try
{
lblMessage.Text = "";
// Get the datatable to export
DataTable dtEmployee = dsEmployee.Tables["Employee"].Copy();
// Specify the column list to export
int[] iColumns = {1,2,3,5,6};
// Export the details of specified columns to CSV
RKLib.ExportData.Export objExport = new
RKLib.ExportData.Export("Win");
objExport.ExportDetails(dtEmployee,
iColumns, Export.ExportFormat.CSV,
"C:\\EmployeesInfo.csv");
lblMessage.Text = "Successfully exported to
C:\\EmployeesInfo.csv";
}
catch(Exception Ex)
{
lblMessage.Text = Ex.Message;
}
}
Working with Demo ProjectsTo work with the WebForms Project in C#.NET
To work with WebForms Project in VB.NET
To work with WindowsForms Project in C#.NET
Points of Interest
History
|
||||||||||||||||||||||||||||||