php教程生成excel文件並輸出到浏覽器
本文介紹了php spreadsheet_excel_writer與如何生成excel文件。
第一步,安裝spreadsheet_excel_writer 由於這包使用ole包,您可能需要安裝它
執行以下命令來執行更新: 更新pear.php.net ole - 0.5 spreadsheet_excel_writer - 0.9.1
看一個實例,myfile.xls是文件名(包括路徑),工作簿包含一個具有學生名單表
<?php
require_once 'spreadsheet/excel/writer.php';
// creating workbook
$workbook = new spreadsheet_excel_writer('myfile.xls');
// adding worksheet
$worksheet =& $workbook->addworksheet('students');
// data input
$worksheet->write(0, 0, 'name');
$worksheet->write(0, 1, 'grade');
$worksheet->write(1, 0, 'ivancho');
$worksheet->write(1, 1, 7);
$worksheet->write(2, 0, 'mariika');
$worksheet->write(2, 1, 7);
$worksheet->write(3, 0, 'stoyancho');
$worksheet->write(3, 1, 8);
// saving file
$workbook->close();
?>
下面來看一款把數據導出給用戶保存吧。
<?php
require_once 'spreadsheet/excel/writer.php';
// creating workbook
$workbook = new spreadsheet_excel_writer();
// sending headers to browser
$workbook->send('students.xls');
// adding worksheet
$worksheet =& $workbook->addworksheet('students');
// data input
$worksheet->write(0, 0, 'name');
$worksheet->write(0, 1, 'grade');
$worksheet->write(1, 0, 'ivancho');
$worksheet->write(1, 1, 7);
$worksheet->write(2, 0, 'mariika');
$worksheet->write(2, 1, 7);
$worksheet->write(3, 0, 'stoyancho');
$worksheet->write(3, 1, 8);
// sending the file
$workbook->close();