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
PHP Web Logs (BLogs)
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
Submit Site
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 : 4 of 5 (graded 5 times)
Viewed : 13711
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" );
?>



Gets the browser and OS from the $_SERVER['http_user_agent'] variable in PHP
Categories : PHP, HTTP, Regexps
A function to check if a URL exists
Categories : PHP, CURL, HTTP
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
Simple pipe delimited file export program that downloads to a local machine
Categories : PHP, Filesystem, Databases, MySQL, HTTP
PHP4 HTTP Compression Speeds up the Web
Categories : PHP, Zlib, HTML and PHP, HTTP, Network
Authentication HTTP protocol POST
Categories : Authentication, HTTP, PHP
This gets the http response headers for a given url and returns them in an assoc array. i.e. to test if a url exists: $array = get_http_headers($url); if($array[result]=200) { }
Categories : HTTP, Arrays, PHP
Remote File Size
Categories : PHP, Filesystem, HTTP, Sockets
Remote File Saving with PHP - Download and serve a remote file. The content of the file will be updated at fixed intervals.
Categories : PHP, Filesystem, Cache, Sockets, HTTP
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
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
This script shows you the 7th latest php items from the mailing list archive on zend.com
Categories : HTML, HTML and PHP, HTTP, PHP
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
The first step Guest Book ... ^^
Categories : MySQL, PHP, Apache, HTML, HTTP
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