我想創建一個頁面,允許用戶將一個SQL表格的內容下載到一個excel文件中。PHPExcel下載不能正常工作
問題:當我打開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();
}
哇謝謝你這個出色的作品 –
哦ye神,接受的答案是創建HTML標記,假裝這是一個Excel文件....什麼世界來? –
它的確行得通......其他解決方案都不起作用:| –