php原生導(dǎo)出excel文件的兩種方法(推薦)
更新時(shí)間:2016年11月19日 08:49:11 投稿:jingxian
下面小編就為大家?guī)?lái)一篇php原生導(dǎo)出excel文件的兩種方法(推薦)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
第一種方法:
$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ù)過(guò)多造成問(wèn)題
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文件的兩種方法(推薦)就是小編分享給大家的全部?jī)?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ù)庫(kù)導(dǎo)出成excel的方法
- 使用PHPExcel實(shí)現(xiàn)數(shù)據(jù)批量導(dǎo)出為excel表格的方法(必看)
- php把數(shù)據(jù)表導(dǎo)出為Excel表的最簡(jiǎn)單、最快的方法(不用插件)
- PHP實(shí)現(xiàn)導(dǎo)出帶樣式的Excel
- php導(dǎo)出excel格式數(shù)據(jù)問(wèn)題
- php導(dǎo)出word文檔與excel電子表格的簡(jiǎn)單示例代碼
- PHP將Excel導(dǎo)入數(shù)據(jù)庫(kù)及數(shù)據(jù)庫(kù)數(shù)據(jù)導(dǎo)出至Excel的方法
- php中通用的excel導(dǎo)出方法實(shí)例
- php 自定義函數(shù)實(shí)現(xiàn)將數(shù)據(jù) 以excel 表格形式導(dǎo)出示例
相關(guān)文章
bindParam和bindValue的區(qū)別以及在Yii2中的使用詳解
下面小編就為大家分享一篇bindParam和bindValue的區(qū)別以及在Yii2中的使用詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-03-03
用php制作簡(jiǎn)單分頁(yè)(從數(shù)據(jù)庫(kù)讀取記錄)的方法詳解
本篇文章介紹了,使用php制作簡(jiǎn)單分頁(yè)(從數(shù)據(jù)庫(kù)讀取記錄)的方法詳解。需要的朋友參考下2013-05-05
yii2 commands模式以及配置crontab定時(shí)任務(wù)的方法
下面小編就為大家?guī)?lái)一篇yii2 commands模式以及配置crontab定時(shí)任務(wù)的方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-08-08
php截取字符串函數(shù)substr,iconv_substr,mb_substr示例以及優(yōu)劣分析
php進(jìn)行中文字符串的截取時(shí),會(huì)經(jīng)常用到二個(gè)函數(shù)iconv_substr和mb_substr,對(duì)這二個(gè)函數(shù)應(yīng)該如何選擇呢?參考下本文介紹的例子就明白了。2014-06-06
PHP中把對(duì)象數(shù)組轉(zhuǎn)換成普通數(shù)組的方法
這篇文章主要介紹了PHP中把對(duì)象數(shù)組轉(zhuǎn)成普通數(shù)組的方法,本文直接給出示例代碼,需要的朋友可以參考下2015-07-07

