我有2d数组,我需要将此数组数据保存为xls文件
我正在尝试使用PHPExcel
include 'PHPExcel.php';
$data = array(
array("firstname" => "Mary", "lastname" => "Johnson", "age" => 25),
array("firstname" => "Amanda", "lastname" => "Miller", "age" => 18),
);
$objPHPExcel = new PHPExcel();
$objPHPExcel->getActiveSheet()->fromArray($data);
$objPHPExcel->save("test.xls");
但这会产生错误:Call to undefined method PHPExcel::save()
使用PHPExcel
将数组保存为xls的正确方法是什么?
答案 0 :(得分:4)
似乎没有这样的功能。
如果你在http://phpexcel.codeplex.com/查看Hallo World示例,你可能会发现他们没有使用$objPHPExcel->save("test.xls");
但
include 'PHPExcel.php';
include 'PHPExcel/Writer/Excel2007.php';
$objPHPExcel = new PHPExcel();
$objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
$objWriter->save(str_replace('.php', '.xlsx', __FILE__));
答案 1 :(得分:2)
类似于Sailinthorns的回答
include 'PHPExcel.php';
$data = array(
array("firstname" => "Mary", "lastname" => "Johnson", "age" => 25),
array("firstname" => "Amanda", "lastname" => "Miller", "age" => 18),
);
$objPHPExcel = new PHPExcel();
$objPHPExcel->getActiveSheet()->fromArray($data);
// Redirect output to a client’s web browser (Excel5)
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="test.xls"');
header('Cache-Control: max-age=0');
// If you're serving to IE 9, then the following may be needed
header('Cache-Control: max-age=1');
// If you're serving to IE over SSL, then the following may be needed
header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified
header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1
header ('Pragma: public'); // HTTP/1.0
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save("test.xls");