
C# Convert Excel to DataTable
Exporting data from an Excel file (XLSX or XLS) to a DataTable in C# is straightforward when you use a robust library. With the Aspose.Cells API you can control export options, handle large workbooks, and immediately work with the populated DataTable for further processing. In this guide we’ll show how to convert Excel to DataTable in C# using Aspose.Cells.
In this post you will learn:
Excel to Datatable C# API installation
The Aspose.Cells library can be added to any .NET project in two simple ways:
- Download the full package from the official site and reference the DLLs manually.
- Install via NuGet – the recommended approach for automatic updates and dependency management.
Install-Package Aspose.Cells
Choose the method that fits your development workflow and add the reference to your project.
How to Convert Excel to DataTable in C# (Step-by-Step)
After installing the API, you can start converting worksheets to DataTables. The library provides a rich set of overloads that let you specify header rows, column ranges, and more.
Follow these steps:
- Create a Workbook object and load the source XLSX or XLS file.
- Retrieve the Worksheet you want to export.
- Call ExportDataTable to generate a DataTable that mirrors the worksheet’s content.
Here’s a complete example you can copy into your project:
Export Excel data to Datatable as a string
Sometimes a column contains mixed data types (numbers, dates, text). In such cases exporting values as strings prevents type‑conversion errors. Use the ExportDataTableAsString method to keep every cell value in its original textual representation.
Steps:
- Initialise a Workbook instance and load the Excel file.
- Select the target Worksheet.
- Invoke ExportDataTableAsString to obtain a DataTable where each cell is stored as a string.
Example code:
Get a Free License
You can request a free temporary license to evaluate Aspose.Cells without any evaluation restrictions.
Summing up
The Aspose.Cells C# API offers powerful, easy‑to‑use methods for exporting Excel data to a DataTable. You now know how to convert Excel to DataTable in C#, both with native data types and as strings. For additional scenarios, explore the full documentation and the comprehensive Getting Started guide.
Stay tuned to conholdate.com for more tutorials and updates.
Ask a question
Post your questions or share feedback on our forum.
FAQs
How do I convert Excel data to a table?
Install the Aspose.Cells C# API and use the ExportDataTable method to read an Excel worksheet directly into a DataTable. Code snippets are provided in the article.
What is the best way to read an Excel file in C#?
Refer to this article for a detailed walkthrough of reading and modifying Excel sheets programmatically with Aspose.Cells.