搜索
熱搜: 活動 交友 discuz
查看: 18435|回復: 0
打印 上一主題 下一主題

[PHP] [PHP] 匯出處理 - CSV、EXCEL匯出實例教學

[複製鏈接]
跳轉到指定樓層
1#
發表於 2013-9-16 17:28:27 | 只看該作者 回帖獎勵 |正序瀏覽 |閱讀模式
匯出文字檔,在PHP程式碼中加入以下header:
  1. header("Content-type: text/x-csv");
  2. header("Content-Disposition: attachment; filename=$filename");
  3. echo $content;
  4. exit;
複製代碼
$filename = 檔案名稱(副檔名自行填入); $content = 輸出內容



檔案格式介紹:

CSV檔:text/x-csv
sxw檔:application/octet-stream
word檔:application/msword
excel檔:application/vnd.ms-excel



編碼轉換:

(1) iconv  ( "原編碼"  , "新編碼"  , "內容" );
echo iconv( "UTF-8", "Big5" , $content);
(2) mb_convert_encoding( "內容" , "新編碼" , "原編碼");
echo mb_convert_encoding($content , "Big5" , "UTF-8");



實作範例:

CSV
  1. <?php
  2. header("Content-type: text/x-csv");
  3. header("Content-Disposition: attachment; filename='output.csv'");
  4. $content = "Column1,Column2,Column3\nRow1,中文測試";
  5. $content = mb_convert_encoding($content , "Big5" , "UTF-8");
  6. echo $content;
  7. exit;
  8. ?>
複製代碼
EXCEL
  1. <?php
  2. header("Content-type:application/vnd.ms-excel");
  3. header("Content-Disposition:filename=exportFileName.xls");
  4. $content = "Column1\tColumn2\tColumn3\nRow1\t中文測試";
  5. $content = mb_convert_encoding($content , "Big5" , "UTF-8");
  6. echo $content;
  7. exit;
  8. ?>
複製代碼
\n: 共用換行; ,: CSV分隔號; \t: XLS分隔號

[PHP] 匯出處理 – CSV、EXCEL匯出實例教學
您需要登錄後才可以回帖 登錄 | 註冊

本版積分規則

本論壇為非營利之網路平台,所有文章內容均為網友自行發表,不代表論壇立場!若涉及侵權、違法等情事,請告知版主處理。


Page Rank Check

廣告刊登  |   交換連結  |   贊助我們  |   服務條款  |   免責聲明  |   客服中心  |   中央分站

手機版|中央論壇

GMT+8, 2026-5-21 19:22 , Processed in 0.036488 second(s), 18 queries .

Powered by Discuz!

© 2005-2015 Copyrights. Set by YIDAS

快速回復 返回頂部 返回列表