EasyXLS

How to export DataGridView to Excel file in C# and VB.NET

EasyXLS™ library allows you to export a DataGridView to an Excel file. The exported data in cells can be formatted.

EasyXLS can be successfully used inclusively to export large Excel files having big volume of data with fast exporting time.

EasyXLS allows you to export Excel files without Excel installed, without Interop or any other additional software installed.

Source code sample

Step 1: Download and install EasyXLS Excel Library for .NET

To download the trial version of EasyXLS Excel Library, press the below button:

Download EasyXLS™ Excel Library for .NET

If you already own a license key, you may login and download EasyXLS from your account.

Step 2: Create a C# or VB.NET project

If don't have a project, create one as ASP.NET web application, windows forms app, console application, class library or service.

Step 3: Include EasyXLS library into project

EasyXLS.dll must be added as reference to your project. EasyXLS.dll can be found after installing EasyXLS, in "Dot NET version" folder.

Add EasyXLS reference

Step 4: Run C# or VB.NET code that exports DataGridView to Excel

The below example shows how to export DataGridView to Excel in C# and VB.NET from a winform windows application.

C#.NET
VB.NET
using System;
using System.Data;
using System.Windows.Forms;
using EasyXLS;

namespace WindowsFormsApplication
{
    public partial class ExportToExcelForm : Form
    {
        public ExportToExcelForm()
        {
            InitializeComponent();
            FillDataGridView();
        }
        
        private void btnExportToExcel_Click(object sender, EventArgs e)
        {
            // Create an instance of the class that exports Excel files, having one sheet
            ExcelDocument workbook = new ExcelDocument(1);
            
            // Set sheet name
            ExcelWorksheet xlsWorksheet = (ExcelWorksheet)workbook.easy_getSheetAt(0);
            xlsWorksheet.setSheetName("DataGridView");
                            
            // Get the sheet table that stores the data
            ExcelTable xlsTable = xlsWorksheet.easy_getExcelTable();
            int tableRow = 0;
            
            // Export DataGridView header if the header is visible
            if (dataGridView.ColumnHeadersVisible)
            {
                // Create the formatting style for the header
                ExcelStyle xlsStyleHeader = new ExcelStyle();
                xlsStyleHeader.setBackground(dataGridView.ColumnHeadersDefaultCellStyle.BackColor);
                xlsStyleHeader.setForeground(dataGridView.ColumnHeadersDefaultCellStyle.ForeColor);
                
                // Add data in cells for header
                for (int column = 0; column < dataGridView.Columns.Count; column++)
                {
                    xlsTable.easy_getCell(tableRow, column).setValue(
                                          dataGridView.Columns[column].HeaderText);
                    xlsTable.easy_getCell(tableRow, column).setStyle(xlsStyleHeader);
                }
                tableRow++;
            }
            
            // Create a formatting style for cells
            ExcelStyle xlsStyleCell = new ExcelStyle();
            xlsStyleCell.setBackground(dataGridView.DefaultCellStyle.BackColor);
            xlsStyleCell.setForeground(dataGridView.DefaultCellStyle.ForeColor);
            
            // Add data in cells
            for (int row = 0; row < dataGridView.Rows.Count-1; row++)
            {
                for (int column = 0; column < dataGridView.Columns.Count; column++)
                {
                    xlsTable.easy_getCell(tableRow, column).setValue(
                                          dataGridView.Rows[row].Cells[column].Value.ToString());
                    xlsTable.easy_getCell(tableRow, column).setStyle(xlsStyleCell);
                }
                tableRow++;
            }
            
            // Export Excel file
            workbook.easy_WriteXLSXFile("C:\\Samples\\Export To Excel.xlsx");
        }
        
        private void FillDataGridView()
        {
            // Create the database connection
            String sConnectionString = "Initial Catalog=Northwind;" +
                                        "Data Source=localhost;" +
                                        "Integrated Security=SSPI;";
            System.Data.SqlClient.SqlConnection sqlConnection = new 
                         System.Data.SqlClient.SqlConnection(sConnectionString);
            sqlConnection.Open();
            
            // Create the adapter used to fill the dataset
            String sQueryString = "SELECT TOP 100 CAST(Month(ord.OrderDate) AS varchar)+'/' +" +
                         "CAST(Day(ord.OrderDate) AS varchar) + '/' +" + 
                         "CAST(year(ord.OrderDate) AS varchar) AS 'Order Date', " +
                         " P.ProductName AS 'Product Name', O.UnitPrice AS Price, O.Quantity , " + 
                         "O.UnitPrice * O. Quantity AS Value FROM Orders AS ord, [Order Details] AS O, " + 
                         "Products AS P WHERE O.ProductID = P.ProductID AND O.OrderID = ord.OrderID";
            System.Data.SqlClient.SqlDataAdapter adp = new 
                        System.Data.SqlClient.SqlDataAdapter(sQueryString, sqlConnection);
            
            // Populate the dataset
            DataSet dataset = new DataSet();
            adp.Fill(dataset);
            
            // Set the data source of the DataGridView
            dataGridView.DataSource = dataset.Tables[0];
        }
    }
}
                                                                    

Formatting cells

EasyXLS™ enables you to format cells, rows and columns in order to set the fonts and colors.

Export DataGridView to XLSX, XLSB, XLSM and XLS files

This code sample shows how to export a DataGridView to XLSX file. Similarly, you can export a DataGridView to XLS file using ExcelDocument.easy_WriteXLSFile_FromDataSet method or export DataGridView to XLSB file using ExcelDocument.easy_WriteXLSBFile_FromDataSet method.

Export DataGridView to Excel file with multiple sheets

EasyXLS enables you to create multiple sheets for the Excel file and insert the DataGridView inside a specific sheet.

Export DataTable of DataGridView to Excel file

If the data source of the DataGridView is a DataTable, like in the above sample, the data can be also exported directly from the DataTable.

Getting started with EasyXLS Excel library

To download the trial version of EasyXLS Excel Library, press the below button:

Download EasyXLS™ Excel Library for .NET

If you already own a license key, you may login and download EasyXLS from your account.

After installation, you will need to create a C# or VB.NET project and add a reference to EasyXLS.dll.



Available for: Professional, Excel Writer, Express Excel Writer
Go to top

EasyXLS Excel libraries:

.NET
.NET Excel Library
full .NET version to import, export or convert Excel files
.NET Excel Writer
.NET version to create and export Excel files
.NET Express Excel Writer
limited .NET version to create and export Excel files in predefined formats
Download EasyXLS™ Excel Library for .NET

File formats:

Office 365
MS Excel 2021 - 2024
MS Excel 2007 - 2019
MS Excel 97 - 2003
XLSXXLSMXLSBXLS
XMLHTMLCSVTXT