dongtan8532 2014-12-29 08:14 采纳率: 0%
浏览 385
已采纳

使用ajax调用下载excel文件

$objPHPExcel = new PHPExcel();
$objPHPExcel->getActiveSheet()->setTitle('TestMessages');
header('Content-Type: application/vnd.ms-excel');
header("Content-Disposition: attachment; filename=test_form_".date("Y-m-d_H:i:s").".xls");
// If you're serving to IE 9, then the following may be needed
header('Cache-Control: max-age=1');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');

When I call above code directly from the browser, the result file is downloaded. But if I make an ajax call to above code, I don't get the download prompt. I can see from console tab that the ajax call was successfully completed and a bunch of random characters is seen in the response data. I'm assuming that is the excel object.

Does anyone know how I can achieve the download excel feature using ajax? I don't want to refresh the page. When the user clicks on the "export" button, there should be an ajax call to the php file and prompt the user to download.

I reffer Passing data from PHP class to PHPExcel via AJAX but do not understand how to achieve my goal?

  • 写回答

4条回答 默认 最新

  • duanshanduo3363 2014-12-30 08:49
    关注

    I solved my issue by following way.

    add target=_blank in your ajax success function like below

    success: function(){
      window.open('http://MY_URL','_blank' );
    },
    

    On success open new window with my_url.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 在若依框架下实现人脸识别
  • ¥15 网络科学导论,网络控制
  • ¥100 安卓tv程序连接SQLSERVER2008问题
  • ¥15 利用Sentinel-2和Landsat8做一个水库的长时序NDVI的对比,为什么Snetinel-2计算的结果最小值特别小,而Lansat8就很平均
  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同