C# DataGridView Export to Excel with Formatting: The Complete Guide

Exporting DataGridView data to an Excel file is a common requirement in Windows Forms applications. Whether generating reports or transferring data for analysis, developers need a reliable method to export DataGridView contents while preserving formatting. In this article, we'll show you how to export DataGridView to Excel using IronXL, a modern approach that works perfectly without requiring Microsoft Excel installation.
How Do I Set Up a Windows Forms Project for DataGridView Export?
Traditional approaches require adding a reference through the COM tab to the Microsoft Excel Object Library. However, this method has significant drawbacks: it requires Microsoft Excel installed on every machine, involves complex releasing object patterns with private void releaseobject methods, and performs poorly with large Excel files.
IronXL eliminates these issues entirely. In Visual Studio, simply install via NuGet Package Manager—no need to select Add Reference menu or navigate to the COM tab. The library handles all the data export operations without dependencies on Microsoft Office.
To get started, create a new Windows Forms project in Visual Studio, then add the following namespaces to your form:
using IronXL;
using System.Data;using IronXL;
using System.Data;Imports IronXL
Imports System.DataAdd a DataGridView control and a Button to your form. The DataGridView control will display data before export, while the button triggers the export DataGridView operation.
How Can I Load Data into a DataGridView Control?
The Form Load event is ideal for populating your DataGridView with data. The following code sample demonstrates binding a DataTable to the DataGridView control:
private void Form1_Load(object sender, EventArgs e)
{
// Create DataTable with sample data
DataTable dt = new DataTable();
dt.Columns.Add("ProductID", typeof(int));
dt.Columns.Add("ProductName", typeof(string));
dt.Columns.Add("Price", typeof(decimal));
dt.Columns.Add("Stock", typeof(int));
// Add rows with values
dt.Rows.Add(1, "Laptop", 999.99, 50);
dt.Rows.Add(2, "Mouse", 29.99, 200);
dt.Rows.Add(3, "Keyboard", 79.99, 150);
dt.Rows.Add(4, "Monitor", 349.99, 75);
dataGridView1.DataSource = dt;
}private void Form1_Load(object sender, EventArgs e)
{
// Create DataTable with sample data
DataTable dt = new DataTable();
dt.Columns.Add("ProductID", typeof(int));
dt.Columns.Add("ProductName", typeof(string));
dt.Columns.Add("Price", typeof(decimal));
dt.Columns.Add("Stock", typeof(int));
// Add rows with values
dt.Rows.Add(1, "Laptop", 999.99, 50);
dt.Rows.Add(2, "Mouse", 29.99, 200);
dt.Rows.Add(3, "Keyboard", 79.99, 150);
dt.Rows.Add(4, "Monitor", 349.99, 75);
dataGridView1.DataSource = dt;
}IRON VB CONVERTER ERROR developers@ironsoftware.comPopulated Datatable

This code creates a DataTable dt with four columns, then uses the Add method to populate DataGridView rows with sample values. The DataGridView control accepts DataTable as its data source, which will display data in a tabular format. The first column contains integer IDs, while the other columns hold string and decimal values.
How Do I Export DataGridView Data to an Excel File?
The button click event handler contains the core export logic. IronXL's approach differs from traditional methods; DataGridView contents can be exported directly to an Excel file through a DataTable intermediate step:
private void btnExport_Click(object sender, EventArgs e)
{
try
{
// Convert DataGridView to DataTable
DataTable dt = new DataTable();
foreach (DataGridViewColumn column in dataGridView1.Columns)
dt.Columns.Add(column.HeaderText);
foreach (DataGridViewRow row in dataGridView1.Rows)
{
if (!row.IsNewRow)
{
DataRow dataRow = dt.NewRow();
for (int i = 0; i < dataGridView1.Columns.Count; i++)
dataRow[i] = row.Cells[i].Value;
dt.Rows.Add(dataRow);
}
}
// Create workbook and export
WorkBook workbook = WorkBook.Create();
WorkSheet worksheet = workbook.DefaultWorkSheet;
worksheet.LoadFromDataTable(dt, true);
workbook.SaveAs("C:\\Reports\\Export.xlsx");
MessageBox.Show("Export complete!");
}
catch (Exception ex)
{
MessageBox.Show($"Error: {ex.Message}");
}
}private void btnExport_Click(object sender, EventArgs e)
{
try
{
// Convert DataGridView to DataTable
DataTable dt = new DataTable();
foreach (DataGridViewColumn column in dataGridView1.Columns)
dt.Columns.Add(column.HeaderText);
foreach (DataGridViewRow row in dataGridView1.Rows)
{
if (!row.IsNewRow)
{
DataRow dataRow = dt.NewRow();
for (int i = 0; i < dataGridView1.Columns.Count; i++)
dataRow[i] = row.Cells[i].Value;
dt.Rows.Add(dataRow);
}
}
// Create workbook and export
WorkBook workbook = WorkBook.Create();
WorkSheet worksheet = workbook.DefaultWorkSheet;
worksheet.LoadFromDataTable(dt, true);
workbook.SaveAs("C:\\Reports\\Export.xlsx");
MessageBox.Show("Export complete!");
}
catch (Exception ex)
{
MessageBox.Show($"Error: {ex.Message}");
}
}IRON VB CONVERTER ERROR developers@ironsoftware.comDataGridView to Excel Output

