首頁

Excel表格導入數據庫的方法詳解

PHPExcel表格數據庫 2018-05-02 4563

在php編程語言中(zhōng),對于如何在Excel表格中(zhōng)導入數據庫的方法是很多編程者比較頭疼的一(yī)個問題,有些技術人員(yuán)可能在百度嘗試過搜索很多不同的問題,但是給出的答案經過自己測試之後,發現還是行不通,那麽對此,專業的app開(kāi)發報價燚軒科技也嘗試了一(yī)下(xià)如何在Excel表格中(zhōng)導入數據庫,現在将源代碼展示給各位技術編程者,大(dà)家可以借鑒參考一(yī)下(xià)。

public function saveexcel(){

       require_once('./Thinkphp/Extend/Vendor/PHPExcel-1.8/Classes/PHPExcel.php');

       require_once('./Thinkphp/Extend/Vendor/PHPExcel-1.8/Classes/PHPExcel/IOFactory.php');

       require_once('./Thinkphp/Extend/Vendor/PHPExcel-1.8/Classes/PHPExcel/Reader/Excel5.php');

        //獲取表格的大(dà)小(xiǎo),限制上傳表格的大(dà)小(xiǎo)5M

        // $file_size = $_FILES['myfile']['size'];

        // if ($file_size>5*1024*1024) {

        // echo "<script>alert('上傳失敗,上傳的表格不能超過5M的大(dà)小(xiǎo)');history.go(-1);</script>";

        //     exit();

        // }

 

        //限制上傳表格類型

        $file_type = $_FILES['myfile']['type'];

        if ($file_type!='application/vnd.ms-excel') {

        echo "<script>alert('上傳失敗,隻能上傳excel200397-2003xls格式!');history.go(-1)</script>";

        exit();

        }

 

        //判斷表格是否上傳成功

        if (is_uploaded_file($_FILES['myfile']['tmp_name'])) {

            

            //以上三步加載phpExcel的類

 

        $objReader = \PHPExcel_IOFactory::createReader('Excel5');//use excel2007 for 2007 format

            //接收存在緩存中(zhōng)的excel表格

        $filename = $_FILES['myfile']['tmp_name'];

        $objPHPExcel = $objReader->load($filename); //$filename可以是上傳的表格,或者是指定的表格

        $sheet = $objPHPExcel->getSheet(0);

        $highestRow = $sheet->getHighestRow(); // 取得總行數

     // $highestColumn = $sheet->getHighestColumn(); // 取得總列數

            

            //循環讀取excel表格,讀取一(yī)條,插入一(yī)條

            //j表示從哪一(yī)行開(kāi)始讀取  從第二行開(kāi)始讀取,因爲第一(yī)行是标題不保存

            //$a表示列号

        $model=M('user');

        $rechargemodel=M('recharge');

        $joinmodel=M('joinplan');

 

        for($j=2;$j<=$highestRow;$j++)  

        {

            $name = $objPHPExcel->getActiveSheet()->getCell("B".$j)->getValue();//獲取B(姓名)列的值

            $tel = $objPHPExcel->getActiveSheet()->getCell("C".$j)->getValue();//獲取C(手機号)列的值

             $arr['name']=$name;

             $arr['tel']=$tel;

              

             $arr['addtime']=time();

             $arr['geti']=0;

             $res=$model->add($arr);

              

                

            }

            if($res){

                success('添加成功',U('userlist'));

            }else{

                error('操作失敗,請稍後再試');

            }

        }

}

 

Php excel 文件

 

代碼導出(不需要引入 任何接口)

     public function  exportDayInner(){

          $model = M("Goodsinfo");

          // $innerdata = $mod->select();

          $list = $model->order('gid desc')->select();


                      // echo $mod->getLastSql();exit;

                      // dump($innerdata);die;

          $table = '';

          $table .= "<table align='center' border='1' cellpadding'0' cellspacing='0'>

                      <thead>

                      <tr>

                      <th class='name'>名稱</th>

                      <th class='name'>名稱</th>

                      <th class='name'>名稱</th>

                      <th class='name'>名稱</th>

                      <th class='name'>名稱</th>

                      <th class='name'>名稱</th>

                      <th class='name'>名稱</th>

                      <th class='name'>名稱</th>

                      <th class='name'>名稱</th>

                      <th class='name'>名稱</th>

                      <th class='name'>名稱</th>

                      <th class='name'>名稱</th>

                      <th class='name'>名稱</th>

                      <th class='name'>名稱</th>

                      <th class='name'>名稱</th>

                      <th class='name'>名稱</th>

                      </tr>

                      </thead>

                      <tbody>";

                      foreach ($list as $v) {

                      $table .= "<tr align='center'>

                      <td class='name'>{$v['gname']}</td>

                      <td class='name'>{$v['gname']}</td>

                      <td class='name'>{$a[$v['gname']]}</td>

                      <td class='name'>{$v['gname']}</td>

                      <td class='name'>{$v['gname']}</td>

                      <td class='name'>{$b[$v['gname']]}</td>

                      <td class='name'>{$v['gname']}</td>

                      <td class='name'>{$v['gname']}/{$v['gname']} </td>

                      <td class='name'>{$v['gname']}</td>

                      <td class='name'>{$v['gname']}%</td>

                      <td class='name'>{$v['gname']}</td>

                      <td class='name'>{$v['gname']}</td>

                      <td class='name'>{$v['gname']}</td>

                      <td class='name'>{$v['gname']}</td>

                      <td class='name'>{$v['gname']}</td>

                      <td class='name'>{$c[$v['gname']]}</td>                

                      </tr>";

                      }

                      $table .= "</tbody>

                      </table>";

  //通過header頭控制輸出excel表格

                      header("Pragma: public");  

                      header("Expires: 0");  

                      header("Cache-Control:must-revalidate, post-check=0, pre-check=0");  

                      header("Content-Type:application/force-download");  

                      header("Content-Type:application/vnd.ms-execl");  

                      header("Content-Type:application/octet-stream");  

                      header("Content-Type:application/download");

                      header('Content-Disposition:attachment;filename="商(shāng)品列表.xls"');  

                      header("Content-Transfer-Encoding:binary");  

                      echo $table;

      }

以上就是爲各位講解的如何在Excel表格中(zhōng)導入數據庫的具體(tǐ)方法了,希望有不懂的技術人員(yuán)可以在評論中(zhōng)提出,我(wǒ)(wǒ)們會爲您解答。

分(fēn)享:
分(fēn)享到微信朋友圈 +
打開(kāi)微信,點擊底部的“發現”,使用 “掃一(yī)掃” 即可将網頁分(fēn)享到我(wǒ)(wǒ)的朋友圈。 如何使用?
推薦文章

熱貼More +

服務範圍More +

聯系我(wǒ)(wǒ)們

請掃二維碼聯系客服

854221200@qq.com

185-3825-9583

QQ客服

關于  ·  招聘  ·  案例中(zhōng)心  ·  網站地圖

©@2018 燚軒科技版權所有 豫ICP備16015002号-4

百度提供搜索支持