tags:

views:

640

answers:

2

Hi,

I'm no php expert (a mere beginner) but need some help!

After hours searching Google and trying out about 100 different scripts, I finally found one that does what I need - almost.

Basically, my site has a button marked 'Export to Excel'. Visitor to site clicks button and a download begins containing all data from a specified table.

I found this on here - http://stackoverflow.com/questions/125113/php-code-to-convert-a-mysql-query-to-csv/125125#125125

which does exactly what I want except the user sees the following error when trying to open the file:

Error - 'The file you are trying to open, 'export.xls', is in a different format than specified by the file extension. Verify that the file is not corrupted and is from a trusted source before opening the file. Wo you want to open the file now?'

User clicks 'Yes' and file opens with all data! Brilliant! Except users will not open the file with this error.

I would be very grateful if someone knows a way to fix this.

Many thanks

TT

+1  A: 

Ok, this results from a feature specified by Excel 2007 called Extension Hardening. You can turn it off, but that can only be done client-side. If you click "OK" or "Yes" the file should open anyway. Check this blog post for more info.

EDIT: What this means is that Excel is finding that the file is of a different type (say HTML or CSV) that what is specified by the file extension. Therefore Excel wants to warn you that this file is not what it says it is. Unless you are going to create native Excel files on the server then prompt the user to download them, there is no getting around this error except for each user to turn off Extension Hardening on their own computer.

Matthew Jones
Thanks - this explains the error message. I might look into Jeff's solution below to see if I can get the data to sit nicely in a .csv file.
TheTub
+3  A: 

Or, you could just change the script in the above solution to return a file with the .csv extension. .csv files are associated with Excel, so they should open directly.

JeffP
+1 ... I came here to say this. Or something.
Adrien
+1 ... Ditto what Adrien said.
Abinadi
Hi Jeff - This works and gets rid of the error message but the file now splits the data from the table (there are 9 columns and around 90 rows as I type) into uneven chunks. One field is 3000 chars and it seems to not like this one at all. If exported as .xls then at least the data is kept in tabular format that can be used.
TheTub
Most likely, you're experiencing something nominally within your data fields that breaks the CSV-ness of it. IOW, the following line wouldn't work like you intuitively think it should: name, ssn, birthDate Blow, Joe, 123-45-6789, 2009-09-30In short, if your data fields have commas, you'll need to look into your export script to wrap those fields with qualifiers (like double-quotes). I fight with putting dirty data into CSV format on almost a daily basis. You need to look at the raw CSV (in notepad), and think like a parser.
Adrien
Err, that didn't format like I wanted it to. Oh well.
Adrien
I don't think it is a problem with the layout of the file, otherwise it won't work when it is named .xls.If it is working when named .xls, try .txt - Excel is probably using something other than the CSV parser when it reads the file.
JeffP
Yep - I just looked at the code on the other page, and it is actually tab separated, not CSV. If .txt doesn't work, you could replace the "\t" in the code with ","
JeffP
Interesting; I'm still on Excel 2003. If I put comma-delimited data in a plain text file, call it foo.xls and open it, all "fields" go into one cell. But, if I replace the commas with tabs, it opens correctly (that is, each field in its own cell). That said, I still think the issue is best solved by making the script put out a clean CSV, which may mean some analysis of the data, and wrapping fields where necessary.
Adrien
I just noticed this comment from Jeff - "you could replace the "\t" in the code with "," "This is very nearly there. The file opens fine! And is arranged with column headers. However, where there is an empty field (say telephone number), it populates with the next available data. Meaning from that point onwards the data is out of line with the column headers. So I guess I need the code to insert something when the data field is empty.
TheTub
Scrub previous comment. I had not replaced one of the "/t" pieces of code. However, I now have another problem! The telephone numbers are being converted to scientific notation! For example, a telephone number now reads 4.876534E+11.
TheTub
Before anyone asks - the field in mySQL is set as varchar(50). Most of the telephone numbers are coming over OK. It seems that those over a certain length are being converted.
TheTub
Try running this script on your phone number data - it will force it to look like text to excel:http://support.microsoft.com/kb/208414
JeffP