$file = request()->file('security');
$info = $file->validate(['ext'=>'xlsx'])->move(ROOT_PATH . 'public' . DS . 'uploadxls');
if($info){
$filename = ROOT_PATH . 'public' . DS . 'uploadxls'.DS.$info->getSaveName();
require_once(VENDOR_PATH .'PHPExcel/PHPExcel.php');//引入PHP EXCEL类
require_once(VENDOR_PATH .'PHPExcel/PHPExcel/IOFactory.php');//引入PHP EXCEL类
$reader = \PHPExcel_IOFactory::createReader('Excel2007');
$PHPExcel = $reader->load($filename); // 载入excel文件
$sheet = $PHPExcel->getSheet(0); // 读取第一個工作表
$highestRow = $sheet->getHighestRow(); // 取得总行数
$highestColumm = $sheet->getHighestColumn(); // 取得总列数
/** 循环读取每个单元格的数据 */
Db::startTrans();
try{
$data = array();
for ($row = 3; $row <= $highestRow; $row++){//行数是以第1行开始
if($column = 'A'){
$data['safeName'] = $sheet->getCell($column.$row)->getValue();
}
if($column = 'B'){
$data['authorization'] = $sheet->getCell($column.$row)->getValue();
}
if($column = 'C'){
$data['ruleType'] = $sheet->getCell($column.$row)->getValue();
}
if($column = 'D'){
$data['netType'] = $sheet->getCell($column.$row)->getValue();
}
if($column = 'E'){
$data['protocolType'] = $sheet->getCell($column.$row)->getValue();
}
if($column = 'F'){
$data['port'] = $sheet->getCell($column.$row)->getValue();
}
if($column = 'G'){
$data['authType'] = $sheet->getCell($column.$row)->getValue();
}
if($column = 'H'){
$data['authObject'] = $sheet->getCell($column.$row)->getValue();
}
if($column = 'I'){
$data['sort'] = $sheet->getCell($column.$row)->getValue();
}
$data['vid'] = Db::name('setting_')->where('Name', $data['safeName'])->value('vid');
$id = Db::name('setting_safe')->where($data)->value('id');
if($id){
$this->error($data['safeName'].'-第'.$row.'行-已存在');
}
if(empty($data['vid'])){
$this->error($data['safeName'].'-不存在');
}else{
Db::name('setting_safe')->insert($data);
}
}
Db::commit();
}catch(Exception $e){
Db::rollback();
$this->error('导入出现问题,请检查后重试');
}
$this->success('导入成功');
}else{
// 上传失败获取错误信息
echo $file->getError();
}