0

Here's my code which is in the beginning of a method for converting .xls file to .csv.

sourceFile="C:\\Users\\myUser\\Desktop\\Folder\\myFile.xls";

string strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + sourceFile + ";Extended Properties=\"Excel 8.0;HDR=No;IMEX=1\"";

OleDbConnection conn = new OleDbConnection(strConn);

conn.Open();

And it crashes on the last line, throwing this exception: Unexpected error from external database driver (22).

I tried removing the IMEX=1 part, but it still didn't work.

What is the problem?

petko_stankoski
  • 10,459
  • 41
  • 127
  • 231

6 Answers6

5

I also had the same issue but I had to rename the spreadsheet to a shorter name, then it worked.(SQL 2012 Dev)

user2730382
  • 51
  • 1
  • 2
2

Strangely enough, I replaced the file in another folder and it worked. I have no idea why this is happening.

petko_stankoski
  • 10,459
  • 41
  • 127
  • 231
  • 1
    Your application needs to be able to open the file with administrator permissions. You probably moved the file to a folder where your application had that level of access. – Ryan Kohn Jul 23 '14 at 13:59
  • I got this exception. And when I moved my excel file to a different folder, it worked. System.Data.OleDb.OleDbException. Unexpected error from external database driver 枰儛☐չ䔰疈 – Kiran R Dec 31 '15 at 16:22
1

Try to use Microsoft.Jet.OLEDB.4.0 provider. Also I would advise you to use OleDbConnectionStringBuilder to build OleDbConnectionString:

var oleConnectionStringBuilder = new OleDbConnectionStringBuilder { Provider = "Microsoft.Jet.OLEDB.4.0" };
oleConnectionStringBuilder.DataSource = sourceFile;
oleConnectionStringBuilder.Add("Extended Properties", "Excel 8.0");
oleConnectionStringBuilder.Add("HDR", "No");
Andrey Gordeev
  • 30,606
  • 13
  • 135
  • 162
0

It seems that there is something wrong with the Microsoft Excel Driver. Try to execute the program in another PC to see whether this error happens.

Please take a look at this KB article:

http://www.codeproject.com/KB/database/ReadExcel07.aspx . You can use OleDb to connect the Excel file.

I hope this can help you and feel free to follow up after you have tried.

Igoy
  • 2,942
  • 22
  • 23
0

i have fixed this by uninstalling Microsoft access DB Engine Security update.Also Uninstall Service pack3 Update of access DB engine 2007. Hope this will work...

0

In my case I renamed excel file's sheet name(it was too long), after that it worked. enter image description here

I'm using :

string connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + FilePath + ";Extended Properties=\"Excel 12.0 Xml; IMEX=1;Importmixedtypes=text;\"";