tags:

views:

109

answers:

3

I am exporting data from a database using PHP to convert it into a CSV. I figured it'd be useful to provide the first row with a title (similar to the <th> element in HTML) so the end user would understand the column's meanings. Example

=============
| id | name |
=============
| 0  | tim  |
| 1  | tom  |
=============

Which would look like this as a CSV

id, name
0, tim
1, tom

Is there a way to mark up the first row's columns or do anything differently that programs that often read CSVs (example Microsoft Excel) will mark it up accordingly. I.e. provide a semantic hook to inform the client (possibly Excel but not restricted to) that this is a column header?

+4  A: 

Nope. And to make it even more fun, there's nothing that says that the header line has to be present at all. Good times, good times...

Ignacio Vazquez-Abrams
I had a suspicion... Well at least with basic formats, less should go wrong (in theory at least :P)
alex
A: 

The best practice I can think of myself is to make the headings the first row only. But this is obviously common sense.

alex
+3  A: 

One key thing to avoid with CSVs is to avoid using 'ID' as the first characters in the file. The lowercase 'id' or double-quoted '"ID"' is acceptable, but if Excel comes across upper-case 'ID' it tries to open the file as a SYLK file and fails.

(edit: note that single quotes in the above should be ignored)

Alistair Knock
Thanks for this advice!
alex
I learned this the hard way a few years ago, it's a very annoying bug that won't get fixed
TravisO