User GuideFAQHow to import Excel file to GridView in C# and VB.NET
How to import Excel file to GridView in C# and VB.NET
EasyXLS™ library allows you to import Excel data to GridView. 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.Linq;
using EasyXLS;
namespace WebApplication
{
public partial class ImportToGridView : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
this.BindGridView();
}
private void BindGridView()
{
// 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 GridView.xlsx");
// Get the DataTable of the DataSet
DataTable dataTable = ds.Tables[0];
// Set GridView header from the first Excel rowfor (int column = 0; column < dataTable.Columns.Count; column++)
{
gridView.Columns[column].HeaderText = (string)dataTable.Rows[0][column];
}
dataTable.Rows.RemoveAt(0);
// Set the data source of the GridView
gridView.DataSource = dataTable;
gridView.DataBind();
}
}
}
Imports EasyXLS
Partial Public Class ImportToGridView
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
BindGridView()
End SubPrivate Sub BindGridView()
' Create an instance of the class that imports Excel filesDim workbook As New ExcelDocument()
' Import Excel file to DataSetDim ds = workbook.easy_ReadXLSXActiveSheet_AsDataSet("C:\Samples\Excel to GridView.xlsx")
' Get the DataTable of the DataSetDim dataTable = ds.Tables(0)
' Set GridView header from the first Excel rowFor column As Integer = 0 To dataTable.Columns.Count - 1
gridView.Columns(column).HeaderText = dataTable.Rows(0)(column)
Next
dataTable.Rows.RemoveAt(0)
' Set the data source of the GridView
gridView.DataSource = dataTable
gridView.DataBind()
End Sub
End Class
Import Excel cell formatting to GridView
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 BindGridView()
{
// Create an instance of the class that imports Excel files
ExcelDocument workbook = new ExcelDocument();
// Import Excel file
workbook.easy_LoadXLSXFile("C:\\Samples\\Excel to GridView.xlsx");
// Get the first sheet from the Excel file
ExcelWorksheet xlsSheet = (ExcelWorksheet)workbook.easy_getSheetAt(0);
// Import Excel file to DataSet
DataSet ds = workbook.easy_ReadExcelWorksheet_AsDataSet(xlsSheet);
// Get the DataTable of the DataSet
DataTable dataTable = ds.Tables[0];
// Set GridView header from the first Excel rowfor (int column = 0; column < dataTable.Columns.Count; column++)
{
gridView.Columns[column].HeaderText = (string)dataTable.Rows[0][column];
}
dataTable.Rows.RemoveAt(0);
// Set the data source of the GridView
gridView.DataSource = dataTable;
gridView.DataBind();
// Set GridView header formatting (assuming that all header columns have the same format)
ExcelStyle xlsStyleHeader = xlsSheet.easy_getExcelTable().easy_getCell(0, 0).getStyle();
gridView.HeaderStyle.BackColor = xlsStyleHeader.getBackground();
gridView.HeaderStyle.Font.Name = xlsStyleHeader.getFont();
gridView.HeaderStyle.ForeColor = xlsStyleHeader.getForeground();
// Set GridView cell formatting (assuming that all cells have the same format)
ExcelStyle xlsStyleData = xlsSheet.easy_getExcelTable().easy_getCell(1, 0).getStyle();
gridView.RowStyle.BackColor = xlsStyleData.getBackground();
gridView.RowStyle.Font.Name = xlsStyleData.getFont();
gridView.RowStyle.ForeColor = xlsStyleData.getForeground();
}
Private Sub BindGridView()
' Create an instance of the class that imports Excel filesDim workbook As New ExcelDocument()
' Import Excel file
workbook.easy_LoadXLSXFile("C:\Samples\Excel to GridView.xlsx")
' Get the first sheet from the Excel fileDim xlsSheet As ExcelWorksheet = workbook.easy_getSheetAt(0)
' Import Excel file to DataSetDim ds = workbook.easy_ReadExcelWorksheet_AsDataSet(xlsSheet)
' Get the DataTable of the DataSetDim dataTable = ds.Tables(0)
' Set GridView header from the first Excel rowFor column As Integer = 0 To dataTable.Columns.Count - 1
gridView.Columns(column).HeaderText = dataTable.Rows(0)(column)
Next
dataTable.Rows.RemoveAt(0)
' Set the data source of the GridView
gridView.DataSource = dataTable
gridView.DataBind()
' Set GridView header formatting (assuming that all header columns have the same format)Dim xlsStyleHeader = xlsSheet.easy_getExcelTable().easy_getCell(0, 0).getStyle()
gridView.HeaderStyle.BackColor = xlsStyleHeader.getBackground()
gridView.HeaderStyle.Font.Name = xlsStyleHeader.getFont()
gridView.HeaderStyle.ForeColor = xlsStyleHeader.getForeground()
' Set GridView cell formatting (assuming that all cells have the same format)Dim xlsStyleData = xlsSheet.easy_getExcelTable().easy_getCell(1, 0).getStyle()
gridView.RowStyle.BackColor = xlsStyleData.getBackground()
gridView.RowStyle.Font.Name = xlsStyleData.getFont()
gridView.RowStyle.ForeColor = xlsStyleData.getForeground()
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 GridView 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.