我正在尝试创建一个页面,允许用户将 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_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();
}
最佳答案
如果您正在下载 xls
文件 (BIFF),请使用 PHPExcel_Writer_Excel5
Writer;如果您正在下载 .xlsx
文件 (OfficeOpenXML),请使用 PHPExcel_Writer_Excel2007
Writer:不要混合搭配...那是您的问题。您正在使用 Excel2007 Writer 创建一个 .xlsx (OfficeOpenXML) 文件,但设置 header 以告知浏览器需要一个 .xls (BIFF) 文件
推荐的 .xls (BIFF) 下载 header 是:
// 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
推荐的 .xlsx (OfficeOpenXML) 下载 header 是:
// 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
请注意,Content-Type
、Content-Disposition
可能会被浏览器视为区分大小写,因此 Content-Type
不是与Content-type
....相同,我相信这也可能会给您带来问题
关于PHPExcel 下载无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21428792/