We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to export DataGridView to Excel?

Platform: WinForms |
Control: XlsIO

Syncfusion Essential XlsIO is a .NET Excel library used to create, read, and edit Excel documents. Using this library, you can export DataGridView to an Excel document in C# and VB.NET.

Microsoft DataGridView control is a new control which has numerous features than DataGrid control like sorting, etc. This article explains how the data can be exported from DataGridView to Excel with its column header and cell formatting using XlsIO.

Steps to export DataGridView to an Excel file programmatically:

  1. Create a new Windows Forms application.

 

Create Windows Forms application in Visual Studio

 

Create a console application

 

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

 

Add XlsIO reference to the project

 

Install NuGet package

 

  1. Initialize the DataGridView object and set data source in Form1.cs.
  2. Include the following namespace to create new DataGridView object.

C#

using System.Windows.Forms;

 

  1. Include the following namespace for XlsIO.

C#

using Syncfusion.XlsIO;

 

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

C#

//Initialize DataGridView control
DataGridView dataGridView = new DataGridView();
 
//Assign data source
dataGridView.DataSource = GetDataTable();

 

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

C#

private Button btnCreate;
private Label label;
private GroupBox groupBox;
 
private void InitializeComponent()
{
  btnCreate = new Button();
  label = new Label();
  groupBox = new GroupBox();
 
  //Button
  btnCreate.Location = new System.Drawing.Point(339, 280);
  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.MiddleLeft;
       
  //Group Box
  groupBox.Location = new System.Drawing.Point(12, 100);
  groupBox.Size = new System.Drawing.Size(442, 151);
  groupBox.Text = "DataGridView";
 
  //DataGridView to Excel 
  ClientSize = new System.Drawing.Size(466, 333);
  Controls.Add(groupBox);
  Controls.Add(label);
  Controls.Add(btnCreate);
  Text = "DataGridView to Excel";
}

 

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

C#

//Initialize the Excel Engine
using (ExcelEngine excelEngine = new ExcelEngine())
{
  //Initialize Application
  IApplication application = excelEngine.Excel;
 
  //Set default version for application
  application.DefaultVersion = ExcelVersion.Xlsx;
 
  //Create a new workbook
  IWorkbook workbook = application.Workbooks.Create(1);
 
  //Accessing first worksheet in the workbook
  IWorksheet worksheet = workbook.Worksheets[0];
}

 

  1. To export data from DataGridView object to Excel worksheet use the ImportDataGridView() method to export the data from DataGridView to Excel worksheet with its column header and cell formatting.

C#

//Import data from DataGridView control
worksheet.ImportDataGridView(dataGridView, 1, 1, true, true);

 

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

C#

//Initialize the Excel Engine
using (ExcelEngine excelEngine = new ExcelEngine())
{
  //Initialize DataGridView control
  DataGridView dataGridView = new DataGridView();
 
  //Assign data source
  dataGridView.DataSource = GetDataTable();
 
  //Add control to group box
  groupBox.Controls.Add(dataGridView);
 
  //Assign the datagridview size
  dataGridView.Size = new System.Drawing.Size(441, 150);
 
  //Initialize Application
  IApplication application = excelEngine.Excel;
 
  //Set default version for application
  application.DefaultVersion = ExcelVersion.Xlsx;
 
  //Create a new workbook
  IWorkbook workbook = application.Workbooks.Create(1);
 
  //Accessing first worksheet in the workbook
  IWorksheet worksheet = workbook.Worksheets[0];
 
  //Import data from DataGridView control
  worksheet.ImportDataGridView(dataGridView, 1, 1, true, true);
 
  //Save the workbook
  workbook.SaveAs("Output.xlsx");
}

 

A complete working example to export DataGridView to Excel in C# is present on this GitHub page.

We recommend you to walkthrough the documentation for importing data from other grid controls, 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.

 

2X faster development

The ultimate WinForms UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile