这里的导出csv格式文件和导出excel是相似的,大家可以参考一下代码,实现导出功能。
/**
* csv导出
*/
public function exportVoter(){
header( "Cache-Control: public" );
header( "Pragma: public" );
header("Content-type:application/vnd.ms-excel");
header("Content-Disposition:attachment;filename=投票人信息.csv");
header('Content-Type:APPLICATION/OCTET-STREAM');
ob_start();
$header_str = iconv("utf-8",'gbk',"姓名,性别,电话,密码\n");
$voter = new \Admin\Model\VotersModel ();
//$data = I ( 'get.' );print_r($data);exit();
if (I ( 'get.name' )!="") {
$name = I ( 'get.name' );
} else {
$name = "";
}
$where=($name==''?'':" and v.name like '%{$name}%'");
$list=$voter->query("select v.*,d.deptName from voters v inner join wkrj_auth_dept
d on v.deptid=d.id".$where);
$file_str='';
if($list){
foreach ($list as $row){
$file_str.= $row['name'].','.$row['sex'].','.$row['phone'].','.$row['password']."\n";
}
}else{
echo "导出失败!";
}
//exit($file_str);
//iconv转码函数
$file_str= iconv("utf-8",'gbk',$file_str);
ob_end_clean();
echo $header_str;
echo $file_str;
}