温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

使用php对excel文件进行导出的方法有哪些

发布时间:2020-12-11 16:01:36 来源:亿速云 阅读:142 作者:Leah 栏目:开发技术

这篇文章给大家介绍使用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";
    // 计数器 
    $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("\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文件进行导出的方法有哪些就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI