简体   繁体   English

用ajax调用下载excel文件

[英]Download excel file with ajax call

$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. 但是,如果我对上述代码进行ajax调用,则不会收到下载提示。 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. 我可以从控制台选项卡中看到ajax调用已成功完成,并且在响应数据中看到了一堆随机字符。 I'm assuming that is the excel object. 我假设这是excel对象。

Does anyone know how I can achieve the download excel feature using ajax? 有谁知道我如何使用Ajax实现excel下载功能? 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. 当用户单击“导出”按钮时,应该对php文件进行ajax调用,并提示用户进行下载。

I reffer Passing data from PHP class to PHPExcel via AJAX but do not understand how to achieve my goal? 推荐通过AJAX将数据从PHP类传递到PHPExcel,但不了解如何实现我的目标?

PHP PHP

$objWriter = new PHPExcel_Writer_Excel5($objPHPExcel);
ob_start();
$objWriter->save("php://output");
$xlsData = ob_get_contents();
ob_end_clean();

$response =  array(
        'op' => 'ok',
        'file' => "data:application/vnd.ms-excel;base64,".base64_encode($xlsData)
    );

die(json_encode($response));

JS JS

$.ajax({
    type:'POST',
    url:"MY_URL.php",
    data: {},
    dataType:'json'
}).done(function(data){
    var $a = $("<a>");
    $a.attr("href",data.file);
    $("body").append($a);
    $a.attr("download","file.xls");
    $a[0].click();
    $a.remove();
});

You don't actually need Ajax. 您实际上不需要Ajax。

Using javascript's window.location.replace() if you get an url which sends a file, it will prompt to be downloaded without redirecting the browser. 使用javascript的window.location.replace()如果您获得发送文件的网址),它将提示您下载而不重定向浏览器。

If you need to post data, you can create a hidden form with the fields set up the way you need them, make sure that the "target" attribute of the form is set to "_BLANK" , and then submit the form programatically. 如果需要发布数据,则可以使用设置所需字段的方式创建隐藏表单,确保表单的“目标”属性设置为"_BLANK" ,然后以编程方式提交表单。

For first comment below : 对于下面的第一个评论:

maybe try replacing : 也许尝试更换:

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output'); 

with readfile ($file_name); readfile ($file_name);

当您单击按钮时,为什么不重定向到下载excel文件的页面

I solved my issue by following way. 我通过以下方式解决了我的问题。

add target=_blank in your ajax success function like below 在您的ajax成功函数中添加target = _blank,如下所示

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

On success open new window with my_url. 成功后,使用my_url打开新窗口。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM