如何利用PHP匯入Excel資料到資料庫

來源:互聯網
上載者:User
這篇文章主要介紹了關於如何利用PHP匯入Excel資料到資料庫,有著一定的參考價值,現在分享給大家,有需要的朋友可以參考一下

    /**     * 匯入excel     * @throws \PHPExcel_Exception     * @throws \PHPExcel_Reader_Exception     */    public function impExcel($filename,$data){        if (!empty($_FILES)) {            $config = array(                'exts'=>array('xlsx','xls'),                'rootPath'=>"./",                'savePath'=> "Public/Uploads/$filename/",                'subName'    =>    array('date','Ymd'),            );            $upload = new \Think\Upload($config);            if (!$info=$upload->upload()) {                $this->error($upload->getError());            }            vendor("PHPExcel.PHPExcel");            $file_name = $info['excel']['savepath'].$info['excel']['savename'];            //$objReader = \PHPExcel_IOFactory::createReader('Excel5');            $Excel_name = getcwd() . '/' .$file_name;            //$aaa = is_readable($Excel_name);           // $objPHPExcel = $objReader->load($Excel_name,$encode='utf-8');            $extension = strtolower( pathinfo($Excel_name, PATHINFO_EXTENSION) );            if ($extension =='xlsx') {                $objReader = new \PHPExcel_Reader_Excel2007();                $objPHPExcel = $objReader ->load($Excel_name);            } else if ($extension =='xls') {                $objReader = new \PHPExcel_Reader_Excel5();                $objPHPExcel = $objReader ->load($Excel_name);            } else if ($extension=='csv') {                $PHPReader = new \PHPExcel_Reader_CSV();                //預設輸入字元集                $PHPReader->setInputEncoding('GBK');                //預設的分隔字元                $PHPReader->setDelimiter(',');                //載入檔案                $objPHPExcel = $PHPReader->load($Excel_name);            }            $sheet = $objPHPExcel->getSheet(0);            $highestRow = $sheet->getHighestRow(); // 取得總行數            $highestColumn = $sheet->getHighestColumn(); // 取得總列數            for($i=2;$i<=$highestRow;$i++)            {                $data['name'] = $objPHPExcel->getActiveSheet()->getCell("A".$i)->getValue();                $data['tel'] = $objPHPExcel->getActiveSheet()->getCell("B".$i)->getValue();                $data['zjh']    = $objPHPExcel->getActiveSheet()->getCell("C".$i)->getValue();                $data['sheng'] = $objPHPExcel->getActiveSheet()->getCell("D".$i)->getValue();                $data['shi'] = $objPHPExcel->getActiveSheet()->getCell("E".$i)->getValue();                $data['address']= $objPHPExcel->getActiveSheet()->getCell("F".$i)->getValue();                $data['money']= $objPHPExcel->getActiveSheet()->getCell("G".$i)->getValue();                $data['hk_time']= $objPHPExcel->getActiveSheet()->getCell("H".$i)->getValue();                $data['nanyi']= $objPHPExcel->getActiveSheet()->getCell("I".$i)->getValue();                $data['uid'] = $_SESSION['user']['id'];                $data['time'] = date("Y-m-d H:i:s");                M('release')->add($data);            }            $this->success('匯入成功!');exit();        }else        {            $this->error("請選擇上傳的檔案");        }    }
調用:
 if(IS_POST)        {            $data['reason'] = I("post.reason","","trim");            $data['type'] = I("post.type","","trim");           // $data['qixian'] = I("post.qixian","","trim");           // $data['comm'] = I("post.comm","","trim");            if(!empty($_FILES)){                $this->impExcel('batch_release',$data);            }        }

以上就是本文的全部內容,希望對大家的學習有所協助,更多相關內容請關注topic.alibabacloud.com!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.