Home >Backend Development >PHP Tutorial >ThinkPHP exports csv format documents

ThinkPHP exports csv format documents

WBOY
WBOYOriginal
2016-07-29 09:06:301481browse

The export of csv format files here is similar to the export of excel. You can refer to the code to implement the export function.

/**
	 * 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;
	}

The above introduces ThinkPHP to export csv format documents, including the content. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn