11/4/22, 22:58 Read (Import) Excel file without OLEDB, Microsoft Office or Interop Library in C# and VB.
Net
Read (Import) Excel file without OLEDB, Microsoft Office or Interop Library in C# and VB.Net (/Articles/Read-Import-Excel-file-without-OLEDB-Mic
rosoft-Office-or-Interop-Library-in-C-and-VBNet.aspx)
01 Jun 2018
Mudassar Khan (/Authors/Mudassar-Khan.aspx)
1
Comments
54128 Views
Excel (/Categories/Excel.aspx) DataGridView (/Categories/DataGridView.aspx) Windows Forms (/Categories/Windows-Forms.aspx) DataTable (/Categories/DataTable.aspx) OpenXml (/Categories/OpenXml.aspx) ClosedXml
(/Categories/ClosedXml.aspx)
Here Mudassar Ahmed Khan has explained with an example, how to read (import) Excel file without using OLEDB or installing Microsoft Office Excel or Interop Library in C# and VB.Net.
The Excel file will be read using ClosedXml and OpenXml DLLs which does not require OLEDB and does not require Microsoft Office Excel or Interop Library software to be installed.
Download
Download Free Word/PDF/Excel API (https://www.aspsnippets.com/Redirect.aspx?AdId=10567&RedirectUrl=https%3A%2F%2Fwww.e-iceblue.com%2FIntroduce%2Fspire-office-for-net-free.html%3Faff_id%3D9
In this article I will explain with an example, how to read (import) Excel file without using OLEDB or installing Microsoft Office Excel or Interop Library in C# and VB.Net.
The Excel file will be read using ClosedXml and OpenXml DLLs which does not require OLEDB and does not require Microsoft Office Excel or Interop Library software to be installed.
Download DocumentFormat.OpenXml and ClosedXml Libraries
You can download the libraries using the following download locations.
Download OpenXml SDK 2.0 (http://www.microsoft.com/en-in/download/details.aspx?id=5124)
Download ClosedXml Library (https://github.com/ClosedXML/ClosedXML)
Note: The DLL files of both OpenXml and ClosedXml are present in the attached sample.
Form Controls
I have added a DataGridView and a Button to the Windows Form. I have also added an OpenFileDialog to the Form which allow us to select the excel file.
https://www.aspsnippets.com/Articles/Read-Import-Excel-file-without-OLEDB-Microsoft-Office-or-Interop-Library-in-C-and-VBNet.aspx 1/8
11/4/22, 22:58 Read (Import) Excel file without OLEDB, Microsoft Office or Interop Library in C# and VB.Net
Namespaces
You will need to import the following namespaces.
C#
using System.IO;
using System.Data;
using ClosedXML.Excel;
VB.Net
Imports System.IO
Imports System.Data
Imports ClosedXML.Excel
Selecting the Excel File
When the Select File button is clicked, it shows the Open File Dialog, using which we need to select the Excel file we want to import to DataGridView control.
C#
private void btnSelect_Click(object sender, EventArgs e)
{
openFileDialog1.ShowDialog();
}
VB.Net
Private Sub btnSelect_Click(sender As Object, e As EventArgs) Handles btnSelect.Click
OpenFileDialog1.ShowDialog()
https://www.aspsnippets.com/Articles/Read-Import-Excel-file-without-OLEDB-Microsoft-Office-or-Interop-Library-in-C-and-VBNet.aspx 2/8
11/4/22, 22:58 Read (Import) Excel file without OLEDB, Microsoft Office or Interop Library in C# and VB.Net
End Sub
Importing the Excel File’s Sheet to DataGridView control
When the Open Button is clicked, the following event handler is executed. First the path of the selected Excel file is read and then the Excel file is read using the ClosedXml WorkBook class object.
Then the instance of the first Sheet is determined and all the rows present in the Sheet are fetched.
Finally a loop is executed over the fetched rows and a DataTable is populated which is then used to populate the DataGridView.
C#
private void openFileDialog1_FileOk(object sender, System.ComponentModel.CancelEventArgs e)
{
string filePath = openFileDialog1.FileName;
//Open the Excel file using ClosedXML.
using (XLWorkbook workBook = new XLWorkbook(filePath))
{
//Read the first Sheet from Excel file.
IXLWorksheet workSheet = workBook.Worksheet(1);
//Create a new DataTable.
DataTable dt = new DataTable();
//
https://www.aspsnippets.com/Articles/Read-Import-Excel-file-without-OLEDB-Microsoft-Office-or-Interop-Library-in-C-and-VBNet.aspx 3/8
11/4/22, 22:58 Read (Import) Excel file without OLEDB, Microsoft Office or Interop Library in C# and VB.Net
//Loop through the Worksheet rows.
bool firstRow = true;
foreach (IXLRow row in workSheet.Rows())
{
//Use the first row to add columns to DataTable.
if (firstRow)
{
foreach (IXLCell cell in row.Cells())
{
dt.Columns.Add(cell.Value.ToString());
}
firstRow = false;
}
else
{
//Add rows to DataTable.
dt.Rows.Add();
int i = 0;
foreach (IXLCell cell in row.Cells())
{
dt.Rows[dt.Rows.Count - 1][i] = cell.Value.ToString();
i++;
}
}
dataGridView1.DataSource = dt;
}
}
}
VB.Net
Private Sub openFileDialog1_FileOk(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles OpenFileDialog1.FileOk
Dim filePath As String = OpenFileDialog1.FileName
'Open the Excel file using ClosedXML.
Using workBook As New XLWorkbook(filePath)
'Read the first Sheet from Excel file.
Dim workSheet As IXLWorksheet = workBook.Worksheet(1)
'Create a new DataTable.
Dim dt As New DataTable()
https://www.aspsnippets.com/Articles/Read-Import-Excel-file-without-OLEDB-Microsoft-Office-or-Interop-Library-in-C-and-VBNet.aspx 4/8
11/4/22, 22:58 Read (Import) Excel file without OLEDB, Microsoft Office or Interop Library in C# and VB.Net
'Loop through the Worksheet rows.
Dim firstRow As Boolean = True
For Each row As IXLRow In workSheet.Rows()
'Use the first row to add columns to DataTable.
If firstRow Then
For Each cell As IXLCell In row.Cells()
dt.Columns.Add(cell.Value.ToString())
Next
firstRow = False
Else
'Add rows to DataTable.
dt.Rows.Add()
Dim i As Integer = 0
For Each cell As IXLCell In row.Cells()
dt.Rows(dt.Rows.Count - 1)(i) = cell.Value.ToString()
i += 1
Next
End If
dataGridView1.DataSource = dt
Next
End Using
End Sub
Screenshots
Excel File
D t G idVi ith E ld t
https://www.aspsnippets.com/Articles/Read-Import-Excel-file-without-OLEDB-Microsoft-Office-or-Interop-Library-in-C-and-VBNet.aspx 5/8
11/4/22, 22:58 Read (Import) Excel file without OLEDB, Microsoft Office or Interop Library in C# and VB.Net
DataGridView with Excel data
Downloads
Download
Download Free Word/PDF/Excel API (https://www.aspsnippets.com/Redirect.aspx?AdId=10567&RedirectUrl=https%3A%2F%2Fwww.e-iceblue.com%2FIntroduce%2Fspire-office-for-net-free.html%3Faff_id%3D91)
Related Articles
The OLE DB provider Microsoft.Ace.OLEDB.12.0 for linked server (null) (/Articles/The-OLE-DB-provider-Microsoft.Ace.OLEDB.12.0-for-linked-server-null.aspx)
Here Mudassar Ahmed Khan has provided solutions to the issue The OLE DB provider ampquotMicrosoft.Ace.OLEDB.12.0ampquot for linked server ampquot(null)ampquot reported
an error. Access denied. while
Comments
Gilberto Espejel
— Aug 09, 2021 10:08 PM 189.160.215.224
Amazing tutorial super fast to implement and exactly what I needed. Thank you so much.
Add Comments
https://www.aspsnippets.com/Articles/Read-Import-Excel-file-without-OLEDB-Microsoft-Office-or-Interop-Library-in-C-and-VBNet.aspx 6/8
11/4/22, 22:58 Read (Import) Excel file without OLEDB, Microsoft Office or Interop Library in C# and VB.Net
You can add your comment about this article using the form below. Make sure you provide a valid email address
else you won't be notified when the author replies to your comment
Please note that all comments are moderated and will be deleted if they are
Not relavant to the article
Spam
Advertising campaigns or links to other sites
Abusive content.
Please do not post code, scripts or snippets.
Name
Name
Email
Email
Comment
Comment
Security code:
I declare, I accept the site's
Privacy Policy
(/PrivacyPolicy.aspx).
Add Comment
What our readers say
Doug
Just want to thank you very much for this article. I have spent hours searching for a solution to a very similar page I am building and I was giving up hope on getting it to work.
Thanks to this page and this site.
https://www.aspsnippets.com/Articles/Read-Import-Excel-file-without-OLEDB-Microsoft-Office-or-Interop-Library-in-C-and-VBNet.aspx 7/8
11/4/22, 22:58 Read (Import) Excel file without OLEDB, Microsoft Office or Interop Library in C# and VB.Net
©
2022
www.aspsnippets.com (https://www.aspsnippets.com/)
All rights reserved | Privacy Policy (/PrivacyPolicy.aspx)
| Powered by Excelasoft Solutions (http://www.excelasoft.com/)
(https://www.facebook.com/pages/ASPSnippets/306994206006035)
(https://plus.google.com/110371172807820981480) (https://twitter.com/aspsnippets)
(/Rss.ashx)
https://www.aspsnippets.com/Articles/Read-Import-Excel-file-without-OLEDB-Microsoft-Office-or-Interop-Library-in-C-and-VBNet.aspx 8/8