PHP generate Excel xls document _php Tutorial

Source: Internet
Author: User
PHP tutorial generates an Excel XLS document

Method 1-Use the HTTP header

As described in MS Word, you need to format the html/php page using Excel-friendly CSS and header information

Add to your PHP script.
Header ("Content-type:application/vnd.ms-excel");
Header ("content-disposition:attachment; Filename=document_name.xls ");

echo "";
echo " ";
echo "";
echo "testdata1 t testdata2 t n";
echo "";
echo "";
?>

Method 2-Use COM objects

Note that the installation of the code described in MS Excel must be in the server running below.

We use a file to save to the temp directory first, as MS Word does the same thing.

Create New COM Object–excel.application
$XL = new COM ("Excel.Application");

Hide MS Excel application window
$XL->visible = 0;

Create New Document
$xlBook = $xl->workbooks->add ();

Create Sheet 1
$xlBook->worksheets (1)->name = "Worksheet 1";
$xlBook->worksheets (1)->select;

Set Width & Height
$XL->activesheet->range ("a1:a1")->columnwidth = 10.0;
$XL->activesheet->range ("b1:b1")->columnwidth = 13.0;

ADD text
$XL->activesheet->cells (->value) = "TEXT";
$XL->activesheet->cells (->font->bold) = True;

Save Document
$filename = Tempnam (Sys_get_temp_dir (), "Excel");
$xlBook->saveas ($filename);

Close and quit
Unset ($xlBook);
$XL->activeworkbook->close ();
$XL->quit ();
Unset ($XL);

Header ("Content-type:application/vnd.ms-excel");
Header ("content-disposition:attachment; Filename=document_name.xls ");

Send file to Browser
ReadFile ($filename);
Unlink ($filename);


http://www.bkjia.com/PHPjc/444977.html www.bkjia.com true http://www.bkjia.com/PHPjc/444977.html techarticle PHP Tutorial Build Excel XLS Document Method 1-Use HTTP header as described in MS Word, you need to format the html/php page using Excel-friendly CSS and header information to add to your PHP script. ...

  • 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.