TP5.1excel導入數(shù)據(jù)庫的示例分析

這篇文章將為大家詳細講解有關(guān)TP5.1excel導入數(shù)據(jù)庫的示例分析,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。

專注于為中小企業(yè)提供網(wǎng)站建設(shè)、成都網(wǎng)站設(shè)計服務,電腦端+手機端+微信端的三站合一,更高效的管理,為中小企業(yè)江城免費做網(wǎng)站提供優(yōu)質(zhì)的服務。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動了上千企業(yè)的穩(wěn)健成長,幫助中小企業(yè)通過網(wǎng)站建設(shè)實現(xiàn)規(guī)模擴充和轉(zhuǎn)變。

在用之前必須
use think\PHPExcel;
并且這個是第三方類庫,這個類庫是直接用命令能后下載的
composer 命令:
composer require phpoffice/phpexcel

public function save(){
        //前臺的form表單大家都會吧,我就不寫了
        //設(shè)置文件上傳的最大限制
        ini_set('memory_limit','1024M');
        //加載第三方類文件
        require_once "../extend/PHPExcel/PHPExcel.php";
        //防止亂碼
        header("Content-type:text/html;charset=utf-8");
        //實例化主文件
        //接收前臺傳過來的execl文件
        $file = $_FILES['file'];
        //截取文件的后綴名,轉(zhuǎn)化成小寫
        $extension = strtolower(pathinfo($file['name'],PATHINFO_EXTENSION));
        if($extension == "xlsx"){
            //2007(相當于是打開接收的這個excel)
            $objReader =\PHPExcel_IOFactory::createReader('Excel2007');
        }else{
            //2003(相當于是打開接收的這個excel)
            $objReader = \PHPExcel_IOFactory::createReader('Excel5');
        }

        $objContent = $objReader -> load($file['tmp_name']);

        if ($objContent){
            $sheetContent = $objContent -> getSheet(0) -> toArray();
            //刪除第一行標題
   //dump($sheetContent);die;
   //其實到這里就結(jié)束了,就已經(jīng)得到excel表格的所有數(shù)據(jù)了,下面就是如何插入數(shù)據(jù)庫的問題了 ,下面我的方法大家可以參考

   $len = count($sheetContent);
   //var_dump($len);exit;
   if($len == 1){
    echo "<script>alert('請不要拿空表格來騙我');window.history.back();</script>";
    exit();
   }
   
   for($i = 1;$i<$len; $i++){
    $data = [
     'id' => '',
     'phone' => $sheetContent[$i][0],
     'company_name' => $sheetContent[$i][1],
     'add_id' => $sheetContent[$i][2],
     'industry_id' => $sheetContent[$i][3],
     'data_name' => $sheetContent[$i][4],
     'data_address' => $sheetContent[$i][5],
     'data_class' => $sheetContent[$i][6],
     'data_set_up_date' => $sheetContent[$i][6],
     'create_time' => date('Y-m-d')
    ];
    //dump($data);die;
    $re = Db::name('data_decoration_company')->insert($data);
   }
            //var_dump($res);die;
            if($re){
    echo "<script>alert('導入成功');window.history.back();</script>";
    exit();
    $this->redirect('/')->remember();
            }else{
                echo "<script>alert('導入失敗了,慢點慢點@!@本電腦的腦子已經(jīng)跟不上你的節(jié)奏了');window.history.back();</script>";
    exit();
    $this->redirect('/')->remember();
            }
        }else{
            $this->error('請導入表格 !');
        }
    }

關(guān)于“TP5.1excel導入數(shù)據(jù)庫的示例分析”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,使各位可以學到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。

網(wǎng)站標題:TP5.1excel導入數(shù)據(jù)庫的示例分析
路徑分享:http://muchs.cn/article22/piscjc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計公司、面包屑導航、軟件開發(fā)、小程序開發(fā)、網(wǎng)站營銷虛擬主機

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都網(wǎng)頁設(shè)計公司