Click here to Skip to main content
15,890,825 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
hi everyone,
i want to convert a excel file data into datatable
I have a data like 9****** is mobile number in excel sheet

I tried following code but it does not work

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


please help me....

Thanks in advance...
Posted
Updated 20-Nov-13 1:20am
v5
Comments
Thomas ktg 20-Nov-13 7:25am    
Do you get any error?
Not working? Have you debugged and check if it is filling to the DataTable or not?
Rahul JR 20-Nov-13 7:35am    
now work it i had taken a just file name instand of full path of the file...
but thanks...
♥…ЯҠ…♥ 20-Nov-13 7:40am    
Code should work perfectly... do you have a file in that filename path? in that file do you have [sheet1$]? do you have content in it?

1 solution

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

Take a full path of the file
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900