php原生导出excel文件的两种方法(推荐)

前端之家收集整理的这篇文章主要介绍了php原生导出excel文件的两种方法(推荐)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

第一种方法

PHP;"> $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("\r\n","\n","\t","\r","\r\n","
","
","
","
","
"); $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";
}

}

第二种方法

PHP;"> $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 ' <Meta http-equiv="expires" content="Mon,06 Jan 1999 00:00:01 GMT"> <Meta http-equiv=Content-Type content="text/html; charset=gb2312"> '; $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&gt;";
echo "<tr&gt;";
foreach($head as $v){
  echo "<td&gt;".iconv('utf-8',$v)."</td&gt;";
}
echo "</tr&gt;";
// 逐行取出数据,节约内存
while ($res = <a href="https://www.jb51.cc/tag/MysqL/" target="_blank" class="keywords">MysqL</a>_fetch_assoc($query)) {
  echo "<tr&gt;";
  echo "<td style='vnd.ms-excel.numberformat:@'>".$res['id']."</td&gt;";
  echo "<td&gt;".iconv('utf-8',$res['xxx']."</td&gt;";
  echo"</tr&gt;";
}
echo "</table&gt;";

}

以上这篇PHP原生导出excel文件的两种方法(推荐)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持编程之家。

原文链接:https://www.f2er.com/php/18470.html

猜你在找的PHP相关文章