php原生導(dǎo)出excel文件的兩種方法(推薦)
第一種方法:
$filename='文件名稱'; $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ù)過多造成問題 ob_flush(); flush(); $cnt = 0; } echo trim($res['id']). "\t"; echo "\n"; } }
第二種方法:
$filename='文件名稱'; $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>"; }
以上這篇php原生導(dǎo)出excel文件的兩種方法(推薦)就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- 利用phpExcel實(shí)現(xiàn)Excel數(shù)據(jù)的導(dǎo)入導(dǎo)出(全步驟詳細(xì)解析)
- php導(dǎo)入導(dǎo)出excel實(shí)例
- php中導(dǎo)出數(shù)據(jù)到excel時(shí)數(shù)字變?yōu)榭茖W(xué)計(jì)數(shù)的解決方法
- php將數(shù)據(jù)庫導(dǎo)出成excel的方法
- 使用PHPExcel實(shí)現(xiàn)數(shù)據(jù)批量導(dǎo)出為excel表格的方法(必看)
- php把數(shù)據(jù)表導(dǎo)出為Excel表的最簡單、最快的方法(不用插件)
- PHP實(shí)現(xiàn)導(dǎo)出帶樣式的Excel
- php導(dǎo)出excel格式數(shù)據(jù)問題
- php導(dǎo)出word文檔與excel電子表格的簡單示例代碼
- PHP將Excel導(dǎo)入數(shù)據(jù)庫及數(shù)據(jù)庫數(shù)據(jù)導(dǎo)出至Excel的方法
- php中通用的excel導(dǎo)出方法實(shí)例
- php 自定義函數(shù)實(shí)現(xiàn)將數(shù)據(jù) 以excel 表格形式導(dǎo)出示例
相關(guān)文章
Yii框架 session 數(shù)據(jù)庫存儲(chǔ)操作方法示例
這篇文章主要介紹了Yii框架 session 數(shù)據(jù)庫存儲(chǔ)操作方法,結(jié)合實(shí)例形式分析了使用Yii框架session組件配置與數(shù)據(jù)庫存儲(chǔ)相關(guān)操作技巧,需要的朋友可以參考下2019-11-11PHP如何通過AJAX方式實(shí)現(xiàn)登錄功能
PHP是一門很好的語言,可以很方便的開發(fā)web應(yīng)用程序,下面介紹一下Ajax+PHP+MySQL登陸示例,感興趣的小伙伴們可以參考一下2015-11-11Yii框架引用插件和ckeditor中body與P標(biāo)簽去除的方法
這篇文章主要介紹了Yii框架引用插件和ckeditor中body與P標(biāo)簽去除的方法,結(jié)合實(shí)例形式分析了Yii框架中引入插件的步驟、實(shí)現(xiàn)方法與相關(guān)注意事項(xiàng),需要的朋友可以參考下2017-01-01CentOS下PHP7的編譯安裝及MySQL的支持和一些常見問題的解決辦法
這篇文章主要介紹了CentOS下PHP7的編譯安裝及MySQL的支持和一些問題的解決 的相關(guān)資料,需要的朋友可以參考下2015-12-12Ajax PHP JavaScript MySQL實(shí)現(xiàn)簡易無刷新在線聊天室
這篇文章主要為大家詳細(xì)介紹了Ajax PHP JavaScript MySQL實(shí)現(xiàn)簡易無刷新在線聊天室,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-08-08ThinkPHP整合datatables實(shí)現(xiàn)服務(wù)端分頁的示例代碼
下面小編就為大家分享一篇ThinkPHP整合datatables實(shí)現(xiàn)服務(wù)端分頁的示例代碼,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-02-02