phpexcel导入数据库

 public function importExcel($file){
//        include(__DIR__.'/../../../vendor/PHPExcel/Classes/PHPExcel.php');
        $cacheMethod = \PHPExcel_CachedObjectStorageFactory::cache_to_phpTemp;
        $cacheSettings = array('memoryCacheSize' => '16MB');
        \PHPExcel_Settings::setCacheStorageMethod($cacheMethod, $cacheSettings);//文件缓存
        //当前空间不用\,非当前空间要加\
        $PHPExcel = new \PHPExcel();//创建一个excel对象
        $PHPReader = new \PHPExcel_Reader_Excel2007(); //建立reader对象,excel—2007以后格式
        if (!$PHPReader->canRead($file)) {
            $PHPReader = new \PHPExcel_Reader_Excel5();//建立reader对象,excel—2007以前格式
            if (!$PHPReader->canRead($file)) {
                $msg = '不是excel格式文件';
                $this->apiReturn(303,$msg);
            }
        }
        $PHPExcel = $PHPReader->load($file); //加载excel对象
        $sheet = $PHPExcel->getSheet(0); //获取指定的sheet表
        $rows = $sheet->getHighestRow();//行数
        $cols = $sheet->getHighestColumn();//列数

        $data = array();
        for ($i = 3; $i <= $rows; $i++){ //行数是以第1行开始
            $count = 0;
            for ($j = 'A'; $j <= $cols; $j++) { //列数是以A列开始
                $value = $sheet->getCell($j . $i)->getValue();

                    $data[$i - 3][$count] = (string)$sheet->getCell($j . $i)->getValue() ?? "";
                    $count += 1;
                if ($value) {
                }
            }
        }
        return $data;
    }

    public function index()
    {
        //接收上传文件
        $file = $_FILES['file'];
        if (!$file) {
            $this->error(303, '缺少file');
        }
        //调用导入excel方法
        $data = $this->importExcel($file['tmp_name']);
//        dump($data);
//        die;
        $sql = [];
        for ($i = 1; $i < count($data); $i++) {
            $sql[$i]['measure_area'] = $data[$i][3];//建筑面积
            $sql[$i]['building_id_id'] = $data[$i][4];//楼栋
            $sql[$i]['unit'] = $data[$i][5];//单元
            $sql[$i]['room_number'] = $data[$i][6];//房间号
            $sql[$i]['fo_measure_area'] = $data[$i][7];//F1面积
            $sql[$i]['fo_orientation'] = $data[$i][8];//F1房建方向
            $sql[$i]['fo_rent'] = $data[$i][9];//F1租金
            $sql[$i]['ft_orientation'] = $data[$i][10];//F2面积
            $sql[$i]['ft_orientation'] = $data[$i][11];//F2房建方向
            $sql[$i]['ft_rent'] = $data[$i][12];//F2租金
            $sql[$i]['ft_orientation'] = $data[$i][13];//F3面积
            $sql[$i]['ft_orientation'] = $data[$i][14];//F3房建方向
            $sql[$i]['ft_rent'] = $data[$i][15];//F3租金
            $sql[$i]['rent'] = (int)$data[$i][9] + (int)$data[$i][12] + (int)$data[$i][15];//租金

            $sql[$i]['hr_name'] = "爱心嘉园";
            $sql[$i]['community_id_id'] = Db::name('community_management')->where(['community_name'=>$sql[$i]['hr_name']])->value('id',0);
            if ($sql[$i]['community_id_id'] == 0){
                echo $sql[$i]['hr_name'];
                die;
            }
        }
        dump($sql);
        die;
        //写入数据库
        Db::name('auth')->insertAll($sql);
    }

composer require phpoffice/phpexcel

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

苗先生的PHP记录

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值