|
|
|
| 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 12 times) |
| Viewed : |
53776 |
| 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" );
?> | | |
|
| Class that allows the PHP developer to create and manage UNIX like password files suitable for use as Apache authentication password files.
Categories : HTTP, PHP, PHP Classes, Filesystem | | | 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 | | | Using php3 to upload files, uploading files, file uploads. Categories : PHP, Filesystem, HTTP | | | Inserting data to a MySQL Database using AJAX Categories : AJAX, HTTP, PHP, Databases, MySQL | | | IP Blocking Categories : PHP, Security, 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 | | | Simple pipe delimited file export program that downloads to a local machine Categories : PHP, Filesystem, Databases, MySQL, HTTP | | | How to force the user to download a file instead of opening it up in an
controlled environment within the browser (i.e. MS Word/Adobe Acrobat) Categories : Browsers, PHP, HTTP | | | Gets the browser and OS from the $_SERVER['http_user_agent'] variable in PHP Categories : PHP, HTTP, Regexps | | | Function that does language negotiation based on the Accept-Language header, a cookie or host name Categories : HTTP, PHP, Cookies | | | Sending SMS Thru HTTP Categories : PHP, HTTP, SMS | | | $REMOTE_HOST does not return a value. Categories : PHP, Global Variables, HTTP | | | 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 | |
| |
| | | | | 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
| | | | Alain Goldberg wrote : 1915
Thanks berber, it helped!
Regards,
Alon Golan (ex early nv if you remember)
| | | | christian revest wrote :1918
Simpler solution
Simply create a text file with notepad.
Change encoding to UTF8 and save it as myfile.CSV file
In Excel, save your document with CSV format and save it to replace the myfile.csv
The utf8 encoding should remain
| |
|
|