How Can I Export data from Datagrid to Excel
I need urgent help. I want to export data from datagrid to Excel but not with datatable. I would like to know if it is possible with datagrid view.
I would appreciate help
here is the code to export
private void buttonAdv1_Click(object sender, EventArgs e)
{
//Exports the DataTable to a spreadsheet.
#region Workbook Initialize
//New instance of XlsIO is created.[Equivalent to launching MS Excel with no workbooks open].
//The instantiation process consists of two steps.
//Step 1 : Instantiate the spreadsheet creation engine.
ExcelEngine excelEngine = new ExcelEngine();
//Step 2 : Instantiate the excel application object.
IApplication application = excelEngine.Excel;
//Set the Workbook version as Excel 97to2003
if (this.rdbExcel97.Checked)
{
application.DefaultVersion = ExcelVersion.Excel97to2003;
fileName = "ExportToExcel.xls";
}
//Set the Workbook version as Excel 2007
else if (this.rdbExcel2007.Checked)
{
application.DefaultVersion = ExcelVersion.Excel2007;
fileName = "ExportToExcel.xlsx";
}
//Set the Workbook version as Excel 2010
else if (this.rdbExcel2010.Checked)
{
application.DefaultVersion = ExcelVersion.Excel2010;
fileName = "ExportToExcel.xlsx";
}
//Set the Workbook version as Excel 2010
else if (this.rdbExcel2013.Checked)
{
application.DefaultVersion = ExcelVersion.Excel2013;
fileName = "ExportToExcel.xlsx";
}
//A new workbook is created.[Equivalent to creating a new workbook in MS Excel]
//The new workbook will have 3 worksheets
IWorkbook workbook = application.Workbooks.Create(1);
//The first worksheet object in the worksheets collection is accessed.
IWorksheet worksheet = workbook.Worksheets[0];
#endregion
#region Export DataTable to Excel
//Export DataTable.
if (this.gridDataBoundGrid1.DataSource != null)
{
worksheet.ImportDataTable((GridDataBoundGrid)this.gridDataBoundGrid1.DataSource, true, 3, 1, -1, -1);
}
else
{
MessageBox.Show("There is no datatable to export, Please import a datatable first", "Error");
//Close the workbook.
workbook.Close();
return;
}
#endregion
#region View the Workbook
//Message box confirmation to view the created spreadsheet.
if (MessageBox.Show("Do you want to view the workbook?", "Workbook has been created",
MessageBoxButtons.YesNo, MessageBoxIcon.Information)
== DialogResult.Yes)
{
//Launching the Excel file using the default Application.[MS Excel Or Free ExcelViewer]
System.Diagnostics.Process.Start(fileName);
//Exit
this.Close();
}
else
{
// Exit
this.Close();
}
#endregion
}
SIGN IN To post a reply.
1 Reply
AK
Adhikesevan Kothandaraman
Syncfusion Team
September 28, 2015 09:46 AM UTC
Hi Richard,
Thanks for contacting Syncfusion Support.
Exporting Grid to an Excel can be achieved using the Converter.GridToExcel method. Please refer to the following Knowledge Base for more details,
http://www.syncfusion.com/kb/697/how-to-export-the-contents-of-a-grid-to-excel
Regards,
Adhi
Thanks for contacting Syncfusion Support.
Exporting Grid to an Excel can be achieved using the Converter.GridToExcel method. Please refer to the following Knowledge Base for more details,
http://www.syncfusion.com/kb/697/how-to-export-the-contents-of-a-grid-to-excel
Regards,
Adhi
SIGN IN To post a reply.
- 1 Reply
- 2 Participants
-
RL Richard lubanga
- Sep 25, 2015 06:00 PM UTC
- Sep 28, 2015 09:46 AM UTC