打开excel导出文件时出现PHP SYMFONY问题

问题描述 投票:-1回答:1

我正在尝试使用phpspreadsheet将表播放器mysql导出到excel文件。问题在于,当下载文件时,当我们打开文件时,文件中具有html页面(包含excel内容)。因此,文件已损坏。可以肯定的是,返回的控制器无效。但是怎么办?

这里是控制器:

* @Route("/export", name="doExport", methods={"POST"})
*/
public function export(Connection $conn, ExportExcel $exportExcel, TrainerRepository $trainerRepository, StaffRepository $staffRepository)
{

  $exportExcel->exportExcel($conn);


  return $this->render('Effectif/home.html.twig', [
   'trainers' => $trainerRepository->findBy(['id'=>228]),
   'staffs' => $staffRepository->findAll()
]);
   }

问题图片:enter image description here完整的导出代码在这里:http://pastebin.fr/61555

$filename = 'joueurs.xlsx';

header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="'. $filename);
header('Cache-Control: max-age=0');
$Excel_writer->save('php://output');
    }
}

请不要关闭帖子。

php mysql excel symfony phpspreadsheet
1个回答
0
投票
* @Route("/export", name="doExport", methods={"POST"}) */ public function export(Connection $conn, ExportExcel $exportExcel, TrainerRepository $trainerRepository, StaffRepository $staffRepository) { $targetPath = $exportExcel->exportExcel($conn); $response = new BinaryFileResponse($targetPath); return $response; } }

在服务导出中:

$filename = 'joueurs.xlsx';

header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="'. $filename);
header('Cache-Control: max-age=0');
$download_dir = '../var/downloads/';
$targetPath = $download_dir.$filename;
$Excel_writer->save($targetPath);
return $targetPath;
    }
}
?>
© www.soinside.com 2019 - 2024. All rights reserved.