PHP生成Excel报表的方法
PHP生成Excel报表
<?php
$path = '/usr/share/pear'; // 你自定义的 PEAR 路径
set_include_path(get_include_path() . PATH_SEPARATOR . $path); // PHP 环境变量路径为除 php.ini 默认的以外, 再加上你自定义的 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(); // 实例化 PEAR::Spreadsheet_Excel_Writer 类
$workbook->send($filename.$name_suffix02); // 发送 Excel 文件名供下载
$workbook->setVersion(8);
$worksheet =& $workbook->addWorksheet('sheet-1'); // 加入一个工作表 sheet-1
$worksheet->setInputEncoding('utf-8'); // 指定行编码
$data = array(
array('姓名', '节目清单', '上传数量'),
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 中写入数据
}
}
$worksheet =& $workbook->addWorksheet('sheet-2'); // 加入一个工作表 sheet-2
$worksheet->setInputEncoding('utf-8'); // 指定行编码
$data = array(
array('姓名', '节目清单', '上传数量'),
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 中写入数据
}
}
$workbook->close(); // 完成下载
echo $name;
?>