$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_vpn')->where('vpnName', $data['safeName'])->value('vid');
$id = Db::name('setting_vpnsafe')->where($data)->value('id');
if($id){
$this->error($data['safeName'].'-第'.$row.'行-已存在');
}
if(empty($data['vid'])){
$this->error($data['safeName'].'-不存在');
}else{
Db::name('setting_vpnsafe')->insert($data);
}
}
Db::commit();
}catch(Exception $e){
Db::rollback();
$this->error('導入出現問題,請檢查後重試');
}
$this->success('導入成功');
}else{
// 上傳失敗獲取錯誤信息
echo $file->getError();
}