我有一个网站,我想用PHPExcel创建一个报告文件。 我为第一张纸制作了两个循环,其中我有一周摘要,另一个循环用于一周中的每一天。 我用我的脚本检索了这个错误:
Catchable fatal error: Argument 1 passed to PHPExcel_IOFactory::createWriter() must be an instance of PHPExcel, null given, called in /var/www/reports/generate_reports_settimanale.php on line 79 and defined in /var/www/inc/PHPExcel/IOFactory.php on line 132
这是我的简化代码:
require_once '../inc/PHPExcel.php';
require_once '../inc/PHPExcel/IOFactory.php';
objPHPExcel = new PHPExcel();
// Set document properties
$objPHPExcel->getProperties()->setCreator("Alessandro Minoccheri")
->setLastModifiedBy("Alessandro Minoccheri")
->setTitle("Office 2007 XLSX Test Document")
->setSubject("Office 2007 XLSX Test Document")
->setDescription("Generazione report inverter")
->setKeywords("office 2007 openxml php")
->setCategory("");
$row=1;
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow('0', $row, 'Inverter');
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow('1', $row, 'Channel');
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow('0', 1, 'inverter');
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow('1', 1, 'channel');
$objPHPExcel->setActiveSheetIndex(0);
$objWriter3 = PHPExcel_IOFactory::createWriter($objPHPExcel3, 'Excel5');
$week_end = date( 'Y-m-d 23:59:59', $start + ( 6 * 86400 ) );
$week_start = date( 'Y-m-d 00:00:00', $start );
$date = $week_start;
while (strtotime($date) <= strtotime($week_end)) {
$date = date ("Y-m-d", strtotime("+1 day", strtotime($date)));
$objWorkSheet = $objPHPExcel->createSheet($date); //Setting index when creating
//Write cells
$objWorkSheet->setCellValue('A1', 'Hello'.$i)
->setCellValue('B2', 'world!')
->setCellValue('C1', 'Hello')
->setCellValue('D2', 'world!');
// Rename sheet
$objWorkSheet->setTitle("test");
}
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="report.xls"');
header('Cache-Control: max-age=0');
$objWriter3->save('php://output');
我该如何解决这个问题?
答案 0 :(得分:2)
$objPHPExcel->setActiveSheetIndex(0);
$objWriter3 = PHPExcel_IOFactory::createWriter($objPHPExcel3, 'Excel5');
正如错误所说...... $ objPHPExcel3尚不存在。到目前为止,您只在脚本中使用了$ objPHPExcel。你发送的是一个NULL值。 (在查看代码时,我没有看到你创建了一个$ objPHPExcel3)