WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDES  |  PHP CLASSES  |  CODE SEARCH  |  ARTICLES SEARCH  |  PHP FORUMS  |  PHP MANUAL  |  PHP FUNCTIONS LIST  |  WEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search
Submit a code Example / Snippet Submit Your Code
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
WeberDev's Monthly code contest PHP Code Contest
Your Personal Examples List My Favorite Examples
Your Personal Articles List My Favorite Articles
Edit Account Info Update Your Profile
PHP Code Search
Web Development Forums
Learn MySQL Playing Trivia
PHPBB2 Templates
Web Development Index
Web Development Resources
Web Development Content
PHPClasses
PHP Editor
PHP Jobs
Vision.To Design
Ajax Tutorials
PHP Programming Help
PHP/MySQL Programming
Webmaster Resources
Webmaster Forum
XML meta language
website builder
Forex Trading Online forex trading platform

Go Back Add a Comment Send this example to a friend Add this Article to your personal favoritest for easy future access to your favorite Code Examples and Articles. Submit a code example Print this code example.
BACK ADD A COMMENT SEND TO A FRIEND ADD TO MY FAVORITES ADD CODE EXAMPLES PRINT
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 6 times)
Viewed : 21223
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

  Submit your own code examples 
 

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" );
?>
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
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
Easy upload class
Categories : PHP Classes, Filesystem, HTTP, PHP



The first step Guest Book ... ^^
Categories : MySQL, PHP, Apache, HTML, HTTP
Simple Password example
Categories : PHP, Authentication, Security, HTTP
Simple pipe delimited file export program that downloads to a local machine
Categories : PHP, Filesystem, Databases, MySQL, HTTP
Browser Detection, Redirection Type, MSIE, MOZILLA, Netscape Navigator, NS, $HTTP_USER_AGENT, HTTP_USER_AGENT
Categories : PHP, HTTP, Browsers, HTML and PHP
Query2Report : Generating Html, Pdf and Csv Reports from SQL Query
Categories : PHP, PHP, HTML, PDF, Excel
Grab links from a page
Categories : PHP, Regexps, HTTP
Pseudo Non Parsed Header. Output to the the browser as the script runs.
Categories : PHP, HTTP, HTML and PHP
Sending SMS Thru HTTP
Categories : PHP, HTTP, SMS
PHP4 HTTP Compression Speeds up the Web
Categories : PHP, Zlib, HTML and PHP, HTTP, Network
WebServerSpy checks which kind of Webserver is running, Apache, Netscape, Fasttrack, IIS, HTTP-Header, HTTP 1.0, GET, spy, WWW
Categories : HTTP, Network, Apache, PHP, Web Servers
Check whether your referer is coming from your website or from someone else's. Very useful when you want to represent the data without your domain name for internel link-mapping in some COUNTER.
Categories : PHP, 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