简体   繁体   中英

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. 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:

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

thanks to all for your help :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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