PHPExcel之读取excel数据 导入数据库

 //读取excel数据 导入到数据库
    public function d_video()
    {
        $res = $this->goods_import('./1.xlsx', 'xlsx');//读取excel表中的数据
        dump($res);
        die;
    }

    //读取excel表中的数据
    protected function goods_import($filename, $exts = 'xls')
    {

        header("Content-Type:text/html;charset=utf-8");
        //导入PHPExcel类库,因为PHPExcel没有用命名空间,只能inport导入
        Vendor("PHPExcel");
        // Vendor('PHPExcel');
        //创建PHPExcel对象,注意,不能少了\
        $PHPExcel = new \PHPExcel();

        //如果excel文件后缀名为.xls,导入这个类
        if ($exts == 'xls') {
            Vendor("PHPExcel.Reader.Excel5");
            $PHPReader = new \PHPExcel_Reader_Excel5();

        } else if ($exts == 'xlsx') {
            Vendor("PHPExcel.Reader.Excel2007");
            $PHPReader = new \PHPExcel_Reader_Excel2007();
        }
        //载入文件
        $PHPExcel = $PHPReader->load($filename, $encode = 'utf-8');

        //获取表中的第一个工作表,如果要获取第二个,把0改为1,依次类推
        $currentSheet = $PHPExcel->getSheet(0);
        //获取总列数
        $allColumn = $currentSheet->getHighestColumn();
        //获取总行数
        $allRow = $currentSheet->getHighestRow();
        //循环获取表中的数据,$currentRow表示当前行,从哪行开始读取数据,索引值从0开始
        for ($currentRow = 1; $currentRow <= $allRow; $currentRow++) {
            //从哪列开始,A表示第一列
            for ($currentColumn = 'A'; $currentColumn <= $allColumn; $currentColumn++) {
                //数据坐标
                $address = $currentColumn . $currentRow;
                //读取到的数据,保存到数组$arr中
                $data[$currentRow][$currentColumn] = $currentSheet->getCell($address)->getValue();
            }

        }
//        return $data;
//        dump($data);die;
        $r = $this->save_import($data);
        return $r;
    }

    //将读取到的excel数据导入数据库
    public function save_import($data)
    {
        header("Content-Type:text/html;charset=utf-8");
        //dump($data);exit;
        $Goods = M('test_video');
        $video = M('kqy_video');
        foreach ($data as $k => $v) {
            $newdata['title'] = $v['A'];
            $newdata['cate'] = $v['B'];
            $newdata['industry'] = $v['C'];
            $newdata['pictures'] = $v['D'];
//            $ve =$v['E'].'xn ('.$k.').jpg';
//            $newdata['avatar']         =$ve;
            $newdata['videourl'] = $v['E'];
            $newdata['MP4'] = $v['F'];
            $newdata['prourl'] = $v['G'];
            $newdata['newvideotime'] = gmdate(" H:i:s", (new \PHPExcel_Shared_Date)->ExcelToPHP($v['H']));//从excel读取的时间转换
            $newdata['content'] = $v['I'];
            $newdata['label'] = $v['J'];
            $newdata['createtime'] = randomDate('2018-10-23 9:00:00', '2018-10-23 18:00:00');//添加时间为任意时间
            $newdata['createtimes'] = randomTime('2018-10-23 9:00:00', '2018-10-23 18:00:00');//添加时间戳
            $newdata['username'] = '13588145470';
            $newdata['userid'] = '4495';
            $newdata['videotype'] = '2';
            $newdata['result'] = '2';
            $newdata['statu'] = '1';
            $newdata['notshow'] = '1';
//            $newdata['MP4'] = $v['mp4'];
            $newdata['score'] = rand(45, 75) / 10;
            $newdata['is_automatic'] = 1;//手动上传
            $result = $video->add($newdata);
            // echo $Goods->getLastSql();
        }
        if ($result) {
            $res = '数据导入成功';
        } else {
            $res = '数据导入失败';
        }
        return $res;
    }

你可能感兴趣的:(跳坑,日常笔记,PHPexcel)