Create excel files with php
Excel files are great for many things. It might though be a little bit tricky to generate them on the fly with php. Therefor I will guide you exactly how to do it, quick and simple.
Have you ever thought of creating excel files from your web site?
Excel is a very useful format for lots of purposes. For example, exporting stats, member lists to hand over to economy department etc.
But how can we create a simple excel file with a header and two different columns?
Of course you can sit down and read the excel specifications, and do it your self…
Or the smarter way, download classes from phpclasses.org made by Ignatius Teo.
You use these classes to generate your excel files. Pretty simple, but very effective.
Start with downloading (registrate for free to download), here http://www.phpclasses.org/browse/package/1919.html .
Unzip and upload the file named excel.php to your server.
I am going to generate a simple excel file that will contain Name and IQ in two columns ![]()
The file will look something like this (Hopefully):
First name IQ
Mattias 250
Tony 100
Peter 100
Edvard 100
(These numbers are just estimates…
Create the php file that will create the excel files for you.
Lets call it: generate.php
Start with:
require_once “excel.php”;
$filename = “theFile.xls”;
This section will create and save the excel file on the server.
The file will be saved in the directory tmp, as theFile.xls.
The code is pretty self explaining I think. don’t bother with small details, just use it as a tool!
$export_file = “xlsfile://tmp/”.$filename;
$fp = fopen($export_file, “wb”);
if (!is_resource($fp))
{
die(”Cannot open $export_file”);
}
// typically this will be generated/read from a database table
$assoc = array(
array(”First name” => “Mattias”, “IQ” => 250,
array(”First name” => “Tony”, “IQ” => 100,
array(”First name” => “Peter”, “IQ” => 100,
array(”First name” => “Edvard”, “IQ” => 100);
fwrite($fp, serialize($assoc));
fclose($fp);
This section is for opening the file directly for the surfer in his browser.
It’s then up to the user to choose if he/she wants to open or save the file.
If you only want to save files on the server, just remove this part from the file.
header (”Expires: Mon, 26 Jul 1997 05:00:00 GMT”);
header (”Last-Modified: ” . gmdate(”D,d M YH:i:s”) . ” GMT”);
header (”Cache-Control: no-cache, must-revalidate”);
header (”Pragma: no-cache”);
header (”Content-type: application/x-msexcel”);
header (”Content-Disposition: attachment; filename=\”" . $filename . “\”" );
header (”Content-Description: PHP/INTERBASE Generated Data” );
readfile($export_file);
exit;
If you run this code you will get two things:
- An excel file in /var/ on your server
- This file opened to the user that surf your site
This is the whole tutorial, showing you that it is not that hard to create an excel file or open it to the user ![]()
Good luck and let me know if you have any problems with the code!!

May 11th, 2009 at 2:13 pm
Thanks for sharing this but I guess it’s not a possible solution I you can’t modify extensions of my “shared” server, right?
May 11th, 2009 at 2:24 pm
Panamon Rn+: This class does not need to modify any extensions. So you just need to download the class and include it and use it in your script.
May 13th, 2009 at 11:32 pm
I added this page to Digg
May 14th, 2009 at 12:10 am
Learned lots, I will return
May 14th, 2009 at 4:11 am
I bookmarked this
May 14th, 2009 at 3:34 pm
Nice post, and a great blog you have here. I’ve been checking back a lot, but is there anyway to signup to the RSS feed?
May 15th, 2009 at 5:03 am
Hi, THanks
Can u tell me same 2 column excel file how to impore in MYSQL using PHP…..
Please
May 17th, 2009 at 10:20 am
Hi, Sorry but I don’t really understand what you are saying…
May 17th, 2009 at 10:22 am
Welcome to the blog! To signup to the RSS feed just click the blue RSS-icon or the text “grab our rss feed” at the top right of the site!
May 17th, 2009 at 10:23 am
Thanks guys! Glad to hear that you like the tutorials!
June 1st, 2009 at 10:56 am
Our servers now require PHP for interactive content (in the past I have used .cgi files). I’ve never used PHP and am
looking for a way to add entries to an existing Excel (or any)
file from a web-form.
It looks like your scripts may do this but I’m not sure 1)
what the form needs to look like and 2) if each new entry
will be added to the same file.
Can you give me some more details on how I might do this?
June 3rd, 2009 at 2:03 pm
Hi John,
I’m sorry buy I have no possibility to help you with that. I know that I have read somewhere about a new package that is capable to create excel files in a new way. so you will need to google to see if you can find it, because it’s not possible with this class. Good luck!
June 4th, 2009 at 12:12 am
how can we configure cell widths, bolded/underline text?
if cannot, we might as well generate a .csv instead?
June 4th, 2009 at 8:08 pm
Really insightful posts here. Glad I finally found this place. I can’t remember who referred me here, but sure glad they did.
June 17th, 2009 at 7:18 am
[...] sure it’d be a simple matter to do the same for Excel. A quick Google search turned up this page which describes how to do exactly what I need. The sheer number of people that write code in PHP [...]