第一种方法:
$filename='文件名称';
$filetitle='你的标题';
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("rn", "n","t","r","rn", "<pre>","</pre>","<br>","</br>","<br/>");
$p_change_line_in_excel_cell = '';
foreach($head as $v){
echo iconv('utf-8','gb2312',$v) . "t";
}
echo "n";
// 计数器
$cnt = 0;
// 每隔$limit行,刷新一下输出buffer,节约资源
$limit = 100000;
// 逐行取出数据,节约内存
while ($res = mysql_fetch_assoc($query)) {
$cnt ++;
if ($limit == $cnt) { //刷新一下输出buffer,防止由于数据过多造成问题
ob_flush();
flush();
$cnt = 0;
}
echo trim($res['id']). "t";
echo "n";
}
}
第二种方法:
$filename='文件名称';
$filetitle='你的标题';
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("rn", "n","t","r","rn", "<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>";
// 逐行取出数据,节约内存
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原生导出excel文件的两种方法(推荐)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持硕编程。
【说明】:本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!