我正在尝试将我的symfony2应用程序导出到Excel。我可以让它写入磁盘没有问题,但当我尝试通过symfony响应导出它时,我得到了很多gobbledygook。任何人都可以提出建议:
function exportAction()
{
$fname = "/tmp/test_xls.xlsx";
$php_excel = new \PHPExcel();//
$php_excel->setActiveSheetIndex(0);
$sheet = $php_excel->getActiveSheet();
$sheet->setCellValue('A1', 'Hello');
$sheet->setCellValue('B2', 'world!');
$writer = new \PHPExcel_Writer_Excel2007($php_excel);
$writer->save($fname);
$content = file_get_contents($fname);
return new Response(
$content,
200,
array(
'Content-Type' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet; charset=utf-8',
'Content-Disposition' => 'attachment; filename="test_957export.xlsx"',
'Content-Transfer-Encoding' => 'application/octet-stream',
'Content-Length' => strlen($content)
)
);
}
答案 0 :(得分:0)
您的标题名称无效。从Content-Transfer-Encoding和Content-Length中删除':'。如果它没有帮助,请尝试删除一些标题。
类似的问题:Convert image to string (for Symfony2 Response)(可能会有所帮助)。