Php 设置标题并下载文件后,代码不会继续
我有一个索引函数,它显示了一些顺序图。在这个索引视图中,用户可以选择从、到的日期,一旦表单提交,所选日期的订单图就会更新。现在我正在尝试实现另一个功能,将数据导出到excel,方法是在这两个日期选择中添加简单的选择选项 问题是,当您不想导出Excel时,必须设置标题,一旦设置了标题,代码就不能像我所希望的那样继续 这是我的索引函数Php 设置标题并下载文件后,代码不会继续,php,cakephp,cakephp-2.0,phpexcel,Php,Cakephp,Cakephp 2.0,Phpexcel,我有一个索引函数,它显示了一些顺序图。在这个索引视图中,用户可以选择从、到的日期,一旦表单提交,所选日期的订单图就会更新。现在我正在尝试实现另一个功能,将数据导出到excel,方法是在这两个日期选择中添加简单的选择选项 问题是,当您不想导出Excel时,必须设置标题,一旦设置了标题,代码就不能像我所希望的那样继续 这是我的索引函数 public function index() { $orderData = $this->Order->getDashboardOrde
public function index() {
$orderData = $this->Order->getDashboardOrdersStatisticBetweenData();
if ($this->request->is('post') || $this->request->is('put')) {
$dateFrom = $this->request->data['orderSumDates']['date_from'];
$dateTo = $this->request->data['orderSumDates']['date_to'];
$orderData = $this->Order->getDashboardOrdersStatisticBetweenData($dateFrom, $dateTo);
if ($this->request->data['orderSumDates']['export_type'] == 'export_excel') {
$this->generateExcelFile($orderData, $dateFrom, $dateTo);
die('Code never gets here, but file is downloaded');
}
}
$this->set('orderStatistic', $orderData);
}
这是我的生成excel文件功能
protected function generateExcelFile($orderData, $dateFrom, $dateTo) {
header('Content-type: application/vnd.ms-excel');
header('Content-Disposition: attachment; filename="OrderReport'.$dateFrom.'-'.$dateTo.'.xlsx"');
$objPHPExcel = new PHPExcel();
$objPHPExcel->setActiveSheetIndex(0);
// Summary of report
$objPHPExcel->getActiveSheet()->SetCellValue('A5', 'Total number of orders');
// Some other stuff
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');
header_remove('Content-type');
header_remove('Content-Disposition');
}
所以问题是,如果我选择export_excel选项,$this->generateExcelFile函数被执行,excel文件被下载,但其余的代码永远不会发生,比如这个die‘代码永远不会到达这里,但文件被下载’;我不想被处决。我已经做了一些测试,如果我注释掉$this->generateExcelFile函数的头部分,代码通常会被执行,但是excel文件没有正确生成,所以这些头是至关重要的。你能帮我解决我的问题吗 您可以保存该Excel文件,然后使用Cakephp内置函数将其发送到浏览器 1-在TMP文件夹上创建文件夹文件 2-将该文件夹中生成的文件保存到函数中,并返回文件位置
protected function generateExcelFile($orderData, $dateFrom, $dateTo) {
//header('Content-type: application/vnd.ms-excel'); // REMOVE THIS LINE
//header('Content-Disposition: attachment; filename="OrderReport' . $dateFrom . '-' . $dateTo . '.xlsx"'); // REMOVE THIS LINE
$objPHPExcel = new PHPExcel();
$objPHPExcel->setActiveSheetIndex(0);
// Summary of report
$objPHPExcel->getActiveSheet()->SetCellValue('A5', 'Total number of orders');
// Some other stuff
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$tmpFile = TMP . "files" . DS . sprintf("excel-file-%s.xlsx", date('Y-m-d-H-i-s')); // The tmp file
$objWriter->save($tmpFile); // Save on excel file
return $tmpFile; //send the file location
//$objWriter->save('php://output'); // REMOVE THIS LINE
//header_remove('Content-type'); // REMOVE THIS LINE
//header_remove('Content-Disposition'); // REMOVE THIS LINE
}
3-在您的操作中,删除此文件后将该文件内容发送到浏览器
public function index() {
$orderData = $this->Order->getDashboardOrdersStatisticBetweenData();
if ($this->request->is('post') || $this->request->is('put')) {
$dateFrom = $this->request->data['orderSumDates']['date_from'];
$dateTo = $this->request->data['orderSumDates']['date_to'];
$orderData = $this->Order->getDashboardOrdersStatisticBetweenData($dateFrom, $dateTo);
if ($this->request->data['orderSumDates']['export_type'] == 'export_excel') {
$excelFile = $this->generateExcelFile($orderData, $dateFrom, $dateTo);
//Get the file content
$content = file_get_contents($excelFile);
//Delete that file
unlink($excelFile);
//Put the content on the response
$this->response->body($content);
//Force download (test.xlsx is the file name browser will recieve)
$this->response->download("test.xlsx");
//spécify the response type
$this->response->type("application/vnd.ms-excel");
//send the response
return $this->response;
}
}
$this->set('orderStatistic', $orderData);
}
是否启用了错误?你检查错误日志了吗?没有错误。。。代码正在运行,问题是写错了。一旦在generateExcelFile函数中设置了头,就不会在索引函数中继续执行。在返回头之前,请尝试执行代码。如果没有执行代码,则可能是某个地方发生了错误,或者代码被明确停止。在函数中,是否尝试返回$objwriter而不是显示它。您可以从控制器中显示它,并在PHPExcel实例运行后设置标题created@TedRed你能分享一个例子吗?谢谢!最后我做的和你做的不完全一样,但是你给了我一些很好的指引;
public function index() {
$orderData = $this->Order->getDashboardOrdersStatisticBetweenData();
if ($this->request->is('post') || $this->request->is('put')) {
$dateFrom = $this->request->data['orderSumDates']['date_from'];
$dateTo = $this->request->data['orderSumDates']['date_to'];
$orderData = $this->Order->getDashboardOrdersStatisticBetweenData($dateFrom, $dateTo);
if ($this->request->data['orderSumDates']['export_type'] == 'export_excel') {
$excelFile = $this->generateExcelFile($orderData, $dateFrom, $dateTo);
//Get the file content
$content = file_get_contents($excelFile);
//Delete that file
unlink($excelFile);
//Put the content on the response
$this->response->body($content);
//Force download (test.xlsx is the file name browser will recieve)
$this->response->download("test.xlsx");
//spécify the response type
$this->response->type("application/vnd.ms-excel");
//send the response
return $this->response;
}
}
$this->set('orderStatistic', $orderData);
}