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. It also, converts Excel documents to PDF files. Using this library, you can export DataGridView to an Excel in Windows Forms.
Microsoft DataGridView control has more features than the Datagrid control, whose differences can be found from here.
Steps to export DataGridView to an 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 a reference to your .NET Framework application from NuGet.org.
Install XlsIO NuGet package
Step 3: Initialize the DataGridView object and set the data source in Form1.cs.
Include the following namespace to create a 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 a 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 the 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 the 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 the DataGridView to an 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); // Access the 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 'Access the 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?
Note:
Starting with v16.2.0.x, if you reference Syncfusion® assemblies from a trial setup or from the NuGet feed, include a license key in your projects. Refer to the link to learn about generating and registering the Syncfusion® license key in your application to use the components without a trial message.
Conclusion
I hope you enjoyed learning about how to export DataGridView to Excel in Windows Forms.
You can refer to our XIsIO’s feature tour page to learn about its other groundbreaking features. Explore our UG documentation and online demos to understand how to manipulate data in Excel documents.
If you are an existing user, you can access our latest components from the License and Downloads page. For new users, you can try our 30-day free trial to check out XlsIO and other Syncfusion® components.
If you have any queries or require clarification, please let us know in the comments below or contact us through our support forums, Support Tickets, or feedback portal. We are always happy to assist you!