public function putCsv($csvFileName, $dataArr ,$haderText = ''){
$handle = fopen($csvFileName,"w");//写方式打开
if(!$handle){
return '文件打开失败';
}
$csv_data = '';
//判断是否定义头标题
if(!empty($haderText)){
foreach ($haderText as $key => $value) {
$haderText[$key] = iconv("utf-8","gbk//IGNORE",$value);//对中文编码进行处理
$csv_data .= $haderText[$key].',';
}
$csv_data .= PHP_EOL;
}
foreach ($dataArr as $key => $value) {
foreach ($value as $k => $v) {
$value[$k] = iconv("utf-8","gbk//IGNORE",$v);//对中文编码进行处理
$csv_data .= $value[$k].',';
}
$csv_data .= PHP_EOL;
}
fclose($handle);
header("Content-type:text/csv;");
header("Content-Disposition:attachment;filename=" . $csvFileName);
header('Cache-Control:must-revalidate,post-check=0,pre-check=0');
header('Expires:0');
header('Pragma:public');
echo $csv_data;
}
public function actionExport()
{
//导出表格
$head = array('编号','姓名','年龄','出生年月');
$data = array(
array('001','zs',10,'1991-1-1'),
array('002','李四',10,'1991-1-1'),
array('003','王五',10,'1991-1-1'),
);
$this->putCsv('putCsv.csv', $data, $head);
}