How to Export GridView to Excel in ASP.NET (XlsIO)Library?
Syncfusion Essential XlsIO is a .NET Excel library used to create, read, and edit Excel documents. Using this library, you can export GridView to an Excel document in C# and VB.NET.
Microsoft GridView control displays data items in columns. This article explains how the data can be exported from GridView to Excel.
Steps to export GridView 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 GridView object and set data source in MainPage.aspx.cs
Step 6: Include the following namespace to create new GridView 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 GridView’s DataSource property. Here the DataTable object has been used for the DataSource property. GetDataTable() method returns a DataTable to import.
C#
//Initialize GridView control GridView gridView = new GridView(); //Assign data source gridView.DataSource = GetDataTable();
VB.NET
'Initialize GridView control Dim gridView As GridView = New GridView() 'Assign data source gridView.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 GridView object to Excel worksheet.
Step 9: Use the ImportGridView() method to import data from GridView to Excel worksheet with its column header and cell formatting.
C#
//Import data from GridView control worksheet.ImportGridView(gridView, 1, 1, true, true);
VB.NET
'Import data from GridView control worksheet.ImportGridView(gridView, 1, 1, True, True)
Step 10: Use the following complete code in OnButtonClicked to export data from GridView to Excel using XlsIO.
C#
//Initialize the Excel Engine using (ExcelEngine excelEngine = new ExcelEngine()) { //Initialize GridView control GridView gridView = new GridView(); //Assign data source gridView.DataSource = GetDataTable(); //Binding the data gridView.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 GridView control worksheet.ImportGridView(gridView, 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 GridView control Dim gridView As GridView = New GridView() 'Assign data source gridView.DataSource = GetDataTable() 'Binding the data. gridView.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 GridView control worksheet.ImportGridView(gridView, 1, 1, True, True ) 'Save the workbook. workbook.SaveAs("Sample.xlsx", Response, ExcelDownloadType.Open, ExcelHttpContentType.Excel2013) End Using
A complete working example to export GridView to Excel can be downloaded from GridViewToExcel.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..)
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.