在drupal 7中提交表单时,使用phpExcel作为附件下载生成的excel文件



这是我的代码。表单已提交,但不会将任何文件作为附件下载。没有错误

function dn_preorder_form_submit($form,&$form_state){   
$excel = new PHPExcel();
$excel->setActiveSheetIndex(0);
$excel->getActiveSheet()->setCellValue('A1',"TEST");
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment; filename="var.xlsx"');
header("Pragma: no-cache");
header("Cache-Control: no-cache");
header("Cache-Control: nmax-age=0");
$file = PHPExcel_IOFactory::createWriter($excel,'Excel2007');
$file->save('php://output');
}

我试过这个解决方案 https://drupal.stackexchange.com/questions/103690/download-file-on-form-submission 但我仍然得到相同的结果。 我已经在drupal之外尝试过这段代码,它可以工作

好的,我通过在代码末尾插入 exit(( 来解决它

function dn_preorder_form_submit($form,&$form_state){   
$excel = new PHPExcel();
$excel->setActiveSheetIndex(0);
$excel->getActiveSheet()->setCellValue('A1',"TEST");
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment; filename="var.xlsx"');
header("Pragma: no-cache");
header("Cache-Control: no-cache");
header("Cache-Control: nmax-age=0");
$file = PHPExcel_IOFactory::createWriter($excel,'Excel2007');
$file->save('php://output');
exit();
}

最新更新