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