0
votes

Trying to create excel file from C#.

Challenges

The 'Microsoft.ACE.OLEDB.12.0' provider is not registered on the local machine. Resolved using this

Now stuck on

An exception of type 'System.Data.OleDb.OleDbException' occurred in System.Data.dll but was not handled in user code

Additional information: External table is not in the expected format.

Searching a lot since my morning. No results yet.

OS- 64 bit, MS Office not installed, AccessDb file given in path above is installed. Can I create Oledb connection without having MS Office?

1

1 Answers

0
votes

After lot of hit and try I finally found a wall where I was hitting my head. External table is not in the expected format. This error not only means that you have some problem with driver. It can also means you file format is unrecognised.

This was because, I was creating xlsx file programatically.

    string pathWithExtension=location + fileName+DateTime.Now.ToString("yyyyMMddss") + ".xls";

               if (!File.Exists(pathWithExtension))
               {
                   File.Create(pathWithExtension).Close(); //Dont create this file. Won't create valid xlsx file
                   //pathWithExtension = "C:/myFile.xlsx"; try using some template like this
                   ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + pathWithExtension + ";Extended Properties='Excel 12.0;HDR=YES'";
                   //call con.Open() throws above exception saying "External file is...."

This was because when I was creating xlsx file manually, it was not an actual xlsx file. It was just a simple txt file with only give extension as xlsx