1

我正在尝试获取与 Zend Framework 2.4.11 和 phpoffice/phpexcel 1.8.1 一起使用的 Excel 书籍。我的控制器代码是这样的:

public function exportAction()
{
    $phpExcel = new \PHPExcel();
    $phpExcel->setActiveSheetIndex(0);

    $phpExcel->getProperties()->setCreator("Me")
        ->setLastModifiedBy("Someone")
        ->setTitle("Report")
        ->setSubject("Demo Report");        
    $activeSheet = $phpExcel->getActiveSheet();

    $results = array('BlahA1', 'BlahA2', 'BlahA3');

    $index = 1;
    foreach ($results as $result){
        $activeSheet->SetCellValue('A'.$index++, $result);
    }
    $objWriter = \PHPExcel_IOFactory::createWriter($phpExcel, 'Excel2007');
    ob_flush();
    ob_start();

    $headers = array(
        'Last-Modified'             => gmdate("D, d M Y H:i:s") . ' GMT',
        'Cache-Control'             => 'no-cache, no-store, must-revalidate',
        'Pragma'                    => 'public',
        'Content-Type'              => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
        'Content-Disposition'       => 'attachment;filename="Report.xlsx"',
        'Content-Transfer-Encoding' => 'binary'
    );
    $response = $this->getResponse();
    $response->getHeaders()->addHeaders($headers);

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

    return $response;
}

工作正常,我可以打开一本 Excel 书,但是当我尝试使用服务定位器从我的数据库中获取数据时不起作用,例如:

class PagamentsController extends AbstractActionController
{
    protected $pagamentTable;

    public function exportAction(){
    ...
        $results = $this->GetPagamentTable()->fetchAllRegs();
    ...
    }

    private function GetPagamentTable()
    {
        if (!$this->pagamentTable) {
            $sm = $this->getServiceLocator();
            $this->pagamentTable = $sm->get('Gestio\Model\PagamentTable');
        }
        return $this->pagamentTable;
    }    

我的模型的代码是:

class PagamentTable
{
    protected $tableGateway;

    public function __construct(TableGateway $tableGateway)
    {
        $this->tableGateway = $tableGateway;
    }

    public function fetchAllRegs()
    {
        return array('BlahA1', 'BlahA2', 'BlahA3');
    }
}

我的 Module.php 是:

   public function getServiceConfig()
   {
        return array(
            'factories' => array(
               'Gestio\Model\PagamentTable' => function ($sm) {
                    $tableGateway = $sm->get('PagamentTableGateway');
                    $table = new PagamentTable($tableGateway);
                    return $table;
                },
                'PagamentTableGateway' => function ($sm) {
                    $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
                    $resultSetPrototype = new ResultSet();
                    $resultSetPrototype->setArrayObjectPrototype(new Pagament());
                    return new TableGateway('pagaments', $dbAdapter, null, $resultSetPrototype);
                },
            )
        );
   }

输出是这样的:浏览器输出

遇到过类似问题并找到解决方案的人可以在这里吗?

提前致谢。

4

0 回答 0