This button click event handler with object sender, EventArgs e parameters iterates through all the data in the DataGridView rows and columns, building a DataTable. The LoadFromDataTable method accepts DataTable and populates the worksheet. The workbook object is then saved to the specified location. IronXL can also export large Excel files efficiently without the memory issues common with Microsoft Interop.
How Can I Apply Formatting to the Exported Excel File?
Creating a formatted Excel file requires styling the header row and cells. This example demonstrates applying header row background color and alternating rows styling:
private void ExportWithFormatting(object sender, EventArgs e)
{
WorkBook workbook = WorkBook.Create();
WorkSheet worksheet = workbook.DefaultWorkSheet;
// Add header row
string[] headers = { "ID", "Name", "Price", "Stock" };
for (int col = 0; col < headers.Length; col++)
{
worksheet.SetCellValue(0, col, headers[col]);
worksheet[$"{(char)('A' + col)}1"].Style.Font.Bold = true;
worksheet[$"{(char)('A' + col)}1"].Style.SetBackgroundColor("#4472C4");
}
// Add data with alternating row colors
int rowIndex = 1;
foreach (DataGridViewRow row in dataGridView1.Rows)
{
if (row.IsNewRow) continue;
for (int col = 0; col < dataGridView1.Columns.Count; col++)
{
worksheet.SetCellValue(rowIndex, col, row.Cells[col].Value?.ToString());
}
// Apply alternating background colors
if (rowIndex % 2 == 0)
{
var range = worksheet[$"A{rowIndex + 1}:D{rowIndex + 1}"];
range.Style.SetBackgroundColor("#D6DCE5");
}
rowIndex++;
}
workbook.SaveAs("C:\\Reports\\FormattedExport.xlsx");
}private void ExportWithFormatting(object sender, EventArgs e)
{
WorkBook workbook = WorkBook.Create();
WorkSheet worksheet = workbook.DefaultWorkSheet;
// Add header row
string[] headers = { "ID", "Name", "Price", "Stock" };
for (int col = 0; col < headers.Length; col++)
{
worksheet.SetCellValue(0, col, headers[col]);
worksheet[$"{(char)('A' + col)}1"].Style.Font.Bold = true;
worksheet[$"{(char)('A' + col)}1"].Style.SetBackgroundColor("#4472C4");
}
// Add data with alternating row colors
int rowIndex = 1;
foreach (DataGridViewRow row in dataGridView1.Rows)
{
if (row.IsNewRow) continue;
for (int col = 0; col < dataGridView1.Columns.Count; col++)
{
worksheet.SetCellValue(rowIndex, col, row.Cells[col].Value?.ToString());
}
// Apply alternating background colors
if (rowIndex % 2 == 0)
{
var range = worksheet[$"A{rowIndex + 1}:D{rowIndex + 1}"];
range.Style.SetBackgroundColor("#D6DCE5");
}
rowIndex++;
}
workbook.SaveAs("C:\\Reports\\FormattedExport.xlsx");
}IRON VB CONVERTER ERROR developers@ironsoftware.comFormatted Excel File Output

This C# DataGridView export to Excel with formatting example sets the first row as a styled header row with bold font and background colors. The code applies alternating rows styling for improved readability. You can customize cells with different font styles, fill patterns, and border configurations using IronXL's comprehensive styling API.
Conclusion
Exporting DataGridView to Excel in Windows Forms applications becomes straightforward with IronXL. Unlike the traditional Microsoft Excel Object Library approach requiring COM tab references and complex object cleanup code, IronXL provides a clean, modern API for creating Excel files with formatting.
The library handles everything from basic data to Excel exports to sophisticated, formatted Excel file generation, including header row background color styling and alternating rows. For applications processing large Excel files, IronXL delivers superior performance without requiring Microsoft Excel installation.
Ready to streamline your DataGridView to Excel workflow? Start your free trial today, or explore IronXL licensing options for production deployment.
Frequently Asked Questions
How can I export DataGridView data to Excel in C#?
Using IronXL, you can easily export DataGridView data to Excel files in C# by leveraging its powerful functionalities for data manipulation and export.
What formatting options are available when exporting DataGridView to Excel?
IronXL allows you to apply various formatting options, such as header styling and alternating row colors, when exporting DataGridView to Excel.
Is it possible to export DataGridView data to Excel in Windows Forms applications?
Yes, you can export DataGridView data to Excel in Windows Forms applications using IronXL, which provides seamless integration and functionality for this purpose.
Can I style headers when exporting DataGridView to Excel?
IronXL enables you to style headers with custom fonts, colors, and other formatting options when exporting DataGridView data to Excel.
How do I apply alternating row colors in Excel when exporting from DataGridView?
IronXL allows you to apply alternating row colors to Excel sheets when exporting data from DataGridView, enhancing the visual appeal and readability of the data.
Do I need special libraries to export DataGridView to Excel in C#?
Yes, using a library like IronXL simplifies the process of exporting DataGridView to Excel in C#, providing robust tools for data handling and formatting.
What are the benefits of using IronXL for DataGridView to Excel export?
IronXL offers benefits such as ease of use, comprehensive formatting options, and compatibility with Windows Forms, making it ideal for exporting DataGridView to Excel.









