Oto Shavadze
Oto Shavadze

Reputation: 42773

Save 2D array as xls file

I have 2d array and I need save this array data as xls file

I am trying make this using 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");

But this gives error: Call to undefined method PHPExcel::save()

What is right way for saving array as xls using PHPExcel ?

Upvotes: 0

Views: 1287

Answers (2)

Dave
Dave

Reputation: 1069

similar to Sailinthorns answer

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

Upvotes: 2

sailingthoms
sailingthoms

Reputation: 948

It seems as if there is no such function.

if u check the Hallo World example at http://phpexcel.codeplex.com/ u may see that they are not using $objPHPExcel->save("test.xls");

but

include 'PHPExcel.php';
include 'PHPExcel/Writer/Excel2007.php';
$objPHPExcel = new PHPExcel();

$objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
$objWriter->save(str_replace('.php', '.xlsx', __FILE__));

Upvotes: 4

Related Questions