I wanted to download an excel file generated using PHPExcel. I executed the code from the PHPExcel Force Download Issue and to no avail. After I log the data, my console displays only these characters. However, when I change $objWriter->save('php://output'); on $objWriter->save('filename.xlsx'); then it just uploads the file to my root folder on my web server without any indication of the download of the file. Below is a snippet of code for my php file
<?php $con = mysqli_connect('localhost', 'root', '', 'test'); mysqli_select_db($con, 'test'); $qry = "SELECT * FROM lostitem"; $res = mysqli_query($con, $qry); require_once '/Classes/PHPExcel.php'; include '/Classes/PHPExcel/Writer/Excel2007.php'; // Create new PHPExcel object $objPHPExcel = new PHPExcel(); /** Determine filename **/ $filename = "report.xlsx"; /** Set header information **/ header('Content-Type: application/vnd.ms-excel'); header('Content-Disposition: attachment;filename="' . $filename . '"'); header('Cache-Control: max-age=0'); $F=$objPHPExcel->getActiveSheet(); $Line=1; $F->setCellValue('A'.$Line, 'Lost Item ID'); $F->setCellValue('B'.$Line, 'Lost Item Title'); $F->setCellValue('C'.$Line, 'Lost Item Description'); while($Trs=mysqli_fetch_assoc($res)){//extract each record ++$Line; $F->setCellValue('A'.$Line, $Trs['LostItemID']); $F->setCellValue('B'.$Line, $Trs['LostItemTitle']); $F->setCellValue('C'.$Line, $Trs['LostItemDescription']);//write in the sheet //++$Line; } // Redirect output to a client's web browser (Excel5) header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'); header('Content-Disposition: attachment;filename="'.$filename.'"'); header('Cache-Control: max-age=0'); $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007'); $objWriter->save('php://output'); exit; ?>
Here is my angularjs code snippet:
$scope.getReport = function(type){ if(type == 'lost'){ $http.post("getLostItemReport.php") .success(function(data, status, headers, config){ console.log("Get report data: " + data); }) } }
Note. I am using AngularJS $ http.post to call a php file.
Browser: Google Chrome
EDIT: I tried the php code example from PHPExcel 01simple-download-xlsx.php and the result is the same too.
UPDATE: I found some solution by doing a search in AngularJS, taking Excel as the answer, but after downloading it it seems that the file is corrupted by text or a mystery symbol, similar to the one coming out of the system in my console. Also, the file name is random text and numbers instead of the file name that I assigned in my php code. The code is as follows:
var blob = new Blob([data], {type: "application/vnd.ms-excel"}); var objectUrl = URL.createObjectURL(blob);