简体   繁体   English

Symfony 无法将数据导出到 Excel

[英]Symfony Can't export data to Excel

hello everyone im using Liuggio Excel Bundle but cant make it work :/ i added and option above my list of users to export the data to excel but when is clicked nothing happens, no download.大家好,我正在使用 Liuggio Excel Bundle,但无法使其正常工作:/ 我在用户列表上方添加了选项以将数据导出到 excel,但单击时什么也没有发生,也没有下载。 could somebody give me a hand?有人可以帮我一把吗?

maybe im calling it wrong??也许我说错了?? or its working but i dont know where it save the file??或者它的工作,但我不知道它保存文件的位置? this is how i call the method.这就是我调用该方法的方式。

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

routing:路由:

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

here is the method of my controller:这是我的控制器的方法:

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

}

thanks in advance提前致谢

i already solved it, it was so simple but here is the answer if anyone have the same problem..我已经解决了,很简单,但如果有人遇到同样的问题,这里是答案。

just need to save the excel, i was generating the excel but never saving it haha, so i added this line:只需要保存excel,我正在生成excel但从未保存它哈哈,所以我添加了这一行:

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

thanks to all for your help :)感谢大家的帮助 :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM