28

I am writing out a comma separated file using a console app, and than using Process to open the file. It's a quick and dirty way of dumping results of a query into excel.

for a while this worked fine, but lately i started getting "The file you are trying to open 'blah.csv', is in a different format than specified by the file extension".

and than after clicking "Yes"

Excel has detected that blah.csv is a SYLK file, but cannot load it. Either the file has errors or it is not a SYLK file format. Click OK to try to open the file in a different format.

Pressing OK opens it, and displays correctly.

I see some solutions for this in web world with adding content-disposition header, but since i am using a Process to open it, i can't apply that fix.

my code to open the file:

ProcessStartInfo info = new ProcessStartInfo();
info.FileName = filePath;
info.UseShellExecute = true;
Process.Start(info);

if i open the file in Notepad++ and show all chars, it just shows as regular CSV with CR LF line endings.

after some investigation, it looks like the headings line is triggering the error. If i simply write a empty line before the headings, the error goes away. the headings look like this:

heading1,heading2,heading3 CRLF

Sonic Soul
  • 23,855
  • 37
  • 130
  • 196

4 Answers4

57

Have a look here: http://support.microsoft.com/kb/323626

it seems having ID as the first two chars on the header row is the issue - pretty bizarre behaviour from Excel in my opinion.

John Pickup
  • 5,015
  • 2
  • 22
  • 15
5

The CSV starts with the two characters ID. If you surround the characters with double quotes it should work fine.

Jeremy
  • 44,950
  • 68
  • 206
  • 332
2

(EDITED) I had a similar issue - I was getting the exact same error, but in my case this had suddenly occurred after months of frequent use. I wondered if the problem really is the "ID" column title, Microsofts solution doesn't explain why, as the OP stated, "for a while this worked fine" and then became a problem.

I found in my scenario there was some erroneous data coming from an upstream system where a comma had been added in someone's first name, and this was invalidating the .csv file. This caused the 'file is in wrong format error' but sent me down the wrong path with the ' [filename] is a SYLK file, but cannot load it'

hope this might help someone else one day.

Ashutosh Nigam
  • 868
  • 8
  • 27
AgentNaz
  • 51
  • 7
  • Avoid asking question as answer. – Ashutosh Nigam Mar 23 '15 at 15:35
  • I don't have a hight enough reputation to comment on existing answers so could only post as an answer. The downvote is a bit harsh but thanks for getting involved! I do now have further information to add now, so will attempt to edit my first post... – AgentNaz Mar 23 '15 at 16:21
  • downvote was not to hurt you but part of review. And, passing such question without flag or downvote can lead reviewer in trouble, believe me flagging is worse. My suggestion is wait for 50 rep before you start commenting. Till then if you have a genuine question, ask it as separate question. – Ashutosh Nigam Mar 23 '15 at 16:29
0

SYLK file format is not valid error occurs when the CSV file has first two characters are in the uppercase I and D. It can be resolved by the following steps: Open the file in Notepad and hit apostrophe (’) key before the 1st character. Save the file and reopen MS Excel.

  • Welcome to Stack Overflow! [How to Answer](http://stackoverflow.com/help/how-to-answer) can help you write answers that will be accepted and upvoted. – zhon Aug 30 '16 at 06:22