PHPExcel in Zend2 Controller

2019-05-18 21:29发布

问题:

I'm trying to get PHPExcel working with Zend2. Actually it is working, but not as I intended (I can write to file, but cannot let download without saving). I found some examples, where you simply do something like this:

$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');

And file was allowed to download. How can I achive something similar in Zend2 Controller? I've tried so far:

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;
}

But it gives me "echo like" output on my page. My second attempt was:

$response->setContent($objWriter->save( 'php://output' ));

Yet result was the same.

回答1:

As @Aydin Hassan commented, I've tried with:

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

And then simply passed $excelOutput to the response content, and it works simply great!

$response->setContent($excelOutput);


回答2:

So you can archive, in your controller action, something like this:

 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;

}    

To do it in the above way, you need to use the MvlabsPHPExcel module that gives to you the possibility to use PHPOffice/PHPExcel library into a ZF2 application easily.