User GuideFAQHow to import Excel file to DataGrid in C# and VB.NET
How to import Excel file to DataGrid in C# and VB.NET
EasyXLS™ library allows you to import Excel data to DataGrid. 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 class ImportExcelForm : Form
{
private System.Windows.Forms.DataGrid dataGrid;
private System.ComponentModel.Container components = null;
public ImportExcelForm()
{
InitializeComponent();
FillDataGrid();
}
[STAThread]
static void Main()
{
Application.Run(new ImportExcelForm());
}
private void FillDataGrid()
{
// 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 DataGrid.xlsx");
// Get the DataTable of the DataSet
DataTable dataTable = ds.Tables[0];
// Set first Excel row as DataGrid header for (int column = 0; column < dataTable.Columns.Count; column++)
{
dataTable.Columns[column].ColumnName = (string)dataTable.Rows[0][column];
}
dataTable.Rows.RemoveAt(0);
// Set the data source of the DataGrid
dataGrid.DataSource = dataTable;
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
private void InitializeComponent()
{
this.dataGrid = new System.Windows.Forms.DataGrid();
((System.ComponentModel.ISupportInitialize)(this.dataGrid)).BeginInit();
this.SuspendLayout();
//
// dataGrid
//this.dataGrid.DataMember = "";
this.dataGrid.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.dataGrid.Location = new System.Drawing.Point(24, 24);
this.dataGrid.Name = "dataGrid";
this.dataGrid.Size = new System.Drawing.Size(464, 208);
this.dataGrid.TabIndex = 0;
//
// ImportExcelForm
//this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(544, 273);
this.Controls.Add(this.dataGrid);
this.Name = "ImportExcelForm";
this.Text = "Import Excel to DataGrid in C#";
((System.ComponentModel.ISupportInitialize)(this.dataGrid)).EndInit();
this.ResumeLayout(false);
}
}
}
Imports EasyXLS
Public Class ImportExcelForm
Inherits System.Windows.Forms.Form
Friend WithEvents dgDataGrid As System.Windows.Forms.DataGrid
Private Sub ImportExcelForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles MyBase.Load
InitializeComponent()
FillDataGrid()
End Sub
Private Sub FillDataGrid()
' Create an instance of the class that imports Excel filesDim workbook = New ExcelDocument
' Import Excel file to DataSetDim ds As DataSet = workbook.easy_ReadXLSXActiveSheet_AsDataSet("C:\Samples\Excel to DataGrid.xlsx")
' Get the DataTable of the DataSetDim dataTable As DataTable = ds.Tables(0)
' Set first Excel row as DataGrid headerFor column As Integer = 0 To dataTable.Columns.Count() - 1
dataTable.Columns(column).ColumnName = dataTable.Rows(0)(column)
Next
dataTable.Rows.RemoveAt(0)
' Set the data source of the DataGrid
dgDataGrid.DataSource = dataTable
End Sub
Private Sub InitializeComponent()
Me.dgDataGrid = New System.Windows.Forms.DataGrid
CType(Me.dgDataGrid, System.ComponentModel.ISupportInitialize).BeginInit()
Me.SuspendLayout()
'
' dataGrid
'Me.dgDataGrid.DataMember = ""Me.dgDataGrid.HeaderForeColor = System.Drawing.SystemColors.ControlText
Me.dgDataGrid.Location = New System.Drawing.Point(24, 24)
Me.dgDataGrid.Name = "dataGrid"Me.dgDataGrid.Size = New System.Drawing.Size(464, 208)
Me.dgDataGrid.TabIndex = 0
'
' ImportExcelForm
'Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(544, 273)
Me.Controls.Add(Me.dgDataGrid)
Me.Name = "ImportExcelForm"Me.Text = "Import Excel to DataGrid in VB.NET"CType(Me.dgDataGrid, System.ComponentModel.ISupportInitialize).EndInit()
Me.ResumeLayout(False)
End Sub
End Class
Import Excel cell formatting to DataGrid
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 FillDataGrid()
{
// Create an instance of the class that imports Excel files
ExcelDocument workbook = new ExcelDocument();
// Import Excel file
workbook.easy_LoadXLSXFile("C:\\Samples\\Excel to DataGrid.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];
// Set first Excel row as DataGrid headerfor (int column = 0; column < dataTable.Columns.Count; column++)
{
dataTable.Columns[column].ColumnName = (string)dataTable.Rows[0][column];
}
dataTable.Rows.RemoveAt(0);
// Set the data source of the DataGrid
dataGrid.DataSource = dataTable;
// Set DataGrid header formatting (assuming that all header columns have the same format)
ExcelStyle xlsStyleHeader = xlsSheet.easy_getExcelTable().easy_getCell(0, 0).getStyle();
dataGrid.HeaderBackColor = xlsStyleHeader.getBackground();
dataGrid.HeaderFont = new System.Drawing.Font
(xlsStyleHeader.getFont(), xlsStyleHeader.getFontSize());
dataGrid.HeaderForeColor = xlsStyleHeader.getForeground();
// Set DataGrid cell formatting (assuming that the cells have an alternating style format)
ExcelStyle xlsStyleData = xlsSheet.easy_getExcelTable().easy_getCell(1, 0).getStyle();
dataGrid.BackColor = xlsStyleData.getBackground();
dataGrid.Font = new System.Drawing.Font(xlsStyleData.getFont(), xlsStyleData.getFontSize());
dataGrid.ForeColor = xlsStyleData.getForeground();
ExcelStyle xlsStyleDataAlternating = xlsSheet.easy_getExcelTable().easy_getCell(2, 0).getStyle();
dataGrid.AlternatingBackColor = xlsStyleDataAlternating.getBackground();
}
Private Sub FillDataGrid()
' Create an instance of the class that imports Excel filesDim workbook = New ExcelDocument
' Import Excel file
workbook.easy_LoadXLSXFile("C:\Samples\Excel to DataGrid.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)
' Set first Excel row as DataGrid headerFor column As Integer = 0 To dataTable.Columns.Count() - 1
dataTable.Columns(column).ColumnName = dataTable.Rows(0)(column)
Next
dataTable.Rows.RemoveAt(0)
' Set the data source of the DataGrid
dgDataGrid.DataSource = dataTable
' Set DataGrid header formatting (assuming that all header columns have the same format)Dim xlsStyleHeader As ExcelStyle = xlsSheet.easy_getExcelTable().easy_getCell(0, 0).getStyle()
dgDataGrid.HeaderBackColor = xlsStyleHeader.getBackground()
dgDataGrid.HeaderFont = New System.Drawing.Font(xlsStyleHeader.getFont(), xlsStyleHeader.getFontSize())
dgDataGrid.HeaderForeColor = xlsStyleHeader.getForeground()
' Set DataGrid cell formatting (assuming that the cells have an alternating style format)Dim xlsStyleData As ExcelStyle = xlsSheet.easy_getExcelTable().easy_getCell(1, 0).getStyle()
dgDataGrid.BackColor = xlsStyleData.getBackground()
dgDataGrid.Font = New System.Drawing.Font(xlsStyleData.getFont(), xlsStyleData.getFontSize())
dgDataGrid.ForeColor = xlsStyleData.getForeground()
Dim xlsStyleDataAlternating As ExcelStyle = xlsSheet.easy_getExcelTable().easy_getCell(2, 0).getStyle()
dgDataGrid.AlternatingBackColor = xlsStyleDataAlternating.getBackground()
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 DataGrid 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.