首先引用
use PhpOffice\PhpSpreadsheet\IOFactory;
具体样例如下
public function import_excel(){
$file = request()->file('imp_excel');
$objReader = IOFactory::createReader('Xlsx');
Db::startTrans();
try{
$objPHPExcel = $objReader->load($file); //$filename可以是上传的表格,或者是指定的表格
$sheet = $objPHPExcel->getSheet(0); //excel中的第一张sheet
$highestRow = $sheet->getHighestRow();
$a = 0;
$arr=array('A','B','C','D','E','F','G');
foreach($arr as $k => $currentColumn){
$name1=$objPHPExcel->getActiveSheet()->getCell($currentColumn . "1")->getValue();
if($name1!="员工积分信息"){
return error(201,"错误的文件内容!!");
}
$temp=$objPHPExcel->getActiveSheet()->getCell($currentColumn . "2")->getValue();
if($temp=='姓名')$A=$currentColumn;
if($temp=='手机号')$B=$currentColumn;
if($temp=='当前积分')$C=$currentColumn;
if($temp=='高级积分')$D=$currentColumn;
if($temp=='总积分')$E=$currentColumn;
if($temp=='部门')$F=$currentColumn;
if($temp=='岗位')$G=$currentColumn;
}
for ($i = 3; $i <= $highestRow; $i++) {
$data[$a]['username'] = (string)$objPHPExcel->getActiveSheet()->getCell($A . $i)->getValue();
$data[$a]['mobile'] = (int)$objPHPExcel->getActiveSheet()->getCell($B . $i)->getValue();
$data[$a]['points'] = (int)$objPHPExcel->getActiveSheet()->getCell($C . $i)->getValue();
$data[$a]['finance_points'] = (int)$objPHPExcel->getActiveSheet()->getCell($D . $i)->getValue();
$data[$a]['total_points'] = (int)$objPHPExcel->getActiveSheet()->getCell($E. $i)->getValue();
$data[$a]['department_name'] = (string)$objPHPExcel->getActiveSheet()->getCell($F. $i)->getValue();
$data[$a]['position'] = (string)$objPHPExcel->getActiveSheet()->getCell($G. $i)->getValue();
$t = ['username' => $data[$a]['username'], 'mobile' => $data[$a]['mobile']];
$mobile = $data[$a]['mobile'];
$insert_data=array(
'username'=>$data[$a]['username'],
'mobile'=>$data[$a]['mobile'],
'points'=>$data[$a]['points'],
'finance_points'=>$data[$a]['finance_points'],
'total_points'=>$data[$a]['total_points'] ,
'position'=>$data[$a]['position'],
);
Db::name('list')->where('mobile',$mobile)->update($insert_data);
}
Db::commit();
insert_logs("导入表更新数据");
return success("ok");
} catch ( \think\exception\ValidateException $e ){
Db::rollback();
return error(201,"导入失败请稍后重试!");
}
}
这样就OK了!
评论 (0)