将二维阵列另存为xls文件


Save 2D array as xls file

我有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的正确方法是什么?

似乎没有这样的函数。

如果你在http://phpexcel.codeplex.com/u可能看到他们没有使用$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__));

类似于Sailinthons的回答

  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");