Zend2控制器中的PHPExcel

时间:2013-05-14 19:32:59

标签: php zend-framework2 phpexcel

我正在尝试让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;
}

但它在我的页面上给了我“echo like”输出。我的第二次尝试是:

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

结果却是一样的。

2 个答案:

答案 0 :(得分:2)

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

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

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

$response->setContent($excelOutput);

答案 1 :(得分: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应用程序。