How to use PHP to generate Excel documents

Source: Internet
Author: User
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.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.