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

來源:互聯網
上載者:User
本篇文章主要介紹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>";  }

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


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.