2013-06-28 56 views
0

我有二維數組,我需要保存這個數組數據xls文件保存的二維數組作爲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的正確方法是什麼?

回答

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

沒有任何事情發生,不能恢復XLS文件... – RIKI

+0

它創建xls文件在運行它的php文件相同的目錄中,檢查web'test.xls' – Dave

+0

嘗試在結尾標題處使用('Location:test.xls');不是最乾淨的方式來做到這一點,但只是爲了讓你想到 – Dave

4

看起來好像沒有這樣的功能。

如果妳在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__));