我想下载一个由PHPExcel生成的Excel文件。我按照PHPExcel Force Download Issue中的代码操作,但无济于事。在记录接收到的数据后,我的控制台只显示这些符号�。然而,当我将
注意:我使用AngularJS $http.post调用php文件。
浏览器:Google Chrome 编辑:我已经尝试了来自PHPExcel 01simple-download-xlsx.php的示例php代码,结果也相同。
更新:我通过搜索关于接受Excel作为响应获取AngularJS的一些解决方案,但在下载后,似乎文件被损坏并且有类似于控制台中注销的无意义文本或符号。此外,文件名是随机文本和数字,而不是我在php代码中分配的文件名。以下是代码:
$objWriter->save('php://output');
更改为$objWriter->save('filename.xlsx');
时,它会将文件下载到我的Web服务器根目录,没有任何下载文件的提示。以下是我的php文件的代码片段。<?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;
?>
以下是我的angularjs代码片段:
$scope.getReport = function(type){
if(type == 'lost'){
$http.post("getLostItemReport.php")
.success(function(data, status, headers, config){
console.log("Get report data: " + data);
})
}
}
注意:我使用AngularJS $http.post调用php文件。
浏览器:Google Chrome 编辑:我已经尝试了来自PHPExcel 01simple-download-xlsx.php的示例php代码,结果也相同。
更新:我通过搜索关于接受Excel作为响应获取AngularJS的一些解决方案,但在下载后,似乎文件被损坏并且有类似于控制台中注销的无意义文本或符号。此外,文件名是随机文本和数字,而不是我在php代码中分配的文件名。以下是代码:
var blob = new Blob([data], {type: "application/vnd.ms-excel"});
var objectUrl = URL.createObjectURL(blob);
application/vnd.ms-excel
或application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
的原始二进制流。 - Mark Bakerapplication/vnd.ms-excel
或application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
的原始二进制流”的问题。我会尝试寻找解决这个问题的方法。 - imationyj