php导入功能,模板下载

模板下载就是放一个文档

        <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('文件过大或格式不正确导致上传失败-_-!');
        }
    }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值