# PHP导出SCV文件
原理:跟php操作文本文件一样,采用 ‘‘fputcsv’’命令 ,将内容一行一行写入到scv文件中。
代码实例
1、文件导出方法
/**
* @param $title 文件名称
* @param $biaoti 标题
* @param $conter 内容
*/
public function daoChuScv($title,$biaoti,$conter){
$total = count($conter);
header("Content-type:application/vnd.ms-excel");
header("Content-Disposition:filename=" . iconv("UTF-8", "GB18030", $title) . ".csv");
$fp = fopen('php://output', 'a');
$headName = array_keys($biaoti);
$headValue = array_values($biaoti);
// var_dump($headValue);
fputcsv($fp, $headValue);
$size = 2000;
$groupNum = ceil($total / $size);
for ($i = 0; $i <= $groupNum; $i++) {
$exportDataTemp = array_slice($conter, $size * $i, $size);
foreach ($exportDataTemp as $item) {
$rows = array();
foreach ($headName as $name) {
$rows[] = $item[$name];
}
fputcsv($fp, $rows);
}
ob_flush();
flush();
}
fclose($fp);
}
2、 如何 调用文件导出方法
/**
* 导出表格
*/
public function shujudaochu(){
$title = '积分对账';
$biaoti = ['one'=>'第一','two'=>'第二','three'=>'第三','four'=>'第四'];
$conter = array();
$conter[] = ['one'=>'第一的值','two'=>'第二的值','three'=>'第三的值','four'=>'第四的值'];
self::daoChuScv($title,$biaoti,$conter);
}