// 假設得到的數據格式如下
$result = array(
array(
"orderid" = "1110111",
"shopid" = "202302323",
),
array(
"orderid" = "1110111",
"shopid" = "202302323",
)
);
$string = "訂單ID,店鋪ID\n";
foreach($result as $key => $value) {
$string .= $value['orderid'] . ',' . "\t" . $value['shopid'] . "\t\n";
}
說明:
- 字段值之間用英文 ","隔開;
- 遇到數字字符串時結尾加 "\t",否則長度超過12位會被轉為科學計數法形式;
$string =iconv('utf-8', 'gb2312', $string);
最好將編碼轉一下,否則execl 下中文亂碼
$filename = date('Y-m-d').'.csv';
header("Content-type:text/csv");
header("Content-Disposition:attachment;filename=".$filename);
header('Cache-Control:must-revalidate,post-check=0,pre-check=0');
header('Expires:0');
header('Pragma:public');
echo $string;