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

网络编程 2021-07-05 08:23www.168986.cn编程入门
狼蚁网站SEO优化长沙网络推广就为大家带来一篇php原生导出excel文件的两种方法(推荐)。长沙网络推广觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随长沙网络推广过来看看吧

第一种方法

$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", "<pre>","</pre>","<br>","</br>","<br/>");
    $p_change_line_in_excel_cell = '';

    foreach($head as $v){
      echo iconv('utf-8','gb2312',$v) . "\t";
    }
    echo "\n";
    // 计数器 
    $t = 0;
    // 每隔$limit行,刷新一下输出buffer,节约资源 
    $limit = 100000;
    // 逐行取出数据,节约内存
    while ($res = mysql_fetch_assoc($query)) {
      $t ++;
      if ($limit == $t) { //刷新一下输出buffer,防止由于数据过多造成问题 
        ob_flush();
        flush();
        $t = 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-:office:office"
      xmlns:x="urn:schemas-microsoft-:office:excel"
      xmlns="http://.w3./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>";
    // 逐行取出数据,节约内存
    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文件的两种方法(推荐)就是长沙网络推广分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持狼蚁SEO。

Copyright © 2016-2025 www.168986.cn 狼蚁网络 版权所有 Power by