Click here to Skip to main content
15,881,812 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have one xls file which has 198000 rows. Whenever I try to import it in sql database table then I am getting following error to open OLE db connection.

External table is not in the expected format.


My code:

 string con = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + Server.MapPath("TowerWorkOrders20110316.xls") + ";Extended Properties=\"Excel 12.0\"";
        //string con = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("TowerWorkOrders20110317.xls") + ";Extended Properties=Excel 8.0";
        OleDbConnection connection = new OleDbConnection(con);
        OleDbDataReader reader;
        connection.Open();
        OleDbCommand command = new OleDbCommand("SELECT * FROM [Sheet1$]", connection);
        reader = command.ExecuteReader();
.........
........


can anyone give me the solution?

Regard
Imrankhan
Posted
Updated 11-Apr-11 22:51pm
v2
Comments
Manfred Rudolf Bihy 12-Apr-11 4:53am    
Edit: Removed the lacs (= 100000) reference and wrote out the number so that everybody understands what OP is talking about.

Here you have a nice article about reading and writing excel using oledb.

Reading and Writing Excel using OLEDB[^]

You can use it to check out what you are actually selecting and check it with the table you want to write it to. Also be sure to check the data types and whether maybe a row with headers is the problem.

Good luck!
 
Share this answer
 
Comments
Imran Khan Pathan 12-Apr-11 4:33am    
I find out the problem. My xls file is in TAB delimeter format and because of this I am getting external table format error.

Is there anyway to read tab delimeter xls file?

I convert it into .csv format and when I read .csv file then it returns only first column records. I have total 33 columns in file.
Manfred Rudolf Bihy 12-Apr-11 4:57am    
See my answer! It has a link to a great article here on CP that shows you how you can achieve your goal.
E.F. Nijboer 12-Apr-11 5:01am    
But then it isn't an actual excel file, simply a tab delimited file. You can have a look at the oledb for text files. This article can help you out. Another way is to simply read and process the file yourself. That should be that hard as well.
http://www.codeproject.com/KB/cs/UsingJetForImport.aspx
See this great article here on CP to find out how to achieve this for text files (csv): Using OleDb to Import Text Files (tab, CSV, custom)[^].

Happy coding!

-MRB
 
Share this answer
 

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