Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHPExcel download using ajax call

App::import('Vendor', 'PHPExcel/Classes/PHPExcel');
$objPHPExcel = new PHPExcel();
$objPHPExcel->getActiveSheet()->setTitle('ReceivedMessages');
header('Content-Type: application/vnd.ms-excel');
$file_name = "kpi_form_".date("Y-m-d_H:i:s").".xls";
header("Content-Disposition: attachment; filename=$file_name");
// 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.

like image 333
Sadikhasan Avatar asked Dec 30 '14 08:12

Sadikhasan


People also ask

How to export PHP to excel using jQuery Ajax?

Create an Export PHP file which we call using Ajax. Create our main JS file to hit the download. Download complete source code of export to excel using jquery ajax call in PHP. We create a MySQL DB connection file where we use mysql_connect () to connect the database. echo "Something went wrong.";

How do I download an Excel file using Ajax?

Downloading Excel File using AJAX in JavaScript When the Download Button is clicked, the DownloadFile JavaScript function is called. Inside the DownloadFile JavaScript function, the URL of the File is passed as parameter to the GET call of the JavaScript XmlHttpRequest call.

How to fetch data from Excel to PHP script?

If you want to fetch data from any excel document to php then you can use this PHPExcel library. We have use ajax request to upload upload excel file and then after by using this PHPExcel library we have use that excel data in php script.

How to download Excel file on button click using JavaScript?

Here Mudassar Ahmed Khan has explained with an example, how to download Excel File on Button click using JavaScript. The Excel file will be downloaded as BLOB using XmlHttpRequest AJAX call and then will be sent for download in the Browser using JavaScript.


3 Answers

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

$.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();
});
like image 180
Cristiano Gehring Avatar answered Oct 18 '22 18:10

Cristiano Gehring


add target=_blank in your ajax success function like below

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

otherwise you can handle smartly to open your Excel download link in new tab with jQuery trigger function or etc.

like image 36
Wiram Rathod Avatar answered Oct 18 '22 18:10

Wiram Rathod


You cannot download a file using ajax neither by phpexcel nor by php itself as its a security reason and almost browsers doesn't support it. But, you can try window.location in success callback like,

var page='mydownload.php';
$.ajax({
    url: page,
    type: 'POST',
    success: function() {
        window.location = page;// you can use window.open also
    }
});

Also @freakish answered for this type of question

Even, you don't need of ajax you can use hyperlink for the page like,

<a href="mydownload.php" target="_blank" >Download</a>
like image 9
Rohan Kumar Avatar answered Oct 18 '22 18:10

Rohan Kumar