ThinkPHP3.2.3,PHPExcel上传文件…

xiaoxiao2021-02-28  145

首先,你要下载PHPExcel文件,放在Vender下面。 控制器写: //值班信息批量 function duty_list_add(){ $this->display(); } public function user_upload(){ if (!empty($_FILES)) { $config = array( 'exts' => array('xlsx','xls'), 'maxSize' => 3145728, 'rootPath' =>'./Public', 'savePath' => '/Excel/', 'subName' => array('date','Y-m-d'), ); $upload = new \Think\Upload($config); if (!$info = $upload->upload()) { $this->error($upload->getError());} vendor("PHPExcel.PHPExcel"); vendor("PHPExcel.PHPExcel.IOFactory"); $file_name=$upload->rootPath.$info['excel']['savepath'].$info['excel']['savename']; $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(); //取得总列数 $j=0; for ($i = 1; $i <= $highestRow; $i++) { $data['db_leader']= $objPHPExcel->getActiveSheet()->getCell("A".$i)->getValue(); $data['zb_leader']= $objPHPExcel->getActiveSheet()->getCell("B".$i)->getValue(); $data['zb_duty']= $objPHPExcel->getActiveSheet()->getCell("C".$i)->getValue(); $data['zh_duty']= $objPHPExcel->getActiveSheet()->getCell("D".$i)->getValue(); $data['zh_center']= $objPHPExcel->getActiveSheet()->getCell("E".$i)->getValue(); $data['jg_duty']= $objPHPExcel->getActiveSheet()->getCell("F".$i)->getValue(); $data['duty_time'] = \PHPExcel_Shared_Date::ExcelToPHP($objPHPExcel->getActiveSheet()->getCell("G".$i)->getValue()); if(M('Duty')->where("duty_time=".$data['duty_time'])->find()){ $this->error("存在相同日期,请认真核对导入日期"); //如果存在相同时间。判断条件:时间存在相同,则返回错误 }else{ M('Duty')->add($data); $j++; } } $this->success('导入成功!返回列表页查看!!',U('Duty/duty_list')); } else { $this->error("请选择上传的文件"); } } View内容: <form class="form form-horizontal" method="post" enctype="multipart/form-data" action="{:U('Duty/user_upload')}" id="form-file-add"> <div class="row cl"> <label class="form-label col-xs-1 col-sm-2">上传人:</label> <div class="formControls col-xs-2 col-sm-2"> <input type="text" class="input-text" value="{$Think.session.username}" placeholder="" disabled id="author" name="author"> </div> </div> <div class="row cl"> <label class="form-label col-xs-1 col-sm-2">值班信息文件:</label> <div class="formControls col-xs-2 col-sm-2"> <input type="file" class="input-text" value="" placeholder="" id="upload_url" name="excel"> </div> </div> <div class="row cl"> <div class="col-xs-8 col-sm-9 col-xs-offset-1 col-sm-offset-2"> <button class="btn btn-primary radius" type="submit"><i class="Hui-iconfont"></i> 提交</button> </div> </div> </form>
转载请注明原文地址: https://www.6miu.com/read-34429.html

最新回复(0)