excel导入图片

 /**
     * 导入信息
     * @return \think\response\Json
     * @throws \PHPExcel_Exception
     * @throws \PHPExcel_Reader_Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function import()
    {
        $objPHPExcel = new \PHPExcel();

        //获取表单上传文件
        $file = request()->file('file');
        $info = $file->validate(['size' => 15678000, 'ext' => 'xlsx,xls,csv'])->move('storage');

        if ($info) {
            $exclePath = $info->getSaveName();  //获取文件名
            $file_name = 'storage/' . $exclePath;   //上传文件的地址

            $objReader =  \PHPExcel_IOFactory::createReaderForFile($file_name);;
            $obj_PHPExcel = $objReader->load($file_name, $encode = 'utf-8');  //加载文件内容,编码utf-8

            $excel_array = $obj_PHPExcel->getsheet(0)->toArray();   //转换为数组格式

            array_shift($excel_array);  //删除第一个数组(标题);

            // 图片保存路径
            $imageFilePath1 = 'storage/'; // 图片保存目录
            $imageFilePath2 = 'upload/images/' . date("Ymd") . '/';
            $imageFilePath = $imageFilePath1 . $imageFilePath2;
            if (!file_exists($imageFilePath)) {
                mkdir("$imageFilePath", 0777, true);
            }

            $images = [];
            // 处理图片
            foreach ($obj_PHPExcel->getsheet(0)->getDrawingCollection() as $drawing) {
                list($startColumn, $startRow) = Coordinate::coordinateFromString($drawing->getCoordinates());
                $imageFileName = $drawing->getIndexedFilename();  // 获取文件名
                switch ($drawing->getExtension()) {
                    case 'jpg':
                    case 'jpeg':
                        $source = imagecreatefromjpeg($drawing->getPath());
                        imagejpeg($source, $imageFilePath . $imageFileName);
                        break;
                    case 'gif':
                        $source = imagecreatefromgif($drawing->getPath());
                        imagegif($source, $imageFilePath . $imageFileName);
                        break;
                    case 'png':
                        $source = imagecreatefrompng($drawing->getPath());
                        imagepng($source, $imageFilePath . $imageFileName);
                        break;
                }
                $startColumn = $this->ABC2decimal($startColumn);
                $images[$startRow - 1][$startColumn] = '/' . $imageFilePath . $imageFileName;
            }

            $i = 0;
            $error_list = [];
            $error = 0;
            foreach ($excel_array as $key => $v) {
                $status = 2; //是否允许插入 1-否 2-是
                $data = [];
                $string = '序号'.$v[0];
                $data['m_name']   = $v[1] ?? '';
                $data['m_sex']    = ($v[2] == '女' ? 2 : 1);
                $data['m_phone'] = $data['m_tel'] = $v[3] ?? '';

                //判断手机号是否存在
                if(MemberModel::getThisByPhone($data['m_phone'])){
                    $string .= ',手机号已存在';
                    $status = 1;
                }

                //判断门店是否存在
                $store = StoreModel::getThisByCode($v[4]);
                if($store) $data['s_id'] = $store->s_id;
                else{
                    $string .= ',门店不存在';
                    $status = 1;
                }

                $data['m_birthday']  = ((int)date('Y') - ((int)$v[5] ?? '0')) . '-01-01';
                $data['m_height']    = $v[6] ?? '';
                $data['m_education'] = array_flip(MemberModel::$m_education_list)[($v[7] ?? '')] ?? 0;
                $data['m_salary']    = array_flip(MemberModel::$m_salary_list)[($v[8] ?? '')] ?? 0;
                $data['m_weight']    = $v[9] ?? '';
                $data['m_post']      = array_flip(MemberModel::$m_post_list)[($v[10] ?? '')] ?? 0;
                $data['is_marry']    = array_flip(MemberModel::$marry_list)[($v[11] ?? '')] ?? 0;
                $data['is_house']    = array_flip(MemberModel::$house_list)[($v[12] ?? '')] ?? 0;
                $data['is_car']      = ($v[13] == '是' ? 2 : 1);
                $data['is_smoke']    = ($v[14] == '是' ? 2 : 1);
                $data['is_wine']     = ($v[15] == '是' ? 2 : 1);
                $data['m_parent']    = $v[16] ?? '';
                $data['m_child']     = $v[17] ?? '';
                $data['m_introduce'] = $v[18] ?? '';
                $data['m_idcard']    = $v[19] ?? '';

                //判断身份证号是否存在
                if($data['m_idcard'] != ''){
                    if(MemberModel::getThisByIdCard($data['m_idcard'])){
                        $string .= ',身份证号已存在';
                        $status = 1;
                    }
                }

                if($data['m_sex'] == 2 && $v[19] == '') $data['m_idcard'] = '';

                $data['m_file'] = $images[$key + 1][20] ?? '';

                $data['create_at'] = $v[21] ?? date('Y-m-d H:i:s');
                $data['update_at'] = date('Y-m-d H:i:s');

                $vip_time = $v[22] ?? '';
                if($vip_time != ''){
                    $data['vip_time'] = strtotime('+1 year',strtotime($vip_time));
                }

                if($status == 2) Db::name('member')->insert($data);
                else {
                    $error++;
                    $error_list[] = $string;
                }

                $i++;
            }

            $success = $i - $error;

            $string = '';
            if(count($error_list) > 0){
                $string = '<br>'.implode('<br>',$error_list);
            }

            return json(['code'=>200,'message'=>"总{$i}条,成功{$success}条,失败{$error}条。".$string]);
            // Db::name('t_station')->insertAll($city); //批量插入数据
        } else {
            // 上传失败获取错误信息
            echo $file->getError();
        }
    }
    /**
     * 字母序列化为数字
     */
    public function ABC2decimal($abc)
    {
        $ten = 0;
        $len = strlen($abc);
        for ($i = 1; $i <= $len; $i++) {
            $char = substr($abc, 0 - $i, 1);//反向获取单个字符
            $int = ord($char);
            $ten += ($int - 65) * pow(26, $i - 1);
        }
        return $ten;
    }
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容