Articles in this section
Category / Section

How to export DataGrid to Excel ?

4 mins read

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:

 

  1. Create a new Windows Forms application.

 

Create Windows Forms application in Visual Studio

 

  1. Install Syncfusion.XlsIO.WinForms NuGet package as reference to your .NET Framework applications from the NuGet.org.

 

Add XlsIO reference to the project

 

  1. Initialize the DataGrid object and set data source in Form1.cs.

 

Include the following namespace to create new DataGrid object.

 

using System.Windows.Forms;

 

Imports System.Windows.Forms

 

Use the following namespace in Web application.

 

using System.Web.UI.WebControls;

 

Imports System.Web.UI.WebControls

 

After the initialization set valid data sources to the DataGrid’s DataSource property. Here the DataTable object has been used for the DataSource property.

 

//Initialize DataGrid control
DataGrid dataGrid = new DataGrid();
 
//Assign data source
dataGrid.DataSource = GetDataTable();

 

'Initialize DataGrid control
Dim dataGrid As DataGrid = New DataGrid()
 
'Assign data source
dataGrid.DataSource = GetDataTable()

 

Note:

GetDataTable() method returns DataTable of applicable data to import.

 

 

  1. Add a new button to export DataGrid to Excel.

 

private Button btnCreate;
private Label label;
 
private void InitializeComponent()
{
  btnCreate = new Button();
  label = new Label();
 
  //Button
  btnCreate.Location = new System.Drawing.Point(339, 110);
  btnCreate.Size = new System.Drawing.Size(115, 26);
  btnCreate.Text = "Create";
  btnCreate.Click += new EventHandler(btnCreate_Click);
   
  //Label
  label.Location = new System.Drawing.Point(0, 50);
  label.Size = new System.Drawing.Size(426, 48);
  label.Text = "Click the button to view an Excel spreadsheet generated by Essential XlsIO. Please note that MS Excel Viewer or MS Excel is required to view the resultant document.";
  label.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
       
  //DataGrid to Excel 
  ClientSize = new System.Drawing.Size(466, 150);
  Controls.Add(label);
  Controls.Add(btnCreate);
  Text = "DataGrid to Excel";
}

 

Private WithEvents label As Label
Private WithEvents btnCreate As Button
 
Private Sub InitializeComponent()
  label = New Label()
  btnCreate = New Button()
 
  'Button
  btnCreate.Location = New Point(339, 110)
  btnCreate.Size = New Size(115, 26)
  btnCreate.Text = "Create"
 
  'Label
  label.Location = New Point(0, 50)
  label.Size = New Size(426, 48)
  label.Text = "Click the button to view an Excel spreadsheet generated by Essential XlsIO. Please note that MS Excel Viewer or MS Excel is required to view the resultant document."
  label.TextAlign = ContentAlignment.MiddleCenter
 
  'DataGrid to Excel 
  ClientSize = New Size(466, 150)
  Controls.Add(btnCreate)
  Controls.Add(label)
  Text = "DataGrid to Excel"
End Sub

 

  1. Use the following code snippet to create new Excel workbook.

 

//Initialize the Excel Engine
using (ExcelEngine excelEngine = new ExcelEngine())
{
  //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];
}

 

'Initialize the Excel Engine
Using excelEngine As ExcelEngine = New ExcelEngine()
 
  'Initialize Application
  Dim application As IApplication = excelEngine.Excel
 
  'Set default version for application
  application.DefaultVersion = ExcelVersion.Excel2013
 
  'Create new workbook
  Dim workbook As IWorkbook = application.Workbooks.Create(1)
 
  'Accessing first worksheet in the workbook
  Dim worksheet As IWorksheet = workbook.Worksheets(0)
 
End Using

 

  1. 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.

 

//Import data from DataGrid control
worksheet.ImportDataGrid(dataGrid, 1, 1, true, true);

 

'Import data from DataGrid control
worksheet.ImportDataGrid(dataGrid, 1, 1, True, True)

 

  1. Use the following complete code in btnCreate_Click to export data from DataGrid to Excel using XlsIO.

 

//Initialize the Excel Engine
using (ExcelEngine excelEngine = new ExcelEngine())
{
  //Initialize DataGrid control
  DataGrid dataGrid = new DataGrid();
 
  //Assign data source
  dataGrid.DataSource = GetDataTable();
 
  //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
  workbook.SaveAs("Output.xlsx");
}

 

'Initialize ExcelEngine
Using excelEngine As ExcelEngine = New ExcelEngine()
 
  'Initialize DataGrid control
  Dim dataGrid As DataGrid = New DataGrid()
 
  'Assign data source
  dataGrid.DataSource = GetDataTable()
 
  '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
 
  '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("Output.xlsx")
 
End Using

 

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.

 

Note:

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.

 

 

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied