x10Hosting Forums

Corporate Free Hosting for the Masses.



Register

Reply
 
LinkBack Thread Tools Display Modes
x10 Lieutenant

Join Date: Oct 2007
Posts: 438
Credits: 4,202
mattura is just really nice
Quote  
06-28-2008, 05:30 AM
Post [PHP Tut] Creating Excel XLS for download

This is for those who want to offer an XLS download, generated on the fly from your database(s).

First, you need to write the code at the top of a page of php, so the headers may be sent. Your file 'this.php' might look something like this:
PHP Code:
<?php
if ($_REQUEST['download']=="xls") {
 
//the link below will cause this code to run
 //CODE HERE
 
exit; //this is  important! It stops the rest of the page being appended to the file
}
?>
<html>
<body>
HTML here perhaps<br/>
<a link="this.php?download=xls">Download xls</a>
</body>
</html>
So...what goes in the 'CODE HERE' gap? We shall see...

Now XLS files are similar to other files in that they must have some kind of 'header' to mark the Beginning Of the File (BOF). This also shows your operating system how to open the file. This is the first thing that must be written to the XLS. This is in binary, and can be generated by the following (look up pack for more details):
PHP Code:
//this is the XLS header:
$xlshead=pack("s*"0x8090x80x00x100x00x0); 
Similarly, the XLS must have an End Of File marker:
PHP Code:
//this is the XLS footer:
$xlsfoot=pack("s*"0x0A0x00); 
We can write to cells by various methods, but here we shall use the cell reference method. This consists of a row/column reference, followed by a value. It is a good idea to wrap this in a function:
PHP Code:
function xlsCell($row,$col,$val) {
 
$len=strlen($val);
 return 
pack("s*",0x204,8+$len,$row,$col,0x0,$len).$val;

So now it is time to create the bulk of your excel file using the function above. Here we will store it in a variable called $data. Let's say you have a database of users (name, phone, email):
PHP Code:
$data=xlsCell(0,0,"Name").xlsCell(0,1,"Phone").xlsCell(0,2,"Email"); 
Note that XLS co-ordinates start at [0,0]

Now you need to loop through your database using your favourite method, and simply add the values to $data, something like this:
PHP Code:
$rowNumber=0;
$q="SELECT * FROM `users` ORDER BY `name` ASC";
$r=mysql_query($q);
while (
$row=mysql_fetch_assoc($r)) {
 
$rowNumber=$rowNumber+1;
 
$name=$row['name'];
 
$phone=$row['phone'];
 
$email=$row['email'];
 
$data.=xlsCell($rowNumber,0,$name) . xlsCell($rowNumber,1,$phone) . xlsCell($rowNumber,2,$email);

Good! Now you have created an XLS file! But wait, you need to make sure you can serve it up properly, so that users/computers know what to do with it! This is accomplished by means of a few headers, sent before the data:
PHP Code:
$filename="users.xls";
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");;
header("Content-Disposition: attachment;filename=$filename"); 
header("Content-Transfer-Encoding: binary "); 
So now we can put it all together:
PHP Code:
<?php
if ($_REQUEST['download']=="xls") {
 
$xlshead=pack("s*"0x8090x80x00x100x00x0);
 
$xlsfoot=pack("s*"0x0A0x00);
 function 
xlsCell($row,$col,$val) {
  
$len=strlen($val);
  return 
pack("s*",0x204,8+$len,$row,$col,0x0,$len).$val;
 }

 
$data=xlsCell(0,0,"Name") . xlsCell(0,1,"Phone") . xlsCell(0,2,"Email");
 
$rowNumber=0;
 
$q="SELECT * FROM `users` ORDER BY `name` ASC";
 
$r=mysql_query($q);
 while (
$row=mysql_fetch_assoc($r)) {
  
$rowNumber=$rowNumber+1;
  
$name=$row['name'];
  
$phone=$row['phone'];
  
$email=$row['email'];
  
$data.=xlsCell($rowNumber,0,$name) . xlsCell($rowNumber,1,$phone) . xlsCell($rowNumber,2,$email);
 }
 
$filename="users.xls";
 
header("Content-Type: application/force-download");
 
header("Content-Type: application/octet-stream");
 
header("Content-Type: application/download");;
 
header("Content-Disposition: attachment;filename=$filename"); 
 
header("Content-Transfer-Encoding: binary ");
 echo 
$xlshead $data $xlsfoot;
 exit; 
//this is  important!
}
?>
<html>
<body>
HTML here perhaps<br/>
<a link="this.php?download=xls">Download xls</a>
</body>
</html>
__________________
----
Please donate credits if you had a really good reply from me! Rep is also appreciated. Thanks
matt.elementfx.com

Last edited by mattura; 06-28-2008 at 05:32 AM. Reason: forgot to increment loop counter!
Reply With Quote
mattura is offlineReport Post
x10 Lieutenant

Join Date: Oct 2007
Posts: 438
Credits: 4,202
mattura is just really nice
Quote  
07-03-2008, 06:45 PM
Re: [PHP Tut] Creating Excel XLS for download

C'mon guys and gals...I'd love to know what you think of this tut. Is it useful for anyone at all? Has anyone even tried it?
__________________
----
Please donate credits if you had a really good reply from me! Rep is also appreciated. Thanks
matt.elementfx.com
Reply With Quote
mattura is offlineReport Post
x10 Lieutenant

Join Date: Mar 2008
Posts: 359
Credits: 4,059
woiwky is a jewel in the rough
Quote  
07-04-2008, 02:09 AM
Re: [PHP Tut] Creating Excel XLS for download

I haven't tried this code in particular, but it looks fine. Although I would recommend using an OOP style or at least procedural so it would be easier to use in other scripts as well.

This is a very good tut, though. Perhaps it's just that it's beyond the understanding of a lot of people around here. The explanations you provide are solid, but they do require intermediate php knowledge and basic mysql knowledge.

Regardless, generating xls files can prove to be quite useful in many situations. Nice tut, +rep.
__________________
"But you have access to the greatest source of knowledge in the universe."
"Well I do talk to myself sometimes, yes."
Reply With Quote
woiwky is offlineReport Post
Account Manager

sunils's Avatar

Join Date: Jan 2008
Posts: 2,033
Credits: 30,567
sunils has a reputation beyond repute
Location: Chennai ,India

Send a message via AIM to sunils Send a message via Yahoo to sunils
Quote  
07-04-2008, 09:20 PM
Re: [PHP Tut] Creating Excel XLS for download

Its a good tutorial. Many of the people would want to offer its user some reports in excel format and they could use this tutorial to generate one on the fly. Good job.
__________________
Sunil Sankar
-------------------------------------------------------------------------
Account Manager

Reply With Quote
sunils is offlineReport Post
x10 Lieutenant

Join Date: Oct 2007
Posts: 438
Credits: 4,202
mattura is just really nice
Quote  
07-08-2008, 06:43 AM
Re: [PHP Tut] Creating Excel XLS for download

Thanks! Just wanted to know if it's useful.
It would be a simple matter to change the style of programming to whatever you prefer, but I wrote it like that for explanatory clarity.
Also it is meant to be a copy-patse tutorial, so hopefully those with less complete php/sql knowledge can at least test it and make simple changes to taste (that's how we learn anyway!)

Let me know if you use it/like it/hate it etc or have any problems with it.
__________________
----
Please donate credits if you had a really good reply from me! Rep is also appreciated. Thanks
matt.elementfx.com
Reply With Quote
mattura is offlineReport Post
Reply

Tags
download, excel, php, spreadsheet, xls

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
[PHP Tut] Make your own CAPTCHA mattura Tutorials 17 11-20-2008 07:40 AM
[PHP - TUT] Site Online/Offline Status Synkc Tutorials 16 09-17-2008 01:33 PM


All times are GMT -5. The time now is 03:13 PM. Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.2.0 RC7
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios

Web Advertising | Problem Mortgage | Remortgages | Bad Credit Mortgages | MySpace Images