zoukankan      html  css  js  c++  java
  • php Excel 导入


    php Excel 导入

        public function storeSql()
        {
            $file = input('file.excel');
            $path = ROOT_PATH . 'public' . DS . 'uploads';
            if ($file) {
                $info = $file->move($path);
                if ($info) {
                    $this->dataStore($info->getPathname());
                } else {
                    $this->error($file->getError());
    
                }
            }
    
        }
    
        //数据导入
        public function dataStore($filePath)
        {
            import('phpoffice.phpexcel.Classes.PHPExcel');
            import('phpoffice.phpexcel.Classes.IOFactory');
            import('phpoffice.phpexcel.Classes.Reader.Excel2007');
            $PHPExcel = new PHPExcel();
            $PHPReader = new PHPExcel_Reader_Excel2007();
            if (!$PHPReader->canRead($filePath)) {
                $PHPReader = new PHPExcel_Reader_Excel5();
                if (!$PHPReader->canRead($filePath)) {
                    $this->error('上传失败!');
                }
            }
            //读取Excel文件
            $PHPExcel = $PHPReader->load($filePath);
            //读取excel文件中的第一个工作表
            $sheet = $PHPExcel->getSheet(0);
            //取得最大的列号
            $allColumn = $sheet->getHighestColumn();
            //取得最大的行号
            $allRow = $sheet->getHighestRow();
            $user = new UserOff;
            $phones = $user->where('merchant_id', $this->userID)->column('phone');
            $all = [];
            //从第二行开始插入,第一行是列名
            for ($currentRow = 2; $currentRow <= $allRow; $currentRow++) {
                $data['phone'] = $PHPExcel->getActiveSheet()->getCell("A" . $currentRow)->getValue();
                $data['point'] = $PHPExcel->getActiveSheet()->getCell("B" . $currentRow)->getValue();
                $data['growth'] = $PHPExcel->getActiveSheet()->getCell("C" . $currentRow)->getValue();
                $data['card_num'] = $PHPExcel->getActiveSheet()->getCell("D" . $currentRow)->getValue();
                $data['user_name'] = $PHPExcel->getActiveSheet()->getCell("E" . $currentRow)->getValue();
                $data['merchant_id'] = $this->userID;
                $data['add_time'] = time();
                $data['phone_no'] = $data['phone'] . $this->userID . "AcDE"; //编号
                empty($data['card_num']) && $data['card_num'] = 0;
                empty($data['user_name']) && $data['user_name'] = "";
                empty($data['phone']) && $data['user_name'] = "";
                empty($data['point']) && $data['point'] = 0;
                empty($data['growth']) && $data['growth'] = 0;
                array_push($all,$data);
            }
            $allData =$this->diffArr($all,$phones);
    
            $update = $user->saveAll($allData['allDataUp'], true);
            //$update = true;
            $insert = $user->saveAll($allData['allDataIn'], false);
            if ($update || $insert) {
                $this->success('数据导入成功!', url('dump/index'));
            } else {
                $this->error('数据导入失败!');
            }
        }
    
    
  • 相关阅读:
    系统编程-进程-fork深度理解、vfork简介
    九鼎S5PV210开发板的SD卡启动、uboot tftp升级内核镜像
    jiffies相关时间比较函数time_after、time_before详解
    Java基础第六章(循环结构二)
    Java基础第五章(循环结构一)
    Java基础第四章(选择结构二)
    Java基础第三章(选择结构一)
    Java基础第二章(变量、数据类型和运算符)
    Java基础第一章
    HTML iframe的使用
  • 原文地址:https://www.cnblogs.com/datiangou/p/10206168.html
Copyright © 2011-2022 走看看