0

在我当前的项目中,我使用了 PHPExcel 1.6.7,按照我的建议,我用我的 projet 配置了 PHPExcel 库,但是当我尝试将数据导出到 excel 文件中时,我得到了以下错误。

Fatal error: Call to a member function setLastModifiedBy() on a non-object in C:\wamp\www\xxx\site\dump.php on line 28 

我在 dump.php 中有后续代码

/** Error reporting */
error_reporting(E_ALL);

/** PHPExcel */
require_once ("Classes/PHPExcel.php");


/** PHPExcel_IOFactory */
require_once ("Classes/PHPExcel/IOFactory.php");

// Create new PHPExcel object
$objPHPExcel = new PHPExcel();

// Set properties
$objPHPExcel->getProperties()->setCreator("Maarten Balliauw")
 ->setLastModifiedBy("Maarten Balliauw")
 ->setTitle("Office 2007 XLSX Test Document")
     ->setSubject("Office 2007 XLSX Test Document")
 ->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")
 ->setKeywords("office 2007 openxml php")
 ->setCategory("Test result file");

// Add some data
$objPHPExcel->setActiveSheetIndex(0)
        ->setCellValue('A1', 'Hello')
        ->setCellValue('B1', 'world!')
        ->setCellValue('C1', 'Hello')
        ->setCellValue('D1', 'world!');

// Miscellaneous glyphs, UTF-8
$objPHPExcel->setActiveSheetIndex(0)
        ->setCellValue('A2', 'Miscellaneous glyphs')
        ->setCellValue('B2', 'test text');

// Rename sheet
$objPHPExcel->getActiveSheet()->setTitle('Simple');


// Set active sheet index to the first sheet, so Excel opens this as the first sheet
$objPHPExcel->setActiveSheetIndex(0);


// Redirect output to a client’s web browser (Excel5)
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="01simple.xls"');
header('Cache-Control: max-age=0');

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output'); 
exit;

我检查了所有配置设置并找到了所有必需的文件,但仍然出现此错误。如果我在此过程中做错任何事情,请向我提出任何建议。

谢谢你。

4

1 回答 1

3

看看这里:

http://www.auditbureau.org.au/a/Documentation/API/PHPExcel/PHPExcel_DocumentProperties.html#methodsetLastModifiedBy

setLastModfiedBy() 返回 void。因此,您不能链接调用您一直在进行的所有 ->setXX 调用。您应该分别在 getProperties() 的返回值上调用 each。

于 2012-11-05T10:02:23.480 回答