2

I'm using visual studio 2008 to create a console application on c#.

The application "reads" an excel file 97-2003 (.XLS).

I'm using jet to read the file:

OleDbConnection oConn = new OleDbConnection();
   oConn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source= " + file_path  + " ;Extended Properties=\"Excel 8.0;HDR=No;IMEX=1\";";    

But when running the program I get error "External table is not in the expected format."

I have tried reading the excel file with the NPOI library and i got error:

Message = "Invalid header signature; read 0x0010000000060809, expected 0xE11AB1A1E011CFD0"

So, it's like the file is invalid in some way.

If, through Windows, I open the excel file on Office Excel, then save it with the same and same extension .xls overwriting it , and after that I run the console application, then the application runs fine with no error.

So, what I would like to do is: with c# code, open the file and save it and overwrite it, so the application could read the file successfully.

I guess I could open the file like this:

File.OpenWrite(file_path);

But how could I overwrite the file?

3
  • Where are you getting the source file from? Is it possible it's not really a valid Excel "xls" file but some other file format with an "xls" extension? Commented Jun 3, 2013 at 22:48
  • The excel file is generated by another program, so it is generated as an excel file. Commented Jun 4, 2013 at 12:57
  • Not to press this point, but are you sure? Many web applications (for example) will generate "xls" files which are nothing more than an HTML table if you look at the content. Excel opens them fine, but other applications may choke on that format. Commented Jun 4, 2013 at 20:22

1 Answer 1

1

You don't need to open and rewrite it, as far as I can see. Your connection string is wrong. You're not saying anywhere it's an Excel file, so by default it's looking for an Access database instead (and not finding one).

Try this instead:

Provider=Microsoft.Jet.OLEDB.4.0; data source="YourFileName.xls"; Extended Properties=Excel 8.0;

For other types of connections, see ConnectionStrings

Sign up to request clarification or add additional context in comments.

1 Comment

Yes I am saying is an excel file. I just edited my question. But i'm getting the same error. As you can see, I also tried opening the excel file with another library, NPOI library, but it failed again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.