最简单的导出EXCEL

Posted thinkbig

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了最简单的导出EXCEL相关的知识,希望对你有一定的参考价值。

	public function put_csv($list,$title){
		$file_name="CSV".date("mdHis",time()).".csv";
		header ( ‘Content-Type: application/vnd.ms-excel‘ );
		header ( ‘Content-Disposition: attachment;filename=‘.$file_name );
		header ( ‘Cache-Control: max-age=0‘ );
		$file = fopen(‘php://output‘,"a");
		$limit=1000;
		$calc=0;
		foreach ($title as $v){
			$tit[]=iconv(‘UTF-8‘, ‘GB2312//IGNORE‘,$v);
		}
		fputcsv($file,$tit);
		foreach ($list as $v){
			$calc++;
			if($limit==$calc){
				ob_flush();
				flush();
				$calc=0;
			}
			foreach ($v as $t){
				$tarr[]=iconv(‘UTF-8‘, ‘GB2312//IGNORE‘,$t);
			}
			fputcsv($file,$tarr);
			unset($tarr);
		}
		unset($list);
		fclose($file);
		exit();
	}
	
	
		
	public function excel(){
		$wherestr=" id > 1000";
		$list1 = M(‘user‘)->where($wherestr)->field("*")->order(‘id desc‘)->limit(0,1000)->select();
		$list=null;
		foreach($list1 as $vo){
			$dd["id"]=$vo["id"];     
			$dd["nickname"]=$vo["nickname"];  
			$dd["signid"]=$vo["signid"];  
			$dd["snumber"]=$vo["snumber"];  
			$dd["inno"]=$vo["inno"];  
			$dd["chinesezodiac"]=$vo["chinesezodiac"];  
			$dd["mobile"]=$vo["mobile"];  
			$dd["firstname"]=$vo["firstname"];  
			$dd["lastname"]=$vo["lastname"];  
			$dd["cname"]=$vo["cname"];  
			$dd["email"]=$vo["email"];  
			if($vo[‘matchtp‘]==1){
				$dd["matchtp"]="Random"; 
			}else{
				$dd["matchtp"]="Intentional"; 
			}
			$dd["matchid"]=$vo["matchid"]; 
			
			if(!empty($vo["matchid"])) {
				$dd["matchid_name"]=getoneid($vo["matchid"],"cname","user"); 
			}else{
				$dd["matchid_name"]=""; 
			}
			if(getcount(" userid=".$vo["id"]." or userid2=".$vo["id"]." ","id","checkinsign")>0){
				$dd["matchid_status"]=‘匹配成功‘; 
			}else{
				$dd["matchid_status"]=‘匹配失败‘; 
			}

			$dd["gender"]=$vo["gender"];  
			if($vo[‘vip‘]==1){
				$dd["vip"]="是"; 
			}else{
				$dd["vip"]="否"; 
			}
			switch(intval($vo["stay"])){
				case 1:
					$dd["stay"]="上海"; 
					break;
				case 2:	
					$dd["stay"]="厦门"; 
					break;
				case 3:	
					$dd["stay"]="上海 & 厦门"; 
					break;    
				default:		
					$dd["stay"]="未选择";  
					break;		
			}
			$dd["game1"]=$vo["game1"];  
			$dd["game2"]=$vo["game2"];  
			$dd["game3"]=$vo["game3"];  
			$dd["passport"]="‘".$vo[‘passport‘];
			$dd["groupid"]="‘".$vo[‘groupid‘];
			$list[]=$dd;				
		}				
		$csv_title=array(‘编号‘,‘nickname‘,‘signid‘,‘No.‘,‘chinesezodiac‘,‘生肖‘,‘mobile‘,‘firstname‘,‘lastname‘,‘中文名‘,‘email‘,‘匹配‘,‘匹配id‘,‘用户中文名‘,‘匹配结果‘,‘性别‘,‘是否vip‘,‘住宿‘,‘game1‘,‘game2‘,‘game3‘,‘passport‘,‘分组‘);
    	$this->put_csv($list,$csv_title);
	}

  

以上是关于最简单的导出EXCEL的主要内容,如果未能解决你的问题,请参考以下文章

php导出excel最简单的办法,无需phpexcel

使用While循环导出Excel

JXLS 2.4.0系列教程——最简单的模板导出

php把数据表导出为Excel表的最简单最快的方法(不用插件)

偷懒小工具 - Excel导出公共类

导出到Excel并且取消默认的科学计算法