User GuideFAQHow to import Excel file to DataGridView in C# and VB.NET
How to import Excel file to DataGridView in C# and VB.NET
EasyXLS™ library allows you to import Excel data to DataGridView. The data can be imported from an Excel sheet or from the active Excel sheet. The entire sheet data or only data from ranges of cells can be imported.
EasyXLS can be successfully used to also import large Excel files having big volume of data with fast importing time.
EasyXLS permits you to import Excel files without Excel installed, without OLEDB, without Interop or any other additional software installed.
using System;
using System.Data;
using System.Windows.Forms;
using EasyXLS;
namespace WindowsFormsApplication
{
public partial class ImportExcelForm : Form
{
public ImportExcelForm()
{
InitializeComponent();
FillDataGridView();
}
private void FillDataGridView()
{
// Create an instance of the class that imports Excel files
ExcelDocument workbook = new ExcelDocument();
// Import Excel file to DataSet
DataSet ds = workbook.easy_ReadXLSXActiveSheet_AsDataSet("C:\\Samples\\Excel to DataGridView.xlsx");
// Get the DataTable of the DataSet
DataTable dataTable = ds.Tables[0];
// Extract the first Excel row as header
Object[] header = dataTable.Rows[0].ItemArray;
dataTable.Rows.RemoveAt(0);
// Set the data source of the DataGridView
dataGridView.DataSource = dataTable;
// Set DataGridView header data from the first Excel rowfor (int column = 0; column < dataTable.Columns.Count; column++)
{
dataGridView.Columns[column].HeaderText = (String)header[column];
}
}
}
}
Imports EasyXLS
Public Class ImportFromExcelForm
Private Sub ImportFromExcel_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles MyBase.Load
FillDataGridView()
End SubPrivate Sub FillDataGridView()
' Create an instance of the class that imports Excel filesDim workbook As ExcelDocument = New ExcelDocument()
' Import Excel file to DataSetDim ds As DataSet = workbook.easy_ReadXLSXActiveSheet_AsDataSet("C:\Samples\Excel to DataGridView.xlsx")
' Get the DataTable of the DataSetDim dataTable As DataTable = ds.Tables(0)
' Extract the first Excel row as headerDim header As Object() = dataTable.Rows(0).ItemArray
dataTable.Rows.RemoveAt(0)
' Set the data source of the DataGridView
dataGridView.DataSource = dataTable
' Set DataGridView header data from the first Excel rowFor column As Integer = 0 To dataTable.Columns.Count - 1
dataGridView.Columns(column).HeaderText = header(column)
Next
End Sub
End Class
Import Excel cell formatting to DataGridView
Importing also the cell formatting requires some changes to previous code in order to import not only data from the Excel file, but also the cell formatting styles. See below how the source code must be adjusted:
private void FillDataGridView()
{
// Create an instance of the class that imports Excel files
ExcelDocument workbook = new ExcelDocument();
// Import Excel file
workbook.easy_LoadXLSXFile("C:\\Samples\\Excel to DataGridView.xlsx");
// Get the first sheet from the Excel file
ExcelWorksheet xlsSheet = (ExcelWorksheet)workbook.easy_getSheetAt(0);
// Import Excel sheet to DataSet
DataSet ds = workbook.easy_ReadExcelWorksheet_AsDataSet(xlsSheet);
// Get the DataTable of the DataSet
DataTable dataTable = ds.Tables[0];
// Extract the first Excel row as header
Object[] header = dataTable.Rows[0].ItemArray;
dataTable.Rows.RemoveAt(0);
// Set the data source of the DataGridView
dataGridView.DataSource = dataTable;
// Set DataGridView header data from the first Excel rowfor (int column = 0; column < dataTable.Columns.Count; column++)
{
dataGridView.Columns[column].HeaderText = (String)header[column];
}
// Set DataGridView header formatting (assuming that all header columns have the same format)
ExcelStyle xlsStyleHeader = xlsSheet.easy_getExcelTable().easy_getCell(0, 0).getStyle();
dataGridView.ColumnHeadersDefaultCellStyle.BackColor = xlsStyleHeader.getBackground();
dataGridView.ColumnHeadersDefaultCellStyle.Font =
new System.Drawing.Font(xlsStyleHeader.getFont(), xlsStyleHeader.getFontSize());
dataGridView.ColumnHeadersDefaultCellStyle.ForeColor = xlsStyleHeader.getForeground();
// Set DataGridView cell formatting (assuming that all cells have the same format)
ExcelStyle xlsStyleData = xlsSheet.easy_getExcelTable().easy_getCell(1, 0).getStyle();
for (int column = 0; column < dataTable.Columns.Count; column++)
{
dataGridView.Columns[column].DefaultCellStyle.BackColor = xlsStyleData.getBackground();
dataGridView.Columns[column].DefaultCellStyle.Font =
new System.Drawing.Font(xlsStyleData.getFont(), xlsStyleData.getFontSize());
dataGridView.Columns[column].DefaultCellStyle.ForeColor = xlsStyleData.getForeground();
}
// Set DataGridView column widthsfor (int column = 0; column < dataTable.Columns.Count; column++)
{
dataGridView.Columns[column].Width = xlsSheet.easy_getExcelTable().getColumnWidth(column);
}
}
Private Sub FillDataGridView()
' Create an instance of the class that imports Excel filesDim workbook As ExcelDocument = New ExcelDocument()
' Import Excel file
workbook.easy_LoadXLSXFile("C:\Samples\Excel to DataGridView.xlsx")
' Get the first sheet from the Excel fileDim xlsSheet As ExcelWorksheet = workbook.easy_getSheetAt(0)
' Import Excel sheet to DataSetDim ds As DataSet = workbook.easy_ReadExcelWorksheet_AsDataSet(xlsSheet)
' Get the DataTable of the DataSetDim dataTable As DataTable = ds.Tables(0)
' Extract the first Excel row as headerDim header As Object() = dataTable.Rows(0).ItemArray
dataTable.Rows.RemoveAt(0)
' Set the data source of the DataGridView
dataGridView.DataSource = dataTable
' Set DataGridView header data from the first Excel rowFor column As Integer = 0 To dataTable.Columns.Count - 1
dataGridView.Columns(column).HeaderText = header(column)
Next' Set DataGridView header formatting (assuming that all header columns have the same format)Dim xlsStyleHeader As ExcelStyle = xlsSheet.easy_getExcelTable().easy_getCell(0, 0).getStyle()
dataGridView.ColumnHeadersDefaultCellStyle.BackColor = xlsStyleHeader.getBackground()
dataGridView.ColumnHeadersDefaultCellStyle.Font = _
New System.Drawing.Font(xlsStyleHeader.getFont(), xlsStyleHeader.getFontSize())
dataGridView.ColumnHeadersDefaultCellStyle.ForeColor = xlsStyleHeader.getForeground()
' Set DataGridView cell formatting (assuming that all cells have the same format)Dim xlsStyleData As ExcelStyle = xlsSheet.easy_getExcelTable().easy_getCell(1, 0).getStyle()
dataGridView.DefaultCellStyle.BackColor = xlsStyleData.getBackground()
' Set DataGridView column widthsFor column As Integer = 0 To dataTable.Columns.Count - 1
dataGridView.Columns(column).Width = xlsSheet.easy_getExcelTable().getColumnWidth(column)
Next
End Sub
EasyXLS also offers some open source code that imports an Excel file to a GridView. The code shows how to import all cell data and formatting, conditional formatting and the charts.
EasyXLS enables you to import Excel data to DataGridView either from the entire sheet or from a range of cells. Importing only a range of cells is a very useful option especially for large Excel files because it reduces the speed of the import process.
In order to import multiple cell ranges at once from Excel sheet, the range parameter must be passed to the method as union of ranges (multiple ranges separated by comma).
All the methods that allow importing Excel to DataSet have parameters that permit importing only ranges of cells.
Import XLSX, XLSB, XLSM and XLS files to DataGridView