[英]How I can save excel file in php?
當我單擊按鈕時,從數據庫中獲取數據並制作一個excel文件並下載。
我單擊按鈕,然后使用curl來請求url,
<?php
$PlaceIdx = $_REQUEST['Idx'];
$Day = $_REQUEST['Day'];
$url = '#url';
$post_data["Idx"] = $Idx;
$post_data["Day"] = $Day;
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST,1);
curl_setopt($ch,CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: appliaction/json'));
curl_setopt($ch,CURLOPT_POSTFIELDS,json_encode($post_data));
$res = curl_exec($ch);
echo print_r($res,true);
?>
並接收成excel文件,
$filename = "TextExcel".'.xls';
$objPHPExcel->getActiveSheet()->setTitle("Tasks-Overview");
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment; filename="'.$filename.'"');
header('Cache-Control: max-age=0');
header( "Content-type: application/vnd.ms-excel" );
header( "Content-type: application/vnd.ms-excel; charset=utf-8");
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel,"Excel5");
$objWriter->save('php://output');
exit;
並在網絡瀏覽器上發生這樣的錯誤,
ࡱ ; ? ? ???? !“#$%&'() +,- 哦... +' 0P(0 < ?H @5 @5 ? B = ``%r8X''1``Calibri''。``.``.``.``.`` .. ''''``''''?''。`` 8 3f ff ̙f3f 3 fff f333 3 3f33 333 $ ?任務概述 g ?\\ ٳ(Ƙ̅ / nj )tմ l < 0 \\ Xֽ T ? 41012143324323U x 1 ֥ 1 D | } \\\\ٳ|ٳ \\ ٳ (00:00〜24:00) ٳX D Q t 2000-04-03?01088887777 2018-04-06?16:46:38〜16:46:59O q t 2017-10-11?01000000000 ?? + &ffffff。?'ffffff ??( ?)。 ??`` dXX333333 ?? 333333 ?? U}?}?} ??} ??} ??} ?? }?} ?? ????@ @ @ @@??@ @ @>>> d ?d?Bgg ՜。? +, 0 HP?X?`?hp x ????? Feuilles de Calcul工作表根目錄 F5 5 ?摘要信息( F Workbook ˚FDocumentSummaryInformation8˚F
但是,當我將php url放在webbrowser地址上時,它成功保存了excel文件。
我不知道為什么和哪個是問題。
如果您知道,請幫助我。
我也使用PHPExcel類,這是我為下載文件編寫的類,請嘗試
public function download($filename = ''){
if(empty($filename))
$filename = date('d-m-Y h.i.s');
header('Content-type: application/vnd.ms-excel');
header('Content-Disposition: attachment; filename="'.$filename.'".xls');
$objWriter = PHPExcel_IOFactory::createWriter($this->excel,'Excel5');
$objWriter->save('php://output');
}
$this->excel
只是這樣的PHPExcel的新實例
$this->excel = new PHPExcel();
讓我知道是否成功
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.