[英]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.