---
title: "How to import data from excel to datatable in c#"  
description: "How to import data from excel to datatable in c#"  
author: "Pravesh Singh"  
published: 2012-01-10  
updated: 2014-10-19  
canonical: https://www.mindstick.com/forum/373/how-to-import-data-from-excel-to-datatable-in-c-sharp  
category: "c#"  
tags: ["c#"]  
reading_time: 2 minutes  

---

# How to import data from excel to datatable in c#

Hello Everyone,

I have been facing a [problem](https://yourviews.mindstick.com/view/81399/tackling-the-problem-of-unemployment-during-corona-pandemic) since last night, actually i want to [import](https://www.mindstick.com/blog/294/how-to-import-or-export-sql-server-table-data-in-ms-excel-sheet-using-c-sharp-code) [data](https://www.mindstick.com/articles/13050/salesforce-aiming-to-dominate-predictive-analytics-with-data-science) from [excel](https://www.mindstick.com/articles/12937/how-to-import-excel-data-in-sql-server-2014) to [datatable in C#](https://www.mindstick.com/forum/12872/getting-count-of-all-repeated-rows-in-a-datatable-in-c-sharp).Net. So please [resolve](https://www.mindstick.com/forum/159427/windows-app-dll-not-found-resolve-library-issue) my problem as soon as possible.

Thanks in [advance](https://www.mindstick.com/blog/33258/jee-mains-and-jee-advance-exams)!!

## Replies

### Reply by lethl marc

MyConnection = new System.Data.OleDb.OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0;Data Source='c:\\yourfile.xls';Extended Properties=Excel 8.0;");\
MyCommand = new System.Data.OleDb.OleDbDataAdapter("select * from [Sheet1$]", MyConnection);\
MyCommand.TableMappings.Add("Table", "TestTable");\
DtSet = new System.Data.DataSet();\
MyCommand.Fill(DtSet);\

\

Lethal

### Reply by Sebastien Morin

Thank you very much for your solution. For future reference, I've modified your code to ensure object disposal:\

```
System.Data.DataTable dtExcel = new System.Data.DataTable();dtExcel.TableName = "MyExcelData";string SourceConstr = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source='MyExcelFilePath';Extended Properties= 'Excel 8.0;HDR=Yes;IMEX=1'";using (OleDbConnection con = new OleDbConnection(SourceConstr))using (OleDbDataAdapter data = new OleDbDataAdapter("Select * from [Sheet1$]", con)){data.Fill(dtExcel);}
```

### Reply by anver sadat

Dear arun,\
thnx for sharing your code, it saved my lots of time.i was getting error when application was deployed on server.\
htnx

### Reply by Laila prog

Hello, \
I want to import data from an Excel file into a database SQL Server 2008, the Excel file contains multiple sheets, each sheet represents a table row e in the database, and I must also respect the constraints intergrités, if you can offer me a code that does almost the same thing THANKS

### Reply by Anonymous User

Hello Pravesh Singh,

Check this code...

```
System.Data.DataTable
dtExcel = new System.Data.DataTable();dtExcel.TableName = "MyExcelData";string SourceConstr = @"Provider=Microsoft.ACE.OLEDB.12.0;Data
Source='MyExcelFilePath';Extended Properties= 'Excel 8.0;HDR=Yes;IMEX=1'";OleDbConnection con = new OleDbConnection(SourceConstr);string query = "Select
* from [Sheet1$]";OleDbDataAdapter data = new OleDbDataAdapter(query,
con);data.Fill(dtExcel);
```

It might be resolve your problem.


---

Original Source: https://www.mindstick.com/forum/373/how-to-import-data-from-excel-to-datatable-in-c-sharp

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
