How do I return a response in Symfony that will output an excel file? I am getting an error that I must return a response and it needs to be in a string correct? Help!
$excel = new PHPExcel();
$excel->setActiveSheetIndex(0);
$excel->getActiveSheet()
->setCellValue('A1', 'hi');
$objWriter = \PHPExcel_IOFactory::createWriter($excel, 'Excel2007');
$objWriter->save(str_replace('.php', '.xlsx', __FILE__));
return $objWriter;
Error:
The controller must return a response (Object(PHPExcel_Writer_Excel2007) given).
Controller must return an instance of Response
class, so do the following:
ob_start();
$objWriter->save('php://output');
return new Response(
ob_get_clean(), // read from output buffer
200,
array(
'Content-Type' => 'application/vnd.ms-excel',
'Content-Disposition' => 'attachment; filename="doc.xls"',
)
);
If you use Excel 2007 or upper set content type to "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
and file extension to xlsx
.
In my case, I've got a route and a controller to export an XLS generated (in my case a button access the route and returns the XLS file)
Also, I read a already generated and stored file, but it should work even if you're not storing the file, you should just use the $objWriter;
in this line
$response = $this->get('phpexcel')->createStreamedResponse($writer);
Also, I'm not using the Response object that VisioN indicated I should.
The Action:
public function exportAction() {
$readerObject = PHPExcel_IOFactory::createReader('Excel5');
$phpExcelObject = $readerObject->load('files/downloads/reports/' . $this->getUser()->getName() . '.xls');
$writer = $this->get('phpexcel')->createWriter($phpExcelObject, 'Excel5');
$response = $this->get('phpexcel')->createStreamedResponse($writer);
$response->headers->set('Content-Type', 'text/vnd.ms-excel; charset=utf-8');
$response->headers->set('Content-Disposition', 'attachment;filename=' . $this->getUser()->getName() . '.xls');
$response->headers->set('Pragma', 'public');
$response->headers->set('Cache-Control', 'maxage=1');
return $response;
}
Please note, 'Excel5'
is for XLS files, if you desire XLSX files you should replace those with 'Excel2007'
Another example using the request symfony component:
use Symfony\Component\HttpFoundation\Request;
/**
* Create a zip file with excel contain the list of clubs.
*
* @param Request $request
* @param $the_response_excel_file
* @param $the_file_name
*/
public function exportAction(Request $request, $the_response_excel_file, $the_file_name)
{
// adding headers
$dispositionHeader = $response->headers->makeDisposition(
ResponseHeaderBag::DISPOSITION_ATTACHMENT,
$filename
);
$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;
}
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.