Fork me on GitHub

php 下载 csv utf8

1
2
3
4
5
6
7
8
9
10
11
header('Expires: 0');
header('Cache-control: private');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Content-Description: File Transfer');
header('Content-Encoding: UTF-8');
header('Content-type: text/csv; charset=UTF-8');
header('Content-Disposition: attachment; filename=download.csv');

$content = implode(PHP_EOL, $content);
echo "\xEF\xBB\xBF"; // UTF-8 BOM
echo $content;

单元格内换行

1
2
3
4
5
6
$cell_content = [
"原因1",
"原因2",
"原因3",
];
$cell_content = '"' . implode("\r\n", $cell_content) . '"';

输出到 csv 文件中为

1
2
3
4
"原因1
原因2
原因3"
注意包含换行符的内容一定要用 "" 包起来,不然就把这行记录打断了

-------------感谢您的阅读 有问题请留言(或mailto:frostbelt@sina.cn)-------------