• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

php原生导出excel文件的两种方法详解

php 搞代码 4年前 (2022-01-22) 20次浏览 已收录 0个评论

本篇文章主要介绍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: at<em>8本文来源gao.dai.ma.com搞@代*码(网$</em><pre>搞代gaodaima码

tachment;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>";  }

搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:php原生导出excel文件的两种方法详解
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址