Symfony无法将数据导出到Excel

时间:2017-01-23 14:47:45

标签: excel symfony

大家好我正在使用Liuggio Excel Bundle但是无法使其工作:/我在我的用户列表上面添加了选项以将数据导出到excel但是当点击时没有任何反应,没有下载。 有人可以帮我一把吗?

也许我说错了?或它的工作,但我不知道它保存文件的位置?? 这就是我称之为方法的方式。

<a href="{{ path('excel_list') }}" class="btn btn-sm btn-info">Export Data</a>

路由:

excel_list:
path:     /excel_list
defaults: { _controller: PaginasUsersBundle:Default:ExcelExport }

这是我的控制器的方法:

//----------------------------------------------------------
//---------------------------Excel--------------------------
//----------------------------------------------------------

public function ExcelExportAction(Request $request)
{
$em=$this->getDoctrine()->getManager();

$query=$em->getRepository('PaginasUsersBundle:Users')
    ->createQueryBuilder('u')
    ->select('u.id, u.name, u.username, u.email')
    ->getQuery();

$result=$filterQuery->getResult();

$phpExcelObject = $this->get('phpexcel')->createPHPExcelObject();

$phpExcelObject->getProperties()
    ->setCreator("xxx")
    ->setLastModifiedBy("xxxx")
    ->setTitle("Excel Example")
    ->setSubject("Example")
    ->setDescription("Example List");

$phpExcelObject->setActiveSheetIndex(0);
$phpExcelObject->getActiveSheet()->setTile('Export Example');

$phpExcelObject->setACtiveSheetIndex(0)
    ->setCellValue('B2','ID')
    ->setCellValue('C2','Name')
    ->setCellValue('D2','Username')
    ->setCellValue('E2','Email');

//fijamos un ancho a las distintas columnas
$phpExcelObject->setActiveSheetIndex(0)
    ->getColumnDimension('B')
    ->setWidth(30);
$phpExcelObject->setActiveSheetIndex(0)
    ->getColumnDimension('C')
    ->setWidth(25);
$phpExcelObject->setActiveSheetIndex(0)
    ->getColumnDimension('D')
    ->setWidth(15);
$phpExcelObject->setActiveSheetIndex(0)
    ->getColumnDimension('E')
    ->setWidth(20);

$row=3;
foreach ($result as $item){
    $phpExcelObject->setActiveSheetIndex(0)
        ->setCellValue('B'.$row, $item['id'])
        ->setCellValue('C'.$row, $item['name'])
        ->setCellValue('D'.$row, $item['username'])
        ->setCellValue('E'.$row, $item['email']);
    $row ++;
}

$writer = $this->get('phpexcel')->createWriter($phpObject, 'Excel5');

$response = $this->get('phpexcel')->createStreamedREsponse($writer);

$dispositionHeader = $response->headers->makeDisposition(
    ResponseHeaderBag::DISPOSITION_ATTACHMENT,
    'userList.xls'
);

$response->headers->set('Content-Type', 'text/vnd.ms-excel; charset=utf-8');
$response->headers->set('Pragma', 'public');
$response->headers->set('Cache-control','maxage=1');
$response->headers->set('Content-Disposition', $dispositionHeader);

return $response;

}

提前致谢

1 个答案:

答案 0 :(得分:1)

我已经解决了它,它很简单,但如果有人遇到同样的问题,这就是答案。

只需要保存excel,我正在生成excel但从不保存它哈哈,所以我添加了这一行:

$writer->save('/path/to/save/filename.xls'); 

感谢所有人的帮助:)