在网上下载PHPExcel资源包,置于vender文件夹之下,也可以点击我的网盘分享:https://pan.baidu.com/s/1v2_oMMSvwTcebG9F3YQJqQ
先说两个注意事项:
1、部分php7环境的朋友可能在导出的时候不能执行:$objWriter->save('php://output') 语句(我就是这种情况),此时将PHPExcel插件中(PHPExcel/PHPExcel/Calculation/Functions.php中576行的break注释掉就是)
2、关于日期的处理,PHPExcel中处理日期的方式如下:
$op_Time = gmdate("Y-m-d", \PHPExcel_Shared_Date::ExcelToPHP($objPHPExcel->getActiveSheet()->getCell("L".$i)->getValue()));
$data['op_Time'] = strtotime($op_Time);
/** 代理商信息导出*/public function downAgent(){//这几个字段是筛选导出if(input('us_truename')){ //姓名$map['us_truename'] = array('like','%'.input('us_truename').'%');}if(input('ag_idcard')){ //身份证号码$map['ag_idcard'] = input('ag_idcard');}if(input('ag_cityid3') != ''){ //代理地区$map['ag_cityid'] = input('ag_cityid3');}elseif(input('ag_cityid2') != ''){$map['ag_cityid'] = input('ag_cityid2');}elseif(input('ag_cityid1') != ''){$map['ag_cityid'] = input('ag_cityid1');}$map['us_role'] = 2; //代理商$map['us_status'] = array('gt','-1');$info = Db::table('tb_users')->field('us_truename,ag_phone_number,ag_idcard,ag_address,ci_name,ag_company_addess,ag_ent_name,ag_offline_num,ag_tax_payment,ag_billing_address,ag_billing_phone,ag_billing_bank,ag_billing_number,ag_info')->join('tb_agent','tb_agent.ag_usid=tb_users.us_id')->join('tb_city','tb_city.ci_id=tb_agent.ag_cityid')->where($map)->select();//导出数据$file_name = date('Y-m-d_His').'.xls';ob_clean();vendor("PHPExcel.PHPExcel"); //引入文件$objPHPExcel = new \PHPExcel();$objPHPExcel->getActiveSheet()->SetCellValue('A1', '姓名');$objPHPExcel->getActiveSheet()->SetCellValue('B1', '电话');$objPHPExcel->getActiveSheet()->SetCellValue('C1', '身份证号码');$objPHPExcel->getActiveSheet()->SetCellValue('D1', '身份证地址');$objPHPExcel->getActiveSheet()->SetCellValue('E1', '代理地区');$objPHPExcel->getActiveSheet()->SetCellValue('F1', '办公地点');$objPHPExcel->getActiveSheet()->SetCellValue('G1', '线下人员人数');$objPHPExcel->getActiveSheet()->SetCellValue('H1', '企业名称');$objPHPExcel->getActiveSheet()->SetCellValue('I1', '纳税人识别号');$objPHPExcel->getActiveSheet()->SetCellValue('J1', '开票地址');$objPHPExcel->getActiveSheet()->SetCellValue('K1', '开票电话');$objPHPExcel->getActiveSheet()->SetCellValue('L1', '开票银行');$objPHPExcel->getActiveSheet()->SetCellValue('M1', '开票账号');$objPHPExcel->getActiveSheet()->SetCellValue('N1', '征信(备注)');$i = 2;foreach($info as $key => $value){$objPHPExcel->getActiveSheet()->SetCellValue('A'.$i,''.$value['us_truename']);$objPHPExcel->getActiveSheet()->SetCellValue('B'.$i,' '.$value['ag_phone_number']);$objPHPExcel->getActiveSheet()->SetCellValue('C'.$i,' '.$value['ag_idcard']);$objPHPExcel->getActiveSheet()->SetCellValue('D'.$i,''.$value['ag_address']);$objPHPExcel->getActiveSheet()->SetCellValue('E'.$i,''.$value['ci_name']);$objPHPExcel->getActiveSheet()->SetCellValue('F'.$i,''.$value['ag_company_addess']);$objPHPExcel->getActiveSheet()->SetCellValue('G'.$i,''.$value['ag_ent_name']);$objPHPExcel->getActiveSheet()->SetCellValue('H'.$i,''.$value['ag_offline_num']);$objPHPExcel->getActiveSheet()->SetCellValue('I'.$i,''.$value['ag_tax_payment']);$objPHPExcel->getActiveSheet()->SetCellValue('J'.$i,''.$value['ag_billing_address']);$objPHPExcel->getActiveSheet()->SetCellValue('K'.$i,' '.$value['ag_billing_phone']);$objPHPExcel->getActiveSheet()->SetCellValue('L'.$i,''.$value['ag_billing_bank']);$objPHPExcel->getActiveSheet()->SetCellValue('M'.$i,' '.$value['ag_billing_number']);$objPHPExcel->getActiveSheet()->SetCellValue('N'.$i,''.$value['ag_info']);$i++;}$objWriter = new \PHPExcel_Writer_Excel5($objPHPExcel);header('Content-Type: application/vnd.ms-excel');header('Content-Disposition: attachment;filename='.$file_name );header('Cache-Control: max-age=0');//输出指定目录$objWriter->save('php://output');die;}/** 批量导入代理商详情*/public function upAgent(){if(request()->ispost()){// 获取表单上传文件$file = request()->file('agent_file');if(empty($file)){$this->error('请选择上传文件!');}else{//文件位置$moveUrl = ROOT_PATH.'public'.DS.'upload/files/agent/';//移动文件$info = $file->validate(['size'=>12428800,'ext'=>'xls,xlsx'])->rule('uniqid')->move($moveUrl);if($info){// 成功上传后 获取上传信息$file_name = $moveUrl.DS.$info->getFilename();//导入vendor("PHPExcel.PHPExcel");$extension = strtolower(pathinfo($file_name, PATHINFO_EXTENSION));//判断导入表格后缀格式if($extension == 'xlsx') {$objReader =\PHPExcel_IOFactory::createReader('Excel2007');$objPHPExcel =$objReader->load($file_name, $encode = 'utf-8');}else if($extension == 'xls'){$objReader =\PHPExcel_IOFactory::createReader('Excel5');$objPHPExcel =$objReader->load($file_name, $encode = 'utf-8');}$sheet = $objPHPExcel->getSheet(0);$highestRow = $sheet->getHighestRow();//取得总行数$highestColumn = $sheet->getHighestColumn(); //取得总列数$num = 0;for($i = 2; $i <= $highestRow; $i++){$map['us_truename'] = $objPHPExcel->getActiveSheet()->getCell("A".$i)->getValue();$map['us_name'] = $objPHPExcel->getActiveSheet()->getCell("A".$i)->getValue();$map['us_number'] = onlyNumber();$map['us_password'] = sha1(md5('123456'));$map['us_time'] = time();$map['us_role'] = 2; //代理商$uid = Db::name('users')->insertGetId($map);if($uid){$data['ag_usid'] = $uid;$data['ag_phone_number'] = $objPHPExcel->getActiveSheet()->getCell("B".$i)->getValue();$data['ag_idcard'] = $objPHPExcel->getActiveSheet()->getCell("C".$i)->getValue();$data['ag_address'] = $objPHPExcel->getActiveSheet()->getCell("D".$i)->getValue();$where['ci_name'] = $objPHPExcel->getActiveSheet()->getCell("E".$i)->getValue();$cityInfo = Db::name('city')->field('ci_id')->where($where)->find();$data['ag_cityid'] = $cityInfo['ci_id'] ? $cityInfo['ci_id'] : 0;$dataU['us_cityId'] = $cityInfo['ci_id'] ? $cityInfo['ci_id'] : 0;@Db::name('users')->where('us_id ='.$uid)->update($dataU);$data['ag_company_addess'] = $objPHPExcel->getActiveSheet()->getCell("F".$i)->getValue();$data['ag_offline_num'] = $objPHPExcel->getActiveSheet()->getCell("G".$i)->getValue();$data['ag_ent_name'] = $objPHPExcel->getActiveSheet()->getCell("H".$i)->getValue();$data['ag_tax_payment'] = $objPHPExcel->getActiveSheet()->getCell("I".$i)->getValue();$data['ag_billing_address'] = $objPHPExcel->getActiveSheet()->getCell("J".$i)->getValue();$data['ag_billing_phone'] = $objPHPExcel->getActiveSheet()->getCell("K".$i)->getValue();$data['ag_billing_bank'] = $objPHPExcel->getActiveSheet()->getCell("L".$i)->getValue();$data['ag_billing_number'] = $objPHPExcel->getActiveSheet()->getCell("M".$i)->getValue();$data['ag_info'] = $objPHPExcel->getActiveSheet()->getCell("N".$i)->getValue();$info = Db::name('agent')->insert($data);if($info){$num++;}else{break;}}else{break;}}//删除excel文件unlink($file_name);if($num > 0){$this->success('导入成功'.$num.'条数据',url('agent/agentList'),3); }else{$this->error('导入第'.$num.'条失败'); }}else{// 上传失败获取错误信息$this->error($file->getError());}} }}