Export / import DataGridView to Excel (XLSX, XLS)

Export / import Windows.Forms DataGridView control to Excel (XLSX, XLS) with GemBox.Spreadsheet component.

GemBox.Spreadsheet is a C# / VB.NET component which provides simple and efficient way to read, write, convert and print native Microsoft Excel (XLSX, XLS) and other spreadsheet (ODS, CSV) formats and various document formats (HTML, PDF, XPS) from .NET applications without the need for Microsoft Excel on either the developer or client machines.
GemBox.Spreadsheet Free is free of charge while GemBox.Spreadsheet Professional is a commercial version licensed per developer.
For more information, see GemBox.Spreadsheet features and why GemBox.Spreadsheet outperforms Excel automation.

Following example imports / exports active Excel worksheet to DataGridView control.

C# code

// Load Excel (XLSX) from a file.
var workbook = ExcelFile.Load("Workbook.xlsx");

// Export active worksheet to DataGridView.
DataGridViewConverter.ExportToDataGridView(
workbook.Worksheets.ActiveWorksheet,
dataGridView,
new ExportToDataGridViewOptions() { ColumnHeaders = true });

// Import DataGridView back to active worksheet.
DataGridViewConverter.ImportFromDataGridView(
workbook.Worksheets.ActiveWorksheet,
dataGridView,
new ImportFromDataGridViewOptions() { ColumnHeaders = true });

// Save Excel (XLSX) to a file.
workbook.Save("Workbook.xlsx");

VB.NET code

' Load Excel (XLSX) from a file.
Dim workbook = ExcelFile.Load("Workbook.xlsx")

' Export active worksheet to DataGridView.
DataGridViewConverter.ExportToDataGridView(
workbook.Worksheets.ActiveWorksheet,
dataGridView,
New ExportToDataGridViewOptions() With {.ColumnHeaders = True})

' Import DataGridView back to active worksheet.
DataGridViewConverter.ImportFromDataGridView(
workbook.Worksheets.ActiveWorksheet,
dataGridView,
New ImportFromDataGridViewOptions() With {.ColumnHeaders = True})

' Save Excel (XLSX) to a file.
workbook.Save("Workbook.xlsx")