Syncfusion Essential XlsIO is a .NET Excel library used to create, read, and edit Excel documents. Using this library, you can export DataGrid to an Excel document in C# and VB.NET.
Microsoft DataGrid control displays data in a customizable form. These data can be exported from DataGrid to Excel worksheet with its header and cell formatting using XlsIO.
The following DataGrid objects are supported in XlsIO to export data from DataGrid to Excel.
Steps to export DataGrid to an Excel file programmatically:
- Create a new Windows Forms application.
- Install Syncfusion.XlsIO.WinForms NuGet package as reference to your .NET Framework applications from the NuGet.org.
- Initialize the DataGrid object and set data source in Form1.cs.
Include the following namespace to create new DataGrid object.
Use the following namespace in Web application.
After the initialization set valid data sources to the DataGrid’s DataSource property. Here the DataTable object has been used for the DataSource property.
Note: GetDataTable() method returns DataTable of applicable data to import.
- Add a new button to export DataGrid to Excel.
- Use the following code snippet to create new Excel workbook.
- Export data from DataGrid object to Excel worksheet.
Use the ImportDataGrid() method to import data from DataGrid to Excel worksheet with its column header and cell formatting.
- Use the following complete code in btnCreate_Click to export data from DataGrid to Excel using XlsIO.
A complete working example to export DataGrid to Excel can be downloaded from DataGrid-to-Excel-file.zip
We recommend you to walkthrough the documentation for importing data from other grid controls, other data objects and exporting operations available with XlsIO for various data objects (DataTable, CLR Objects, etc..).
An online sample link to import from DataGrid.
Starting with v16.2.0.x, if you reference Syncfusion assemblies from trial setup or from the NuGet feed, include a license key in your projects. Refer to link to learn about generating and registering Syncfusion license key in your application to use the components without trail message.