php导出csv格式的文件
注: 最简单的导出excel的方式
excel编码格式为gb2312,所以导出文件的文件名及导出的内容都必须为gb2312的。
另外需注意导出时各个浏览器的兼容性,ie下可设定导出格式为excel的,其他浏览器设为csv(octet-stream) 的
不多说了,看代码:
1 <?php
2
3 $export_file = iconv('utf-8','gb2312',$t);
4 ob_end_clean();
5 ini_set('zlib.output_compression','Off');
6
7 header('Pragma: public');
8 header("Expires: Sat, 26 Jul 1997 05:00:00 GMT");
9 header('Last-Modified: '.gmdate('D, d M Y H:i:s') . ' GMT');
10 header('Cache-Control: no-store, no-cache, must-revalidate'); // HTTP/1.1
11 header('Cache-Control: pre-check=0, post-check=0, max-age=0'); // HTTP/1.1
12 header ("Pragma: no-cache");
13 header("Expires: 0");
14 header('Content-Transfer-Encoding: none');
15 header('Content-Type: application/vnd.ms-excel;'); // This should work for IE & Opera
16 //header("Content-type: application/x-msexcel"); // This should work for the rest
17 header("Content-type: application/octet-stream"); // This should work for the rest
18 header('Content-Disposition: attachment; filename="'.basename($export_file).'"');
19
20 echo 'a,b\n';
21 echo 'c,'.iconv('utf-8','gb2312','中文').'\n';
22
2
3 $export_file = iconv('utf-8','gb2312',$t);
4 ob_end_clean();
5 ini_set('zlib.output_compression','Off');
6
7 header('Pragma: public');
8 header("Expires: Sat, 26 Jul 1997 05:00:00 GMT");
9 header('Last-Modified: '.gmdate('D, d M Y H:i:s') . ' GMT');
10 header('Cache-Control: no-store, no-cache, must-revalidate'); // HTTP/1.1
11 header('Cache-Control: pre-check=0, post-check=0, max-age=0'); // HTTP/1.1
12 header ("Pragma: no-cache");
13 header("Expires: 0");
14 header('Content-Transfer-Encoding: none');
15 header('Content-Type: application/vnd.ms-excel;'); // This should work for IE & Opera
16 //header("Content-type: application/x-msexcel"); // This should work for the rest
17 header("Content-type: application/octet-stream"); // This should work for the rest
18 header('Content-Disposition: attachment; filename="'.basename($export_file).'"');
19
20 echo 'a,b\n';
21 echo 'c,'.iconv('utf-8','gb2312','中文').'\n';
22