Export DataGrid to Excel in ASP.NET
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. This article explains how 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:
Step 1: Create a new ASP.NET Web application project.
Create ASP.NET Web Forms application in Visual Studio
Step 2: Install the Syncfusion.XlsIO.AspNet NuGet package as reference to your .NET Framework applications from the NuGet.org.
Add XlsIO reference to the project
Step 3: Add a new Web Form in ASP .NET project. Right click on the project and select Add > New Item and add a Web Form from the list. Name it as MainPage.
Step 4: Add a new button in the MainPage.aspx as shown below.
CSHTML
<html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:Button ID="Button1" runat="server" Text="Create Document" OnClick="OnButtonClicked" /> </div> </form> </body> </html>
Step 5: Initialize the DataGrid object and set data source in MainPage.aspx.cs
Step 6: Include the following namespace to create new DataGrid object.
C#
using System.Web.UI.WebControls;
VB.NET
Imports System.Web.UI.WebControls
Step 7: After the initialization set valid data sources to the DataGrid’s DataSource property. Here the DataTable object has been used for the DataSource property. GetDataTable() method returns a DataTable to import.
C#
//Initialize DataGrid control DataGrid dataGrid = new DataGrid(); //Assign data source dataGrid.DataSource = GetDataTable();
VB.NET
'Initialize DataGrid control Dim dataGrid As dataGrid = New DataGrid() 'Assign data source dataGrid.DataSource = GetDataTable()
Consider the GetDataTable() method as below.
C#
/// <summary> /// create datatable with numbers and returns the same /// </summary> /// <returns>datatable filled with number</returns> private DataTable GetDataTable() { Random r = new Random(); DataTable dt = new DataTable("NumbersTable"); int nCols = 4; int nRows = 10; for (int i = 0; i < nCols; i++) dt.Columns.Add(new DataColumn("Column" + i.ToString())); for (int i = 0; i < nRows; ++i) { DataRow dr = dt.NewRow(); for (int j = 0; j < nCols; j++) dr[j] = r.Next(0, 10); dt.Rows.Add(dr); } return dt; }
VB.NET
Private Function GetDataTable() As DataTable Dim r As Random = New Random() Dim dt As DataTable = New DataTable("NumbersTable") Dim nCols As Integer = 4 Dim nRows As Integer = 10 For i As Integer = 0 To nCols - 1 dt.Columns.Add(New DataColumn("Column" & i.ToString())) Next For i As Integer = 0 To nRows - 1 Dim dr As DataRow = dt.NewRow() For j As Integer = 0 To nCols - 1 dr(j) = r.[Next](0, 10) Next dt.Rows.Add(dr) Next Return dt End Function
Step 8: Export data from DataGrid object to Excel worksheet.
Step 9: Use the ImportDataGrid() method to import data from DataGrid to Excel worksheet with its column header and cell formatting.
C#
//Import data from DataGrid control worksheet.ImportDataGrid(dataGrid, 1, 1, true, true);
VB.NET
'Import data from DataGrid control worksheet.ImportDataGrid(dataGrid, 1, 1, True, True)
Step 10: Use the following complete code in OnButtonClicked to export data from DataGrid to Excel using XlsIO.
C#
//Initialize the Excel Engine using (ExcelEngine excelEngine = new ExcelEngine()) { //Initialize DataGrid control DataGrid dataGrid = new DataGrid(); //Assign data source dataGrid.DataSource = GetDataTable(); //Binding the data dataGrid.DataBind(); //Initialize Application IApplication application = excelEngine.Excel; //Set default version for application. application.DefaultVersion = ExcelVersion.Excel2013; //Create a new workbook IWorkbook workbook = application.Workbooks.Create(1); //Accessing first worksheet in the workbook IWorksheet worksheet = workbook.Worksheets[0]; //Import data from DataGrid control worksheet.ImportDataGrid(dataGrid, 1, 1, true, true); //Save the workbook to disk in xlsx format workbook.SaveAs("Sample.xlsx", Response, ExcelDownloadType.Open, ExcelHttpContentType.Excel2013); }
VB.NET
'Initialize ExcelEngine Using excelEngine As ExcelEngine = New ExcelEngine() 'Initialize DataGrid control Dim dataGrid As DataGrid = New DataGrid() 'Assign data source dataGrid.DataSource = GetDataTable() 'Binding the data. dataGrid.DataBind() 'Initialize Application. Dim application As IApplication = excelEngine.Excel 'Set default version for application. application.DefaultVersion = ExcelVersion.Excel2013 'Create a new workbook. Dim workbook As IWorkbook = application.Workbooks.Create(1) 'Accessing first worksheet in the workbook. Dim worksheet As IWorksheet = workbook.Worksheets(0) 'Import data from DataGrid control worksheet.ImportDataGrid(dataGrid, 1, 1, True, True ) 'Save the workbook. workbook.SaveAs("Sample.xlsx", Response, ExcelDownloadType.Open, ExcelHttpContentType.Excel2013) End Using
A complete working example to export DataGrid to Excel can be downloaded from DataGrid-to-Excel-file.zip
By executing the program, you will get the Excel file as follows.
Output Excel document
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.