程序员qiao

"Hello word" 即是起点,也是终点。

tp5 Excel导入数据库

    public function daoru()
    {
        $param = $this->request->param();
        require_once Env::get('extend_path') . 'phpexcel/Classes/PHPExcel/IOFactory.php';  
        $DOMAIN = $_SERVER['HTTP_HOST'];
        $file = $_FILES;
        $filename = $file['file']['name'];
        $file_temp_name =$file['file']['tmp_name'];
        $dir = '/www/wwwroot/files/';
        $type = strstr($filename,'.');
        if($type != '.xls' && $type != '.xlsx')
        {
            return json(['code'=>500,'msg'=>'格式错误']);
        }
        if(is_uploaded_file($file_temp_name))
        {
            $full_name = $dir.time().'_'.$filename;
            $result = move_uploaded_file($file_temp_name, $full_name);
        }
        else
        {
            return json(['code'=>500,'msg'=>'失败']);
        }
        //如果上传文件成功,就执行导入 excel操作
        $objReader = \PHPExcel_IOFactory::createReaderForFile($full_name);
        $objPHPExcel = $objReader->load($full_name);
        $objPHPExcel->setActiveSheetIndex(0);
        $sheet = $objPHPExcel->getSheet(0);
        $highestRow = $sheet->getHighestRow(); // 取得总行数
        $test = $objPHPExcel->getActiveSheet()->getCell('A2')->getValue();
        $data = array();
        for ($i=2; $i <= $highestRow ; $i++) 
        {
            $sn =  $objPHPExcel->getActiveSheet()->getCell('A'.$i)->getValue();
            $pwd = $objPHPExcel->getActiveSheet()->getCell('B'.$i)->getValue();
            $data = array(
                'number' => $sn,
                'money' => $pwd,
                'create_time' => time(),
                'update_time' => time(),
            );
            $res = Db::name('pos_number')->insert($data);
        }
        if($res)
        {
            return json(['code'=>200,'msg'=>'导入成功']);
        }
        return json(['code'=>500,'msg'=>'导入失败']);
    }

 

声明 本人博客的所有东西,部分源于网络书籍和视频,其他的是个人的理解感悟,经过自己整理撰写成博客。 本人博客均只用于个人学习、复习,不作为商业用途,如有侵权,请联系我修改或删除。 联系邮箱:15121014713@163.com

posted on 2022-01-13 17:43  程序员qiao  阅读(200)  评论(0编辑  收藏  举报

导航