PHP导出excel的方法总结

PHP导出excel的方法总结

1.两个步骤定义导出方法和内容

public function exportExcel($expTitle,$expCellName,$expTableData,$starttime,$endtime){
		// $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
        $xlsTitle = iconv('utf-8', 'gb2312', $expTitle);//文件名称
        $fileName = '收银汇总查询'.date('_YmdHis');//or $xlsTitle 文件名称可根据自己情况设定$_SESSION['username']
        $cellNum = count($expCellName);//计算cell数量
        $dataNum = count($expTableData);
        vendor("PHPExcel.PHPExcel");
       
        $objPHPExcel = new \PHPExcel();
        $cellName = array('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','AA','AB','AC','AD','AE','AF','AG','AH','AI','AJ','AK','AL','AM','AN','AO','AP','AQ','AR','AS','AT','AU','AV','AW','AX','AY','AZ');
        
        $objPHPExcel->getActiveSheet(0)->mergeCells('A1:'.$cellName[$cellNum-1].'1');//合并单元格
        $objPHPExcel->setActiveSheetIndex(0)->setCellValue('A1', '表'.$expTitle.'  导出时间:'.date('Y-m-d H:i:s').' 起止时间'.date('Y-m-d H:i:sa',$starttime).'至'.date('Y-m-d H:i:sa',$endtime));

        for($i=0;$i<$cellNum;$i++){
            $objPHPExcel->setActiveSheetIndex(0)->setCellValue($cellName[$i].'2',$expCellName[$i][1]); 
        } 
          // Miscellaneous glyphs, UTF-8   
        for($i=0;$i<$dataNum;$i++){
          for($j=0;$j<$cellNum;$j++){
            $objPHPExcel->getActiveSheet(0)->setCellValue($cellName[$j].($i+3),$expTableData[$i][$expCellName[$j][0]].' ');//如果导出订单号、身份证号等,可能会因为数字太长,导出的excel会显示科学技术方式显示,可以加空格' '来避免
          }             
        }  
        
        header('pragma:public');
        header('Content-type:application/vnd.ms-excel;charset=utf-8;name="'.$xlsTitle.'.xls"');
        header("Content-Disposition:attachment;filename=$fileName.xls");//attachment新窗口打印inline本窗口打印
        $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');  
        $objWriter->save('php://output'); 
        exit;   
    }

定义传参调用方法


    public function excel(){

    		$starttimestamp = strtotime($_POST['starttimestamp']);//字符串转时间戳
			$endtimestamp = strtotime($_POST['endtimestamp']);
			//我们必须先确定收银的角色然后,用收银员的角色查询当日值班人员的所有人员,然后通过人员分别查询个人的收银数据	
			$finance = M('Role')->where("roleuname = 'Finance'")->find(); 
			$where['roleid'] = $finance['id'];
			$adminusers = M('Adminuser')->where($where)->select();//二位数组获取角色id,uname,name
			$arrview = array();
			$amountcount;
			if($adminusers){
				for ($i=0; $i < count($adminusers); $i++) { 
					$where['send_account_name'] = $adminusers[$i]['name'];
					$where['send_account_id'] = $adminusers[$i]['id'];
					$res = M('order')->where("send_time>$starttimestamp AND send_time<$endtimestamp")->where($where)->field('amount')->select();
					for ($y=0; $y < count($res); $y++) {
							$sum = $sum+$res[$y]['amount'];	
						}//计算合计
						$arritem['rownum'] = $i;
						$arritem['financetype'] = "微信";
						$arritem['send_account_name']=$adminusers[$i]['name'];//收银账号
						$arritem['financeuname'] =$adminusers[$i]['uname'];//收银员姓名
						$arritem['eqrmb'] =number_format($sum,2,".","");//收银人员合计金额
						$arritem['zerormb'] =number_format($sum,2,".","");//折换RMB
						$arritem['noterror'] =number_format("0",2,".","");//RMB找零
						$arritem['actualacmount'] =number_format("0",2,".","");//误差
						$arritem['denomination'] =number_format($sum,2,".","");//实收虚收
						$arritem['actualreceipt'] =number_format("0",2,".","");//面额差
						$arritem['actualrmb'] =number_format($sum,2,".","");//实收金额
						$arritem['virtualreceipt'] =number_format("0",2,".","");//虚收金额

						$arrview[$i] = $arritem;
						$amountcount = $amountcount+$sum;
				}
				
			}
			//追加合计数组
			$amountcounted = number_format($amountcount,'2','.','');
        	$zerormb = number_format('0','2','.','');
        	$pusharray['rownum'] = '合计';
			$pusharray['financetype'] = "";
			$pusharray['send_account_name']="";//收银账号
			$pusharray['financeuname'] ="";//收银员姓名
			$pusharray['eqrmb'] =$amountcounted;//收银人员合计金额
			$pusharray['zerormb'] =$amountcounted;//折换RMB
			$pusharray['noterror'] =$zerormb;//RMB找零
			$pusharray['actualacmount'] =$zerormb;//误差
			$pusharray['denomination'] =$amountcounted;//实收虚收
			$pusharray['actualreceipt'] =$zerormb;//面额差
			$pusharray['actualrmb'] =$amountcounted;//实收金额
			$pusharray['virtualreceipt'] =$zerormb;//虚收金额
			// dump($pusharray);
        	$arrview[count($arrview)]=$pusharray;
			// $this->assign('restarttimestamp',$_POST['starttimestamp']);
			// $this->assign('reendtimestamp',$_POST['endtimestamp']);
        //$months = I('get.months');
        //$month_start = strtotime($months);//获取指定月份月初时间戳  
        //$month_end   = mktime(23, 59, 59, date('m', strtotime($months))+1, 00);//指定月份月末时间戳

        //$where['type'] = 2;//群主充值
        //$where['time'] = array(array('gt',$month_start),array('lt',$month_end));
        //$m = M('dap_stream');
        // $list = $m->where($where)->field('id,stream_id,time,player_id,stream_money')->select();
        $xlsName  = "Order";
        $xlsCell  = array(
        array('rownum','行号'),
        array('financetype','收款方式'),
        array('send_account_name','收银账号'),
        array('financeuname','收银员'),
        array('eqrmb','收银总额'),
        array('zerormb','折换RMB'),
        array('noterror','RMB找零'),
        array('actualacmount','误差'), 
        array('denomination','实收虚收'),
        array('actualreceipt','面额差'),
        array('actualrmb','实收金额'),
        array('virtualreceipt',"虚收金额"),
        );
        // $xlsData  = $m->where($where)->field("id,stream_id, FROM_UNIXTIME(time, '%Y-%m-%d %H:%i:%S') time,player_id,stream_money")->select();
        // $xlsData = array();   
        //这里需要计算类型 
        $xlsData = $arrview;
            // dump($xlsData);exit();
        $this->exportExcel($xlsName,$xlsCell,$xlsData,$starttimestamp,$endtimestamp);
    }

猜你喜欢

转载自blog.csdn.net/qq_27905477/article/details/86476731
0条评论
添加一条新回复