最新消息:文章中包含代码时,请遵守代码高亮规范!

导入功能(原创)

PHP Alex 85浏览 0评论

 public function importGovAction()
 {
   $r = $this->inlet($this->getParameter("return_type_ajax"),true);
   if(!$r){
     return $r;
   }

  $m_gov_director = new m_gov_director($this->container);

  $filePath = trim($this->get('request')->get('filePath'));
  $dir = dirname(dirname(dirname(dirname(__FILE__))));
  $filePath = $dir. '/web/' . $filePath;
  $filePath = str_replace('\\', '/', $filePath);

 //实例化PHPExcel类
 $PHPReader = new \PHPExcel_Reader_Excel2007();
 if (!$PHPReader->canRead($filePath)) {
   $PHPReader = new \PHPExcel_Reader_Excel5();
   if (!$PHPReader->canRead($filePath)) {
     exit('文件格式不正确!');
    }
 }
 //实例化对象
 $m_area = new m_area($this->container);
 $m_company = new m_company($this->container);
 $m_company_contact = new m_company_contact($this->container);
 $m_staff = new m_staff($this->container);
 $m_division = new m_division($this->container);
 //设置只读,可取消类似"3.08E-05"之类自动转换的数据格式,避免写库失败
 $PHPReader->setReadDataOnly(true);
 //读取Excel文件
 $PHPExcel = $PHPReader->load($filePath);
 //读取excel文件中的第一个工作表
 $sheet = $PHPExcel->getSheet(0);
 //取得最大的列号
 $allColumn = $sheet->getHighestColumn();
 //取得最大的行号
 $allRow = $sheet->getHighestRow();

 for ($currentRow = 2; $currentRow <= $allRow; $currentRow++) {
   $ex = array();

   $division = trim($PHPExcel->getActiveSheet()->getCell("B" . $currentRow)->getValue());
   $position = trim($PHPExcel->getActiveSheet()->getCell("C" . $currentRow)->getValue());
   $name = trim($PHPExcel->getActiveSheet()->getCell("D" . $currentRow)->getValue());
   $gender = trim($PHPExcel->getActiveSheet()->getCell("E" . $currentRow)->getValue());
   $phone = trim($PHPExcel->getActiveSheet()->getCell("F" . $currentRow)->getValue());

   $qq = trim($PHPExcel->getActiveSheet()->getCell("G" . $currentRow)->getValue());
   $weixin = trim($PHPExcel->getActiveSheet()->getCell("H" . $currentRow)->getValue());
 
   if(empty($name)){
     continue;
   }

   if($gender=='男'){
     $gender =1;
   }elseif ($gender=='女'){
     $gender =2;
   }else{
     return $this->response(" 第".$currentRow."行,性别只能是男或女");
   }

   $ex['division'] = $division;
   $ex['position'] = $position;
   $ex['name'] = $name;
   $ex['gender'] = $gender;
   $ex['phone'] = $phone;
   $ex['qq'] = $qq;
   $ex['weixin'] = $weixin;

   //添加并验证
   $res = $m_gov_director->insertOrUpdate(0,$ex);

   if($res['errorCode']==1){
    return $this->response("导入失败,第".$currentRow."行,".$res['message']['message']);
   }

  }

  return new JsonResponse(Responses::arrays("导入成功",0));
 }

转载时请注明出处及相应链接,本文永久地址:http://blog.it985.com/22674.html


pay_weixin
pay_weixin
微信打赏
pay_weixin
支付宝打赏
感谢您对作者Alex的打赏,我们会更加努力!    如果您想成为作者,请点我

您必须 登录 才能发表评论!