How to Convert Excel to Datatable in C# Without oledb
Microsoft Excel is a robust spreadsheet program created by Microsoft. It's a widely used tool for data organization, analysis, and visualization. It is a part of the Office suite from Microsoft. Because of its various features, Excel is a versatile tool that may be used by professionals, individuals, organizations, and universities.
Excel is widely used in a wide range of sectors, including finance, accounting, business analysis, data analysis, research, education, and more. Due to its versatility and extensive feature set, it is the go-to tool for organizing, assessing, and presenting data in both personal and professional situations. In this article, we are going to convert Excel to a DataTable in C# without OLEDB connectivity.
How to Convert Excel to DataTable in C# without OLEDB
- Create a new Visual Studio project.
- Install the required library.
- Load the Excel file into an object.
- Convert the Excel sheet data into a DataTable using specific methods.
- Use the DataTable as required.
IronXL Library
An alternative to Microsoft Interop for handling Excel files in .NET programs is IronXL. While Microsoft Interop necessitates using the Interop assemblies to connect with Excel, IronXL provides a simpler, more efficient, and more potent way to manipulate Excel files programmatically in .NET environments.
The following are some advantages of using IronXL:
- Performance and Resource Economy: IronXL outperforms Microsoft Interop in terms of performance and resource efficiency as it is not reliant on the Excel program being installed on the PC.
- Readability and Simplicity: IronXL offers a simpler API, making it easier to read, write, and manipulate Excel files without the drawbacks of Microsoft Interop.
- Compatibility and Dependency: IronXL removes dependencies and potential compatibility issues with different Office or Excel versions by not needing Microsoft Excel to be installed on the PC.
- Platform Independence: While Microsoft Interop is more closely linked to certain Microsoft Office versions, IronXL provides more flexibility and simplicity of deployment in a wider range of settings and platforms.
Because IronXL is faster, easier to use, and requires fewer third-party software installs, it is often a preferable choice for .NET developers who need to work with Excel files programmatically. However, factors like the project's specifics, the pre-existing infrastructure, and the user's familiarity with each library may affect their choice.
Always consider the needs of your application when choosing among these solutions. For further information about the IronXL library, visit this website.
Creating a New Project in Visual Studio
To open Visual Studio, click on File from the menu and select "New Project." Then, choose "Windows Forms Application."

After selecting the file location, enter the project name in the appropriate text area. Next, click the "Create" button and choose the necessary .NET Framework, as seen in the sample below.

The type of application chosen will dictate how the Visual Studio project is structured. To add code, you may enter the Program.cs file and construct or run the application using the console, Windows, or web application.

Next, add the necessary library and test the code.
Installing IronXL Library
To install the IronXL library, open the NuGet Package Manager Console and enter the following command:
Install-Package IronXL.Excel

Alternatively, you can use the NuGet Package Manager to search for the package "IronXL." This will display a list of NuGet packages related to IronXL, from which you can choose the one you need.

Convert Excel File to Data Table
The following code demonstrates how to read an Excel file and convert it into a DataTable after installing the package.
// Necessary namespaces for IronXL and application namespaces
using IronXL;
using System;
using System.Data;
using System.Windows.Forms;
namespace DataTableWindowsForm
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
/// <summary>
/// Exports data from an Excel file specified by the user through a file dialog.
/// </summary>
/// <param name="filename">The name of the file to export data from.</param>
private void ExportData(string filename)
{
string importFilePath = string.Empty;
openFileDialog1.Filter = "Excel (*.xlsx)|*.xlsx";
DialogResult result = openFileDialog1.ShowDialog();
if (result == DialogResult.OK)
{
importFilePath = openFileDialog1.FileName;
}
if (!string.IsNullOrEmpty(importFilePath))
{
// Load the Excel document
var excelDoc = WorkBook.Load(importFilePath);
// Select the first worksheet
var worksheet = excelDoc.WorkSheets[0];
// Convert worksheet to a DataTable
DataTable dt = worksheet.ToDataTable();
// Bind DataTable to the DataGridView
dataGridView1.DataSource = dt;
}
}
}
}// Necessary namespaces for IronXL and application namespaces
using IronXL;
using System;
using System.Data;
using System.Windows.Forms;
namespace DataTableWindowsForm
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
/// <summary>
/// Exports data from an Excel file specified by the user through a file dialog.
/// </summary>
/// <param name="filename">The name of the file to export data from.</param>
private void ExportData(string filename)
{
string importFilePath = string.Empty;
openFileDialog1.Filter = "Excel (*.xlsx)|*.xlsx";
DialogResult result = openFileDialog1.ShowDialog();
if (result == DialogResult.OK)
{
importFilePath = openFileDialog1.FileName;
}
if (!string.IsNullOrEmpty(importFilePath))
{
// Load the Excel document
var excelDoc = WorkBook.Load(importFilePath);
// Select the first worksheet
var worksheet = excelDoc.WorkSheets[0];
// Convert worksheet to a DataTable
DataTable dt = worksheet.ToDataTable();
// Bind DataTable to the DataGridView
dataGridView1.DataSource = dt;
}
}
}
}' Necessary namespaces for IronXL and application namespaces
Imports IronXL
Imports System
Imports System.Data
Imports System.Windows.Forms
Namespace DataTableWindowsForm
Partial Public Class Form1
Inherits Form
Public Sub New()
InitializeComponent()
End Sub
''' <summary>
''' Exports data from an Excel file specified by the user through a file dialog.
''' </summary>
''' <param name="filename">The name of the file to export data from.</param>
Private Sub ExportData(ByVal filename As String)
Dim importFilePath As String = String.Empty
openFileDialog1.Filter = "Excel (*.xlsx)|*.xlsx"
Dim result As DialogResult = openFileDialog1.ShowDialog()
If result = System.Windows.Forms.DialogResult.OK Then
importFilePath = openFileDialog1.FileName
End If
If Not String.IsNullOrEmpty(importFilePath) Then
' Load the Excel document
Dim excelDoc = WorkBook.Load(importFilePath)
' Select the first worksheet
Dim worksheet = excelDoc.WorkSheets(0)
' Convert worksheet to a DataTable
Dim dt As DataTable = worksheet.ToDataTable()
' Bind DataTable to the DataGridView
dataGridView1.DataSource = dt
End If
End Sub
End Class
End NamespaceIn the above code:
- The Windows Form loads the Excel files and converts them into a new DataTable.
- It uses an open file dialog to let the user choose an Excel file.
- The
WorkBook.Load()method from IronXL is used to load the Excel file into a Workbook object. - The first worksheet is selected using an index or name.
- The
ToDataTablemethod is used to turn the worksheet data into a DataTable. - The filled DataTable is set as the data source for a DataGridView control on the Windows Form.
At the bottom, a form and a button load the Excel file into the application.

