我试图让 PHPExcel 与 Zend2 一起工作。实际上它正在工作,但不是我想要的(我可以写入文件,但不能让下载而不保存)。我找到了一些示例,您只需执行以下操作:
$objPHPExcel = ....
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');
并且文件被允许下载。如何在 Zend2 Controller 中实现类似的功能?到目前为止我已经尝试过:
public function generateRaportAction()
{
$objPHPExcel = new PHPExcel();
$objPHPExcel->getActiveSheet()->setCellValue( 'B8', 'Some value' );
$objWriter = \PHPExcel_IOFactory::createWriter( $objPHPExcel, 'Excel5' );
$response = $this->getEvent()->getResponse();
$response->getHeaders()->clearHeaders()->addHeaders( array(
'Pragma' => 'public',
'Content-Type' => 'application/vnd.ms-excel',
'Content-Disposition' => 'attachment; filename="test.xls"',
'Cache-Control' => 'must-revalidate, post-check=0, pre-check=0',
'Content-Transfer-Encoding' => 'binary',
) );
$objWriter->save( 'php://output' );
return $response;
}
但它在我的页面上给了我“类似回声”的输出。我的第二次尝试是:
$response->setContent($objWriter->save( 'php://output' ));
然而结果是一样的。