导出csv
程序员文章站
2022-06-13 22:59:11
...
导出csv
# TODO 增加内存限制值
ini_set('memory_limit', '2048m');
/**
* 导入CSV
* @param array $aData
* @param array $aTitle
* @param string $sFileName
* @return boolean
*/
/*
#TODO case:
$aData = array(
array('id'=>1,'name'=>'名称1'),
array('id'=>2,'name'=>'名称2'),
array('id'=>3,'name'=>'名称3'),
);
$aTitle = array(
array('id','标记'),
array('name','名称'),
);
exportCSV($aData, $aTitle);
*/
function exportCSV($aData = [], $aTitle = [], $sFileName=false)
{
if (!is_array($aData) || !is_array($aTitle))
return false;
if (empty($aData) || empty($aTitle))
return false;
$sFileName = $sFileName ? mb_convert_encoding($sFileName, "GB2312", "UTF-8, GB2312") . ".csv": date("_YmdHis") . ".csv";
header('Content-Type: text/csv; CHARSET=gb2312');
header('Content-Disposition: attachment; filename=' . $sFileName);
$output = fopen('php://output', 'w');
for ($i=0;$i
for($j=0;$j
$aList[$i][$j] = mb_convert_encoding($aData[$i][$aTitle[$j][0]], "GB2312", "UTF-8, GB2312");
}
}
for ($i=0;$i
$aTitle[$i] = mb_convert_encoding($aTitle[$i][1], "GB2312", "UTF-8, GB2312");
}
fputcsv($output, $aTitle);
foreach ($aList as $key) {
fputcsv($output, $key);
}
return true;
}
AD:真正免费,域名+虚机+企业邮箱=0元
上一篇: jmeter对udp接口压测