When the user clicks the "Load Excel" button, it will prompt a dialog allowing the user to select a file.

It will then process the selected file, convert it into a DataTable, and load it into a DataGridView.

Learn more about using IronXL with DataTables.
Conclusion
IronXL is one of the most commonly used Excel add-ons that does not depend on any extra external libraries. Installing Microsoft Excel is unnecessary as it is a self-contained program. It can manipulate Excel files in various ways. In contrast, the Interop library requires other libraries to parse files and edit Word documents.
IronXL provides a comprehensive solution for any programming process using Microsoft Excel documents, including operations like calculations, sorting, merging, and file storage. It facilitates handling Excel data, reading, and writing files more efficiently.
Originally, IronXL was priced at $799. Users can receive software updates and support by paying a one-year membership fee. IronXL also offers protection against illegal redistribution for a fee. Click here to try IronXL with a free trial for more detailed pricing information. Visit the Iron Software home page for further information on other Iron software products.
Frequently Asked Questions
How can I convert Excel data to a DataTable in C# without using OLEDB?
To convert Excel data to a DataTable in C# without using OLEDB, you can use the IronXL library. First, set up a new Visual Studio project and install IronXL. Load the Excel file with the WorkBook.Load() method, select the worksheet, and then use the ToDataTable method to convert the data.
What is the advantage of using IronXL over Microsoft Interop for Excel operations?
IronXL offers better performance, is resource-efficient, and simplifies the process of Excel file manipulation. It does not require Microsoft Excel to be installed, thus avoiding compatibility issues with different Office versions.
How do I install IronXL in my C# project?
You can install IronXL in your C# project by opening the NuGet Package Manager Console in Visual Studio and running the command Install-Package IronXL. Alternatively, you can search for IronXL in the NuGet Package Manager and install it directly.
Is it necessary to have Microsoft Excel installed to use IronXL?
No, it is not necessary to have Microsoft Excel installed to use IronXL. This library functions independently of Excel, eliminating any dependency or compatibility issues.
What are the benefits of converting Excel to DataTable using IronXL?
Converting Excel to DataTable using IronXL provides excellent performance, ease of use, and does not require additional software installations. This method allows for efficient manipulation of Excel files in .NET applications.
Can IronXL handle Excel files across different platforms?
Yes, IronXL is platform-independent and can handle Excel files across different environments without requiring any specific version of Microsoft Excel.
Is there a way to try IronXL before purchasing?
Yes, IronXL offers a free trial version that allows users to explore its features before making a purchase. You can find more information and download the trial from the IronXL website.
What types of projects benefit most from using IronXL?
Projects that involve Excel data manipulation, such as data analysis, business intelligence applications, and any .NET applications requiring Excel file processing, can greatly benefit from using IronXL.
How does IronXL manage Excel file manipulation programmatically?
IronXL allows developers to load, read, and manipulate Excel files programmatically through its intuitive API, without relying on Excel's installed components, making it an efficient tool for .NET applications.









