PHP;">
PHP
header('Content-Type: text/plain; charset=utf-8');
//导出CSV表格:数组转CSV
$arr = array (
array('ab','cd'),array('"a,b"','"c,d"'),);
$fp = fopen('file.csv','w');
foreach ($arr as $row) {
//将一行格式化为CSV并写入文件指针
fputcsv($fp,$row);
}
fclose($fp);
unset($arr);
//导入CSV表格:CSV转数组
$fp = fopen('file.csv','r');
while ( ($row = fgetcsv($fp)) !== FALSE ) {
//从文件指针中读入一行并解析CSV
$arr[] = $row;
}
fclose($fp);
var_export($arr);
PHP;">
PHP" target="_blank">下载csv
文件
test.PHP:
<div class="jb51code">
<pre class="brush:PHP;">
<?php
session_start();
$outputArray=$_SESSION['outputArray'];
header('Content-Type: application/csv');
header('Content-Disposition: attachment;filename="sales.csv"');
$output=fopen('php://output','w') or die("can not open");
foreach ($outputArray as $line) {
fputcsv($output,split(',',$line));
}
fclose($output) or die("can not close");
?>
PHP;">
$list = array (
'aaa,"bbb"'
);
$fp = fopen('file.csv','w');
foreach ($list as $line) {
fputcsv($fp,$line));
}
fclose($fp);
?>