$url = 'http://bs.baidu.com/wenku4/%2Fe43e6732eba84a316af36c5c67a7c6d6?sign=MBOT:y1jXjmMD4FchJHFHIGN4z:lfZAx1Nrf44aCyD6tJqJ2FhosLY%3D&time=1392893977&response-content-disposition=attachment;%20filename=%22php%BA%AF%CA%FD.xls%22&response-content-type=application%2foctet-stream';
//an example xls file form baidu wenku
$url = 'http://bs.baidu.com/wenku4/%2Fe43e6732eba84a316af36c5c67a7c6d6?sign=MBOT:y1jXjmMD4FchJHFHIGN4z:lfZAx1Nrf44aCyD6tJqJ2FhosLY%3D&time=1392893977&response-content-disposition=attachment;%20filename=%22php%BA%AF%CA%FD.xls%22&response-content-type=application%2foctet-stream';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
file_put_contents('./test.xls', curl_exec($ch));
curl_close($ch);
exec("libreoffice ./test.xls", $out, $status);
?>
第1,2种方案存在一个问题,就是在写入本地磁盘之前,文件会被读入内存中,那么当文件很大的时候,可能会超出内存而崩溃
即使你的内存设置的足够的大,那这也是不别要的开销
解决方法是:直接给CURL一个可写的文件流来让它自己来解决这个问题(通过 CURLOPT_FILE选项),这样就要先创建一个文件指针给它。
//an example xls file form baidu wenku
$url = 'http://bs.baidu.com/wenku4/%2Fe43e6732eba84a316af36c5c67a7c6d6?sign=MBOT:y1jXjmMD4FchJHFHIGN4z:lfZAx1Nrf44aCyD6tJqJ2FhosLY%3D&time=1392893977&response-content-disposition=attachment;%20filename=%22php%BA%AF%CA%FD.xls%22&response-content-type=application%2foctet-stream';
$fp_output = fopen('./test.xls', 'w');
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_FILE, $fp_output);
curl_exec($ch);
curl_close($ch);
exec("libreoffice ./test.xls", $out, $status);
?>
http://www.bkjia.com/PHPjc/735879.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/735879.htmlTechArticle今天在做导出Excel的时候,总是要测试导出的Excel文件,频繁的下载和打开,很麻烦 就想着写段代码一气呵成 服务端导出Excel==下载Excel文件...