PHP生成Excel報表
<?php
$path = ‘/usr/share/pear’; // 你自定義的 PEAR 路徑
set_include_path(get_include_path() . PATH_SEPARATOR . $path); // 設(shè)置 PHP 環(huán)境變量路徑為除 php.ini 默認(rèn)的以外, 再加上你自定義的 PEAR 路徑
require ‘/usr/share/pear/Spreadsheet/Excel/Writer.php’; // 包含 PEAR::Spreadsheet_Excel_Writer 類文件
$name = ‘atyu30’;
$work = $_POST[‘file_body’];
$num = ’10’;
$filename = $_POST[‘file_name’];
$name_suffix01 = ‘_報告.xls’;
$name_suffix02 = iconv(‘utf-8′,’gb2312’,$name_suffix01);
$workbook = new Spreadsheet_Excel_Writer(); // 實(shí)例化 PEAR::Spreadsheet_Excel_Writer 類
$workbook->send($filename.$name_suffix02); // 發(fā)送 Excel 文件名供下載
$workbook->setVersion(8);
$worksheet =& $workbook->addWorksheet(‘sheet-1’); // 加入一個工作表 sheet-1
$worksheet->setInputEncoding(‘utf-8’); // 指定行編碼
$data = array(
array(‘姓名’, ‘節(jié)目清單’, ‘上傳數(shù)量’),
array($name, $work, $num),
array($name, $work, $num),
);
for ($row = 0; $row < count($data); $row ++) {
for ($col = 0; $col < count($data[0]); $col ++) {
$worksheet->writeString($row, $col, $data[$row][$col]); // 在 sheet-1 中寫入數(shù)據(jù)
$worksheet =& $workbook->addWorksheet(‘sheet-2’); // 加入一個工作表 sheet-2
$worksheet->setInputEncoding(‘utf-8’); // 指定行編碼
$data = array(
array(‘姓名’, ‘節(jié)目清單’, ‘上傳數(shù)量’),
array($name, $work, $num),
array($name, $work, $num),
);
for ($row = 0; $row < count($data); $row ++) {
for ($col = 0; $col < count($data[0]); $col ++) {
$worksheet->writeString($row, $col, $data[$row][$col]); // 在 sheet-2 中寫入數(shù)據(jù)
$workbook->close(); // 完成下載
echo $name;
?>