2

我试图让 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' ));

然而结果是一样的。

4

2 回答 2

2

正如@Aydin Hassan 评论的那样,我尝试过:

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
ob_start();
$objWriter->save('php://output');
$excelOutput = ob_get_clean();

然后简单地传递$excelOutput给响应内容,效果非常好!

$response->setContent($excelOutput);
于 2013-05-17T15:01:56.270 回答
0

因此,您可以在控制器操作中归档如下内容:

 public function testPHPExcelAction() {
    // I recommend constructor injection for all needed dependencies ;-)
    $this->phpExcelService = $this->serviceLocator->get('mvlabs.phpexcel.service');

    $objPHPExcel = $this->phpExcelService->createPHPExcelObject();
    $objPHPExcel->getProperties()->setCreator("Diego Drigani")
        ->setLastModifiedBy("Diego Drigani")
        ->setTitle("MvlabsPHPExcel Test Document")
        ->setSubject("MvlabsPHPExcel Test Document")
        ->setDescription("Test document for MvlabsPHPExcel, generated using Zend Framework 2 and PHPExcel.")
        ->setKeywords("office PHPExcel php zf2 mvlabs")
        ->setCategory("Test result file");
    $objPHPExcel->setActiveSheetIndex(0)
        ->setCellValue('A1', 'Hello')
        ->setCellValue('B2', 'world!')
        ->setCellValue('C1', 'Hello')
        ->setCellValue('D2', 'world!');
    $objPHPExcel->setActiveSheetIndex(0)
        ->setCellValue('A4', 'Miscellaneous glyphs')
        ->setCellValue('A5', 'éàèùâêîôûëïüÿäöüç');

    $objPHPExcel->getActiveSheet()->setCellValue('A8',"Hello\nWorld");
    $objPHPExcel->getActiveSheet()->getRowDimension(8)->setRowHeight(-1);
    $objPHPExcel->getActiveSheet()->getStyle('A8')->getAlignment()->setWrapText(true);
    $objPHPExcel->getActiveSheet()->setTitle('Mvlabs');
    $objPHPExcel->setActiveSheetIndex(0);

    $objWriter = $this->phpExcelService->createWriter($objPHPExcel, 'Excel2007' );

    $response = $this->phpExcelService->createHttpResponse($objWriter, 200, [
        'Pragma' => 'public',
        'Cache-control' => 'must-revalidate, post-check=0, pre-check=0',
        'Cache-control' => 'private',
        'Expires' => '0000-00-00',
        'Content-Type' => 'application/vnd.ms-excel; charset=utf-8',
        'Content-Disposition' => 'attachment; filename=' . 'myTest.xls',
        ]);

    return $response;

}    

要以上述方式执行此操作,您需要使用MvlabsPHPExcel模块,该模块使您可以轻松地将PHPOffice/PHPExcel库用于 ZF2 应用程序。

于 2017-03-18T20:54:15.033 回答