csv 导出CSV

PHP导入导出CSV

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
function  ($data) {
$filename = date('YmdHis').".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');
if (is_string($data)) {
echo i($data);
} elseif (is_array($data)){
echo array2string($data);
} else {
echo '不支持的数据类型'
}
}

function array2string ($data) {
$csv ='';
$size = count($data);
for($i = 0 ; $i < $size ; $i++) {
$lineCount = count($data[$i]);
foreach ($data[$i] as )
$csv .= i($result[$i][0]).','.i($result[$i][1])."n";
}
foreach ($data as $lineData) {
foreach ($lineData as $line) {
$csv .= i($line).',';
}
$csv = rtrim($csv, ',') . "n"; // 双引号的n
}
return $csv;
}

function i($strInput) {
return iconv('utf-8','GB2312//IGNORE',$strInput);//页面编码为utf-8时使用,否则导出的中文为乱码
}