Home  >  Article  >  Backend Development  >  Generate excel xls document with php_PHP tutorial

Generate excel xls document with php_PHP tutorial

WBOY
WBOYOriginal
2016-07-20 11:07:08986browse

php tutorial to generate excel xls document

Method 1 - Using HTTP headers

As mentioned in MS Word, you need to format the HTML/PHP page using Excel friendly CSS and markup Add header

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 - Using COM object

Please note that there must be all MS Excel files under the server running Install the code described above.

We use a file saved to a temporary directory first, the same approach as MS Word.

//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(1,1)->Value = "TEXT";
$xl->ActiveSheet- >Cells(1,1)->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);


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/444977.htmlTechArticlephp tutorial to generate excel xls document method 1 - using HTTP headers As stated in MS Word, you need to format HTML/PHP pages use Excel friendly CSS and add header information to your PHP scripts. ...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn