下面小编就为大家带来一篇php原生导出excel文件的两种方法(推荐)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
第一种方法:
","","
","","
");
$p_change_line_in_excel_cell = '';
foreach($head as $v){
echo iconv('utf-8','gb2312',$v) . "\t";
}
echo "\n";
// 计数器
$cnt = 0;
// 每隔$limit行,刷新一下<a href="https://www.jb51.cc/tag/shuchu/" target="_blank" class="keywords">输出</a>buffer,节约资源
$limit = 100000;
// 逐行取出数据,节约内存
while ($res = <a href="https://www.jb51.cc/tag/MysqL/" target="_blank" class="keywords">MysqL</a>_fetch_assoc($query)) {
$cnt ++;
if ($limit == $cnt) { //刷新一下<a href="https://www.jb51.cc/tag/shuchu/" target="_blank" class="keywords">输出</a>buffer,防止由于数据过多造成问题
ob_flush();
flush();
$cnt = 0;
}
echo trim($res['id']). "\t";
echo "\n";
}
}
第二种方法:
';
$where = "1=1";
$<a href="https://www.jb51.cc/tag/sql/" target="_blank" class="keywords">sql</a> = " ";
<a href="https://www.jb51.cc/tag/MysqL/" target="_blank" class="keywords">MysqL</a>_query('set names "utf8"');
<a href="https://www.jb51.cc/tag/MysqL/" target="_blank" class="keywords">MysqL</a>_set_charset('utf8');
$query = DB::Query($<a href="https://www.jb51.cc/tag/sql/" target="_blank" class="keywords">sql</a>);
// <a href="https://www.jb51.cc/tag/PHP/" target="_blank" class="keywords">PHP</a><a href="https://www.jb51.cc/tag/wenjian/" target="_blank" class="keywords">文件</a>句柄,<a href="https://www.jb51.cc/tag/PHP/" target="_blank" class="keywords">PHP</a>://output 表示直接<a href="https://www.jb51.cc/tag/shuchu/" target="_blank" class="keywords">输出</a>到浏览器
$fp = fopen('<a href="https://www.jb51.cc/tag/PHP/" target="_blank" class="keywords">PHP</a>://output','a');
// <a href="https://www.jb51.cc/tag/shuchu/" target="_blank" class="keywords">输出</a>Excel列头信息
$head = array('ID','xxx');
//字符替换
$p_new_lines = array("\r\n","<br/>");
$p_change_line_in_excel_cell = '';
echo "<table>";
echo "<tr>";
foreach($head as $v){
echo "<td>".iconv('utf-8',$v)."</td>";
}
echo "</tr>";
// 逐行取出数据,节约内存
while ($res = <a href="https://www.jb51.cc/tag/MysqL/" target="_blank" class="keywords">MysqL</a>_fetch_assoc($query)) {
echo "<tr>";
echo "<td style='vnd.ms-excel.numberformat:@'>".$res['id']."</td>";
echo "<td>".iconv('utf-8',$res['xxx']."</td>";
echo"</tr>";
}
echo "</table>";
}
以上这篇PHP原生导出excel文件的两种方法(推荐)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持编程之家。