Home > DeveloperSection > Forums > How to import data from excel to datatable in c#
Pravesh Singh

Total Post:411

Points:2881
Posted on    January-10-2012 1:27 AM

 C# C# 
Ratings:


 5 Reply(s)
 30595  View(s)
Rate this:

Hello Everyone,

I have been facing a problem since last night, actually i want to import data from excel to datatable in C#.Net. So please resolve my problem as soon as possible.

Thanks in advance!!



Arun Singh

Total Post:68

Points:476
Posted on    January-10-2012 8:28 AM

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.


Modified On Jan-10-2012 08:30:31 AM

Laila prog

Total Post:1

Points:7
Posted on    March-21-2013 12:45 AM

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

anver sadat
anver sadat

Total Post:1

Points:7
Posted on    April-08-2013 10:34 PM

Dear arun,
             thnx for sharing your code, it saved my lots of time.i was getting error when application was deployed on server.
htnx

Sebastien Morin
Sebastien Morin

Total Post:1

Points:7
Posted on    February-19-2014 8:02 AM

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);
}

lethl marc
lethl marc

Total Post:1

Points:7
Posted on    October-19-2014 11:50 PM

                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

 

 

 


Modified On Oct-20-2014 01:43:08 AM

Don't want to miss updates? Please click the below button!

Follow MindStick