php匯入功能,模板下載

二十五歲的我發表於2020-11-04

模板下載就是放一個文件

        <a class="btn btn-primary" href="/upload/excel_tpl/匯入攤位模板下載.xlsx" download="">攤位模板下載</a>

public function upload()
    {

        //獲取表格的大小,限制上傳表格的大小5M
        $file_size = $_FILES['file']['size'];

        if ( $file_size > 5 * 1024 * 1024 ) {
            $this->error('檔案大小不能超過5M');

        }
        $marketId = $this->marketIdAndCompanyId();
        //限制上傳表格型別
        $fileExtendName = substr(strrchr($_FILES['file']["name"], '.'), 1);

        //application/vnd.ms-excel  為xls檔案型別
        if ( $fileExtendName != 'xlsx' ) {
            $this->error('必須為excel表格,且必須為xlsx格式!');
//            exit();
        }

        if ( is_uploaded_file($_FILES['file']['tmp_name']) ) {
            // 有Xls和Xlsx格式兩種
            $objReader = IOFactory::createReader('Xlsx');
//            $objReader= new Xls();

            $filename    = $_FILES['file']['tmp_name'];
            $objPHPExcel = $objReader->load($filename);  //$filename可以是上傳的表格,或者是指定的表格
            $sheet       = $objPHPExcel->getSheet(0);   //excel中的第一張sheet
            $highestRow  = $sheet->getHighestRow();       // 取得總行數
//            var_dump($highestRow);die;
            $objReader->getReadEmptyCells(false);
            // $highestColumn = $sheet->getHighestColumn();   // 取得總列數


//        var_dump($highestRow);die;
            $fail =0;
            $total = $highestRow -2;
            for ($j = 3; $j <= $highestRow; $j++) {
                $stall_number = $objPHPExcel->getActiveSheet()->getCell("A" . $j)->getFormattedValue();
                $floor_name   = $objPHPExcel->getActiveSheet()->getCell("B" . $j)->getFormattedValue();
                $area_name   = $objPHPExcel->getActiveSheet()->getCell("C" . $j)->getFormattedValue();

                $floor = model('stall_category')->where(['type'=>'樓層','market_id'=>$marketId['market_id'],'name'=>$floor_name])->field('id')->find();
                $area = model('stall_category')->where(['type'=>'業態','market_id'=>$marketId['market_id'],'name'=>$area_name])->field('id')->find();
                if(empty($floor) || empty($area)){
                    $fail++;
                    file_put_contents($marketId['market_id']."upload_stall匯入失敗的攤位.txt", "時間:".date('Y-m-d H:i:s')."市場:".$marketId['market_id']."stall_number:".$stall_number.'floor_id:'.''.'area_id:'.''.PHP_EOL, FILE_APPEND);
                    continue;
                }
                if ($stall_number && $floor_name && $area_name){
                    $stall_is_exists = model('stall')->where('market_id',$marketId['market_id'])->where(['stall_number'=>$stall_number,'floor_id'=>$floor->id,'area_id'=>$area->id])->find();
                    if($stall_is_exists){
                        $fail++;
                        file_put_contents($marketId['market_id']."upload_stall匯入失敗的攤位.txt", "時間:".date('Y-m-d H:i:s')."市場:".$marketId['market_id']."stall_number:".$stall_number.'floor_id:'.$floor->id.'area_id:'.$area->id.PHP_EOL, FILE_APPEND);
                        continue;
                    }
                    $data =[
                        'stall_number'=>$stall_number,
                        'floor_id'=>$floor->id,
                        'area_id'=>$area->id,
                        'market_id'=>$marketId['market_id'],
                    ];
                    model('stall')::create($data);
                    file_put_contents($marketId['market_id']."upload_stall匯入成功的攤位.txt", "時間:".date('Y-m-d H:i:s')."市場:".$marketId['market_id']."stall_number:".$stall_number.'floor_id:'.$floor->id.'area_id:'.$area->id.PHP_EOL, FILE_APPEND);
                }else{
                    file_put_contents($marketId['market_id']."upload_stall匯入失敗的攤位.txt", "時間:".date('Y-m-d H:i:s')."市場:".$marketId['market_id']."stall_number:".$stall_number.'floor_id:'.$floor['id'].'area_id:'.$area['id'].PHP_EOL, FILE_APPEND);
                    continue;
                }
            }
            $this->success('匯入成功,總條數為:'.$total.'失敗了:'.$fail.'條');
        }else{
            $this->error('檔案過大或格式不正確導致上傳失敗-_-!');
        }
    }

相關文章