How to export DataGridView to Excel in Windows Forms?
Syncfusion Excel (XlsIO) library is a .NET Excel library used to create, read, and edit Excel documents. Also, converts Excel documents to PDF files. Using this library, you can export DataGridView to Excel in Windows Forms.
Microsoft DataGridView control has more features than in Datagrid control, whose differences can be found from here.
Steps to export DataGridView to Excel in Windows Forms, programmatically:
Step 1: Create a new C# Windows Forms application project.
Create a new Windows Forms application
Step 2: Install the Syncfusion.XlsIO.WinForms NuGet package as reference to your .NET Framework application from NuGet.org.
Install XlsIO NuGet package
Step 3: Initialize the DataGridView object and set data source in Form1.cs.
Include the following namespace to create new DataGridView object.
C#
using System.Windows.Forms;
VB.NET
Imports System.Windows.Forms
Include the following namespace for XlsIO.
C#
using Syncfusion.XlsIO;
VB.NET
Imports Syncfusion.XlsIO
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();
VB.NET
'Initialize DataGridView control Dim dataGridView As DataGridView = New DataGridView() 'Assign data source dataGridView.DataSource = GetDataTable()
GetDataTable() method returns DataTable of applicable data to import.
Step 4: 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"; }
VB.NET
Private WithEvents label As Label Private WithEvents btnCreate As Button Private WithEvents groupBox As GroupBox Private Sub InitializeComponent() label = New Label() btnCreate = New Button() groupBox = New GroupBox() 'Button btnCreate.Location = New Point(339, 280) 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.MiddleLeft 'Group Box groupBox.Location = New Point(12, 100) groupBox.Size = New Size(442, 151) groupBox.Text = "DataGridView" 'DataGridView to Excel ClientSize = New Size(475, 196) Controls.Add(groupBox) Controls.Add(btnCreate) Controls.Add(label) Text = "DataGridView to Excel" End Sub
Step 5: 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.Excel2013; //Create a new workbook IWorkbook workbook = application.Workbooks.Create(1); //Accessing first worksheet in the workbook IWorksheet worksheet = workbook.Worksheets[0]; }
VB.NET
'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
Step 6: 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);
VB.NET
'Import data from DataGridView control worksheet.ImportDataGridView(dataGridView, 1, 1, True, True)
Step 7: 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(); //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.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 DataGridView control worksheet.ImportDataGridView(dataGridView, 1, 1, true, true); //Save the workbook workbook.SaveAs("Output.xlsx"); }
VB.NET
'Initialize ExcelEngine Using excelEngine As ExcelEngine = New ExcelEngine() 'Initialize DataGridView control Dim dataGridView As DataGridView = New DataGridView() 'Assign data source dataGridView.DataSource = GetDataTable() 'Assign the datagridview size dataGridView.Size = New System.Drawing.Size(441, 150) '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 DataGridView control worksheet. ImportDataGridView (dataGridView, 1, 1, True, True ) 'Save the workbook workbook.SaveAs("Output.xlsx") End Using
A complete working example of how to export DataGridView to Excel in Windows Forms can be downloaded from Export DataGridView to Excel.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..).
Refer here to explore the rich set of Syncfusion Excel (XlsIO) library features.
An online sample link to import from DataGrid.
See Also:
Insert Excel rows and columns in C#, VB.NET
Why formulas are not updated when inserting rows?
How can we format a cell in Accounting format type of MS Excel?
How to copy and insert a chart in the same worksheet using C#,VB.NET?
How do I insert watermark in MS Excel?
How to insert an Hyperlink in a cell?
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 the link to learn about generating and registering Syncfusion license key in your application to use the components without trial message.
Conclusion
I hope you enjoyed learning about how to export DataGridView to Excel in Windows Forms.
You can refer to our .NET Excel library page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our .NET Excel library example to understand how to create and manipulate data.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedbackportal. We are always happy to assist you!