1

I have a link which points to a CSV file. When clicked, it opens the file in the browser. I would like to use a different application (i.e. Excel, Open Office, Lotus) to open the file as a spreadsheet. Is it possible to include headers or something to make this happen? Thanks

Link:

<a target="_blank" href="contacts.csv">Contacts</a>

Browser Displays:

First Name,Last Name,Title,Email,Company,Address,City,State,Zipcode,Direct Phone,Mobile Phone,Direct Fax,Company Phone,Company Fax
John,Doe,Estimator,johnd@acme.com,Acme Co.,112 Main St,Gothem,IL,,(847) 555-1122,,,,
Jane,Doe,Engineer,,Ace Industries,,Seattle,WA,,,,,(206) 555-1234,
user1032531
  • 24,767
  • 68
  • 217
  • 387

2 Answers2

2

You will need to set the content type header on whatever generates the csv file.

See: Setting mime type for excel document

For the available headers.

You don't say which language you're using to generate the csv data, or whether it's simply a flat file you're linking to. While most languages will provide the means to set headers on the fly, if you are using static flat files, you may need to set your htaccess (assuming apache is your web server). There are instructions for that case here: http://htaccess.wordpress.com/2008/07/16/sending-correct-content-type-headers-with-htaccess/

Community
  • 1
  • 1
stakolee
  • 893
  • 1
  • 7
  • 20
  • Thanks Stakolee, Just a flat file. Don't want to do this site wide, just one place, so not sure about using .htaccess. Guess I can change it from a .csv file to an PHP file, but this seems a little odd as well. – user1032531 Feb 25 '13 at 14:43
  • Actually, maybe doing site wide is not a bad idea. – user1032531 Feb 25 '13 at 14:47
1

The answer that @stakolee links to is really more specific to opening a file in Excel. If you want to be correct and widely compatible, the mime type should be for CSV, not Excel.


The correct mime type is text/csv. Excel will likely be the application associated with this type, so you'll get the behavior you want.

What MIME type should I use for CSV?


You cam also set Content-Disposition to help things along.

Content-Disposition: attachment; filename="filename.csv"

What to set as mimetype for CSV files to open in spreadsheet applications


If you want backup options, you can set multiple mime types.

text/csv, application/csv, application/excel, application/vnd.ms-excel, application/vnd.msexcel

Filepicker does not accept mimetype 'text/csv' on windows


RFC 4180 says you can respond with text/csv; header=present or header=absent to indicate if the files contains column headers.

Proper syntax for optional header parameter for text/csv mimetype?

Community
  • 1
  • 1
Snekse
  • 15,474
  • 10
  • 62
  • 77