|
|
匯出文字檔,在PHP程式碼中加入以下header:- header("Content-type: text/x-csv");
- header("Content-Disposition: attachment; filename=$filename");
- echo $content;
- 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- <?php
- header("Content-type: text/x-csv");
- header("Content-Disposition: attachment; filename='output.csv'");
- $content = "Column1,Column2,Column3\nRow1,中文測試";
- $content = mb_convert_encoding($content , "Big5" , "UTF-8");
- echo $content;
- exit;
- ?>
複製代碼 EXCEL- <?php
- header("Content-type:application/vnd.ms-excel");
- header("Content-Disposition:filename=exportFileName.xls");
- $content = "Column1\tColumn2\tColumn3\nRow1\t中文測試";
- $content = mb_convert_encoding($content , "Big5" , "UTF-8");
- echo $content;
- exit;
- ?>
複製代碼 \n: 共用換行; ,: CSV分隔號; \t: XLS分隔號
[PHP] 匯出處理 – CSV、EXCEL匯出實例教學 |
|