1. 程式人生 > >PHPExcel之讀取excel資料 匯入資料庫

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;
    }