How to use PHP to generate Excel documents
----------------------------
Excel Functions
----------------------------
Save the following code as excel.php, and then include it in the page
and then call
1. Call Xlsbof ()
2. Write some content to Xlswritenunber () or Xlswritelabel ().
3. Then call Xlseof ()
You can also use the Fwrite function to write directly to the server instead of using echo to display only on the browser.
-----Begin of Function library-----
Excel begin of File header
function Xlsbof () {
Echo Pack ("Ssssss", 0x809, 0x8, 0x0, 0x10, 0x0, 0x0);
Return
}
Excel End of File footer
function xlseof () {
Echo Pack ("ss", 0x0A, 0x00);
Return
}
Function to write a number (double) into Row, Col
function Xlswritenumber ($Row, $Col, $Value) {
Echo Pack ("Sssss", 0x203, $Row, $Col, 0x0);
Echo Pack ("D", $Value);
Return
}
Function to write a label (text) into Row, Col
function Xlswritelabel ($Row, $Col, $Value) {
$L = strlen ($Value);
Echo Pack ("Ssssss", 0x204, 8 + $L, $Row, $Col, 0x0, $L);
Echo $Value;
Return
}
-----End of Function library-----
?>
//
To display the contents directly in a MIME compatible browser
Add the following lines on TOP of your PHP file:
Header ("Expires:mon, 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=empllist.xls ");
Header ("Content-description:php/interbase Generated Data");
//
The next lines demonstrate the generation of the Excel stream
//
Xlsbof (); Begin Excel Stream
Xlswritelabel (0,0, "This is a label"); Write a label in A1, use for dates too
Xlswritenumber (0,1,9999); Write a number B1
Xlseof (); Close the stream
?>
The above describes how to use PHP to generate Excel documents, including aspects of the content, I hope that the PHP tutorial interested in a friend helpful.