首頁 >後端開發 >php教程 >php原生匯出excel檔案的兩種方法詳解

php原生匯出excel檔案的兩種方法詳解

墨辰丷
墨辰丷原創
2018-05-30 16:21:452284瀏覽

這篇文章主要介紹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 class="brush:php;toolbar:false">","
","
","
","
"); $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=&#39;文件名称&#39;;
  $filetitle=&#39;你的标题&#39;;
  if($_POST){
    $title = &#39;&#39;;
    ini_set(&#39;memory_limit&#39;,&#39;300M&#39;);
    header(&#39;Content-Type: application/vnd.ms-excel;charset=utf-8&#39;);
    $name = $title.".xls";
    header(&#39;Content-Disposition: attachment;filename=&#39;.$name.&#39;&#39;);
    header(&#39;Cache-Control: max-age=0&#39;);
    echo &#39;<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>&#39;;
    $where = "1=1";

    $sql = " ";
    mysql_query(&#39;set names "utf8"&#39;);
    mysql_set_charset(&#39;utf8&#39;);
    $query = DB::Query($sql);

    // PHP文件句柄,php://output 表示直接输出到浏览器 
    $fp = fopen(&#39;php://output&#39;, &#39;a&#39;);
    // 输出Excel列头信息 
    $head = array(&#39;ID&#39;,&#39;xxx&#39;);
    //字符替换
    $p_new_lines = array("\r\n", "\n","\t","\r","\r\n", "<pre class="brush:php;toolbar:false">","
","
","
","
"); $p_change_line_in_excel_cell = ''; echo ""; echo ""; foreach($head as $v){ echo ""; } echo ""; // 逐行取出数据,节约内存 while ($res = mysql_fetch_assoc($query)) { echo ""; echo ""; echo ""; echo""; } echo "
".iconv('utf-8','gb2312',$v)."
".$res['id']."".iconv('utf-8', 'gb2312', $res['xxx']."
"; }

以上就是本文的全部內容,希望對大家的學習有幫助。


相關推薦:

php實作刪除陣列指定元素的方法

#php中的例外與錯誤解析

#php陣列依拼音順序排序的方法詳解

#

以上是php原生匯出excel檔案的兩種方法詳解的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn