csv出力したいときの3つのコツ

このエントリーをはてなブックマークに追加
はてなブックマーク - csv出力したいときの3つのコツ
Share on Facebook
Post to Google Buzz
Bookmark this on Yahoo Bookmark
Bookmark this on Livedoor Clip
Share on FriendFeed

1、出力前にheader出力

header(“Accept-Ranges: none”);
header(“Content-Disposition: inline; filename=$filename”);
header(“Content-Transfer-Encoding: binary”);
header(“Content-Length: “. strlen($contents) );
header(“Content-Type: text/octet-stream”);

$filenameは、DLするファイル名

$filename = ***.csv

2、データは全てSJISにエンコード変換

.csvのファイルを開くと大概Excelで開こうとするので、SJISに変換しておく。
ソースをSJIS以外で書いているときは特に、
ソース内で挿入するデータやデータ連結に使う文字列「:」(全角コロン)なんかも
忘れずにエンコードしておかないと、文字化けする。

3、データは配列で

php側では2次元配列に整形しておく

$data_cnt = count($data);
for($i=0; $i< $data_cnt; $i++){
$csv = implode(","$data[$i]);
echo $csv;
}

コレでOK

This entry was posted in php. Bookmark the permalink.