您好,登錄后才能下訂單哦!
這篇文章給大家介紹使用php對(duì)excel文件進(jìn)行導(dǎo)出的方法有哪些,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對(duì)大家能有所幫助。
第一種方法:
$filename='文件名稱(chēng)'; $filetitle='你的標(biāo)題'; if($_POST){ set_time_limit(10000); $title = ''; ini_set('memory_limit','300M'); header('Content-Type: application/vnd.ms-excel;charset=utf-8'); $name = $title.".xls"; header('Content-Disposition: attachment;filename='.$name.''); header('Cache-Control: max-age=0'); $where = "1=1"; $sql = ""; $query = DB::Query($sql); // PHP文件句柄,php://output 表示直接輸出到瀏覽器 $fp = fopen('php://output', 'a'); // 輸出Excel列頭信息 $head = array('ID'); //字符替換 $p_new_lines = array("\r\n", "\n","\t","\r","\r\n", "<pre>","</pre>","<br>","</br>","<br/>"); $p_change_line_in_excel_cell = ''; foreach($head as $v){ echo iconv('utf-8','gb2312',$v) . "\t"; } echo "\n"; // 計(jì)數(shù)器 $cnt = 0; // 每隔$limit行,刷新一下輸出buffer,節(jié)約資源 $limit = 100000; // 逐行取出數(shù)據(jù),節(jié)約內(nèi)存 while ($res = mysql_fetch_assoc($query)) { $cnt ++; if ($limit == $cnt) { //刷新一下輸出buffer,防止由于數(shù)據(jù)過(guò)多造成問(wèn)題 ob_flush(); flush(); $cnt = 0; } echo trim($res['id']). "\t"; echo "\n"; } }
第二種方法:
$filename='文件名稱(chēng)'; $filetitle='你的標(biāo)題'; if($_POST){ $title = ''; ini_set('memory_limit','300M'); header('Content-Type: application/vnd.ms-excel;charset=utf-8'); $name = $title.".xls"; header('Content-Disposition: attachment;filename='.$name.''); header('Cache-Control: max-age=0'); echo '<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40"> <head> <meta http-equiv="expires" content="Mon, 06 Jan 1999 00:00:01 GMT"> <meta http-equiv=Content-Type content="text/html; charset=gb2312"> <!--[if gte mso 9]><xml> <x:ExcelWorkbook> <x:ExcelWorksheets> <x:ExcelWorksheet> <x:Name></x:Name> <x:WorksheetOptions> <x:DisplayGridlines/> </x:WorksheetOptions> </x:ExcelWorksheet> </x:ExcelWorksheets> </x:ExcelWorkbook> </xml><![endif]--> </head>'; $where = "1=1"; $sql = " "; mysql_query('set names "utf8"'); mysql_set_charset('utf8'); $query = DB::Query($sql); // PHP文件句柄,php://output 表示直接輸出到瀏覽器 $fp = fopen('php://output', 'a'); // 輸出Excel列頭信息 $head = array('ID','xxx'); //字符替換 $p_new_lines = array("\r\n", "\n","\t","\r","\r\n", "<pre>","</pre>","<br>","</br>","<br/>"); $p_change_line_in_excel_cell = ''; echo "<table>"; echo "<tr>"; foreach($head as $v){ echo "<td>".iconv('utf-8','gb2312',$v)."</td>"; } echo "</tr>"; // 逐行取出數(shù)據(jù),節(jié)約內(nèi)存 while ($res = mysql_fetch_assoc($query)) { echo "<tr>"; echo "<td style='vnd.ms-excel.numberformat:@'>".$res['id']."</td>"; echo "<td>".iconv('utf-8', 'gb2312', $res['xxx']."</td>"; echo"</tr>"; } echo "</table>"; }
關(guān)于使用php對(duì)excel文件進(jìn)行導(dǎo)出的方法有哪些就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。