|
|
|
| Title : |
How to easily export data to an excel sheet (CSV) and also a solution for UTF-8 export. |
| Categories : |
PHP, HTTP |
 Boaz Yahav |
| Date : |
Jul 15th 2003 |
| Grade : |
3 of 5 (graded 8 times) |
| Viewed : |
27690 |
| File : |
No file for this code example. |
| Images : |
No Images for this code example. |
|
| Search : |
More code by Boaz Yahav |
|
| Action : |
Grade This Code Example
|
|
| Tools : |
My Examples List |
|
|
|
|
|
|
So you want to allow your users to get some data directly into their excel.... no problem.
All you need to do it organize your data in rows where each data field is delimited by a comma or
some other acceptable delimiter and send the correct header to the browser.
Suppose you want to send something like :
Car Type Car Model Number of Cars
Mazda 323 5
Mazda 6 4
Mazda MPV 6
all you need to do is :
|
<?php
header ("Content-type: application/csv\nContent-Disposition: \"inline; filename=Report.csv\"");
Echo"Car Type,Car Model,Number of Cars\n";
Echo"Mazda,323,5\n";
Echo"Mazda,6,4\n";
Echo"Mazda,MPV,6\n";
?> | |
Tip : It always helps to put a nice excel logo where you offer to export your data to excel format.
You can get such a logo here:
http://www.weberdev.com/images/excel.gif
When your data is in UTF-8 you can't simply export it into excel since Excel does not support UTF-8. It took me some time to find but finally i found an example by peter kehl which you can see bellow :
UTF-16LE solution for CSV for Excel by Eugene Murai works well:
| <?php
$unicode_str_for_Excel = chr(255).chr(254).mb_convert_encoding( $utf8_str, 'UTF-16LE', 'UTF-8');
?> | |
However, then Excel on Mac OS X and also some windows doesn't identify columns properly and its puts each whole row in its own cell. In order to fix that, use TAB "\t" character as the CSV delimiter rather than comma or colon.
You may also want to use HTTP encoding header, such as
| <?php
header( "Content-type: application/vnd.ms-excel; charset=UTF-16LE" );
?> | | |
|
| $REMOTE_HOST does not return a value. Categories : PHP, Global Variables, HTTP | | | Function that does language negotiation based on the Accept-Language header, a cookie or host name Categories : HTTP, PHP, Cookies | | | Script to check values being submitted by POST or GET method from a form. This script may help diagnose what variables are being supplied by a browser to other php scripts. Categories : HTML, Variables, Debugging, PHP, HTTP | | | valid link! Categories : HTTP, URLs, PHP | | | The following snippet gives complete info about all submitted
HTTP_POST_VARS and HTTP_GET_VARS Categories : Variables, HTTP, PHP | | | Upload Script, Upload File Script, Input Type="File" Categories : PHP, HTML and PHP, HTTP | | | This script shows you the 7th latest php items from the mailing list archive on zend.com Categories : HTML, HTML and PHP, HTTP, PHP | | | PHP Domain Availability Checker Categories : PHP, Complete Programs, Regexps, HTTP, Sockets | | | A function to check if a URL exists Categories : PHP, CURL, HTTP | | | Gonx Proxy - This class is meant to act as an HTTP proxy to serve pages of a remote server as if they were local pages.
Categories : PHP, PHP Classes, HTTP | | | Easy upload class Categories : PHP Classes, Filesystem, HTTP, PHP | | | Request Method Class - seful for situations like form processing or API development. Requires PHP5 for the magic __call() method.
Categories : PHP, PHP Classes, HTTP, Headers | | | redirect redirection ip address authentication authenticate addr Categories : Authentication, HTTP, Network, PHP | | | The first step Guest Book ... ^^ Categories : MySQL, PHP, Apache, HTML, HTTP | | | Simple Password example Categories : PHP, Authentication, Security, HTTP | |
| | | | erwin traas wrote : 994
i`m getting the following error :
Warning: Cannot modify header information - headers already sent by (output started at /www/htdocs/etsoft/cor/Corforma/Marketing/_Business_Plan_Concepts/SWOT/Analyse_your_SWOT/printswot.php:10)
What is going on ??
Regards
Erwin
| | | | Boaz Yahav wrote :995
As with any header that is sent, it must be sent at the begining of the file before any other content is sent. Make sure that you are not echo`ing any other content before the header is sent.
berber
| |
|
|