PHPExcel下载无法正常工作
我正在尝试创建一个页面,允许用户将SQL表的内容下载到excel文件中 问题:当我打开excel文件时,它只包含随机乱码。一个例子-PHPExcel下载无法正常工作,php,mysql,phpexcel,Php,Mysql,Phpexcel,我正在尝试创建一个页面,允许用户将SQL表的内容下载到excel文件中 问题:当我打开excel文件时,它只包含随机乱码。一个例子- PKQ=DG’D²Xð[Content_Types].xml”MNÃ0…÷œ"ò%nY „švAa •(0ö¤±êØ–gúw{&i‰@ÕnbEö{ßøyìÑdÛ¸l mð¥‘×ÁX¿(ÅÛü)¿’òF¹à¡;@1_滘±Øc)j¢x/%ê…Eˆày¦ 这是我的密码- <?php error_reporting(E_ALL); ini_s
PKQ=DG’D²Xð[Content_Types].xml”MNÃ0…÷œ"ò%nY „švAa
•(0ö¤±êØ–gúw{&i‰@ÕnbEö{ßøyìÑdÛ¸l
mð¥‘×ÁX¿(ÅÛü)¿’òF¹à¡;@1_滘±Øc)j¢x/%ê…Eˆày¦
这是我的密码-
<?php
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);
$download="";
if (isset($_GET['surveyid'])) {
//Survey ID
$download = $_GET['surveyid'];
require_once('../Classes/PHPExcel.php');
$query="SELECT b.question_id as qid,
a.question as ques,
b.response as response,
count(b.response) as cnt
FROM v3_sai.survey_responses b
INNER JOIN v3_sai.survey_questions a
ON a.id = b.question_id
AND a.survey_id=".intval($download)."
group by b.response, a.question
order by b.question_id";
var_dump($query);
$resultdl= mysql_query($query) or die(mysql_error());
$objPHPExcel = new PHPExcel();
$objPHPExcel->setActiveSheetIndex(0);
$rowcount=1;
while($row = mysql_fetch_array($resultdl)){
$objPHPExcel->getActiveSheet()->SetCellValue('A'.$rowcount, $row['qid']);
$objPHPExcel->getActiveSheet()->SetCellValue('B'.$rowcount, $row['ques']);
$objPHPExcel->getActiveSheet()->SetCellValue('C'.$rowcount, $row['response']);
$objPHPExcel->getActiveSheet()->SetCellValue('D'.$rowcount, $row['cnt']);
$rowCount++;
}
$objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
header('Content-type: application/vnd.ms-excel');
header('Content-Disposition: attachment; filename="file.xls"');
$objWriter->save('php://output');
die();
}
您可以这样使用
<?php
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=abc".xls");
header("Pragma: no-cache");
header ("Expires: 0");
?>
<table width="100%" border="1">
<tr>
<td>
<h1> qid</h1>
</td>
<td>
<h1> ques</h1>
</td>
<td>
<h1> response</h1>
</td>
<td>
<h1> cnt</h1>
</td>
</tr>
while($row = mysql_fetch_array($resultdl)){
<tr>
<td>
<?php echo $row['qid']; ?>
</td>
<td>
<?php echo $row['ques']; ?>
</td>
<td>
<?php echo $row['response']; ?>
</td>
<td>
<?php echo $row['cnt']; ?>
</td>
</tr>
<?php } ?>
</table>
尝试此操作并禁用错误报告
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");;
header("Content-Disposition: attachment;filename=file.xls");
header("Content-Transfer-Encoding: binary ");
$objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
$objWriter->setOffice2003Compatibility(true);
$objWriter->save('php://output');
如果您正在下载xls
文件(BIFF),请使用PHPExcel\u Writer\u Excel5
Writer;如果您正在下载.xlsx
文件(OfficeOpenXML),请使用PHPExcel\u Writer\u Excel2007
Writer:不要混搭。。。那是你的问题。您正在使用Excel2007 Writer创建一个.xlsx(OfficeOpenXML)文件,但设置了标题以告知浏览器需要一个.xls(BIFF)文件
建议下载.xls(BIFF)的标题为:
// Redirect output to a client’s web browser (Excel5)
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="01simple.xls"');
header('Cache-Control: max-age=0');
// If you're serving to IE 9, then the following may be needed
header('Cache-Control: max-age=1');
// If you're serving to IE over SSL, then the following may be needed
header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified
header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1
header ('Pragma: public'); // HTTP/1.0
// Redirect output to a client’s web browser (Excel2007)
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="01simple.xlsx"');
header('Cache-Control: max-age=0');
// If you're serving to IE 9, then the following may be needed
header('Cache-Control: max-age=1');
// If you're serving to IE over SSL, then the following may be needed
header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified
header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1
header ('Pragma: public'); // HTTP/1.0
建议下载.xlsx(OfficeOpenXML)的标题为:
// Redirect output to a client’s web browser (Excel5)
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="01simple.xls"');
header('Cache-Control: max-age=0');
// If you're serving to IE 9, then the following may be needed
header('Cache-Control: max-age=1');
// If you're serving to IE over SSL, then the following may be needed
header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified
header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1
header ('Pragma: public'); // HTTP/1.0
// Redirect output to a client’s web browser (Excel2007)
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="01simple.xlsx"');
header('Cache-Control: max-age=0');
// If you're serving to IE 9, then the following may be needed
header('Cache-Control: max-age=1');
// If you're serving to IE over SSL, then the following may be needed
header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified
header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1
header ('Pragma: public'); // HTTP/1.0
请注意,内容类型
,内容配置
,可能被浏览器视为区分大小写,因此内容类型
与内容类型
不同。。。。我相信这也会给你带来问题我遇到了同样的问题,并最终找到了原因。
在$objWriter->save()之前的某个地方php://output“)
被调用,输出缓冲区被无意写入。
您可以在$objWriter->save()之前通过var\u dump(ob\u get\u contents())
来测试这一点php://output)
一个快速解决方法是添加ob_在脚本开始处启动(),然后ob_结束_clean()
就在$objWriter->save()之前php://output“)
但最理想的解决办法是找到输出缓冲区的填充位置。我知道我可能会对此做出晚一点的响应,但上述所有方法都不适用于我。使用ob_clean()
将删除任何缓存的材料,以便它不会干扰返回的标题+文件内容。重要的是你在哪里消除多余的胡言乱语。所以在我搔了几天的头之后,我注意到你需要ob_clean()代码>就在标题之前。如果你在别的地方做,你也会有同样的问题。下面是一个对我有用的示例代码
ob_clean();
$fileName = "Test.xlsx";
# Output headers.
header("Set-Cookie: fileDownload=true; path=/");
header("Cache-Control: private");
header("Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
header("Content-Disposition: attachment; filename='".$fileName."'");
// If you're serving to IE 9, then the following may be needed
header('Cache-Control: max-age=1');
// If you're serving to IE over SSL, then the following may be needed
header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified
header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1
header ('Pragma: public'); // HTTP/1.0
# Output file.
$return->save('php://output');
die();
试过这个。无变化:(仍然是胡言乱语)诸神,公认的答案是创建html标记并假装它是Excel文件……世界将要发生什么?它确实有效……其他解决方案都不起作用:|它做了一些完全不同的事情,MS Excel足够慷慨地处理……这可能足以满足您的需要,但实际上它并没有回答您的问题;一些较新版本的Excel可能会抱怨文件格式和扩展名之间的差异,更不用说标题(“内容处置:附件;文件名=abc.xls”);
和标题(“Pragma:no cache”);(“Expires:0”)中的语法错误了;
如何强制下载此文件?执行此操作时出现的错误是-“Excel无法打开文件'01simple-1.xlsx',因为文件格式或文件扩展名无效。请验证文件是否已损坏,以及文件扩展名是否与文件格式匹配。“Excel仍然会打开,尽管某些版本的Excel会抱怨文件格式与文件扩展名不匹配……这就是为什么您应该发送与您正在创建的文件类型匹配的正确标题:但您的问题不清楚您是否看到PKQ=DG'D²Xð[Content_Types].xml-“MNÃ0…÷œ”ònYšvAa•在浏览器中,或在打开文件备查时,在MS Excel中,这是您称之为“垃圾邮件”的数据filestream…这是Excel的一个实际Excel文件;但是,由于您拥有所有附加的表数据,因此在这种情况下,您是否在遍历数据库查询结果集以构建html标记,然后再次遍历它以填充PHPExcel对象?如果是,您不应该构建html标记首先,您的代码没有显示这一点,但它可以解释为什么文件显然是“加载”的,并作为html导入,最后是垃圾数据流
;除非您重置结果集,使其在开始时再次循环(使用)这样就不会有更多的行需要循环了……这一点,再加上@markbarker关于“不要混搭”的回答,解决方案奏效了。最后,ob_clean and die成功了。