phpexcel導入excel文件(二)

phpexcel導入excel文件:

    一、步驟

        01)、獲取表單上傳文件

                    $file = request()->file('excel');

        02)、移動到框架應用根目錄/public/uploads/ 目錄下

                    $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');

        03)、調用importexcel方法

                    $data = Excel::importexcel($path);

        04)、獲取路徑

                     $type = pathinfo($path);

        05)、判斷excel文件類型

                    (xlsx,xls) =>   createReader($type)

        06)、讀取文件  

                     $objPHPExcel = $objReader->load($path);

        07)、獲取工作薄     

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

        08)、獲取總行數   

                    $highestRow = $sheet->getHighestRow();

        09)、獲取總列數 

                     $highestColumn = $sheet->getHighestColumn();

        10)、獲取每個單元格的值

                    $data[$j][]=$objPHPExcel->getActiveSheet()->getCell("$k$j")->getValue();

        11)、組裝數組,完成業務邏輯。

    二、詳細過程設計

        1、新建extend->Excel->Excel.php文件

            01)、給定和使用命名空間  

      namespace Excel;
        use PHPExcel;
        use PHPExcel_IOFactory;
            02)、新建importexcel方法
       public  static function importexcel($path){
            // 判斷文件是什麼格式
            $type = pathinfo($path);
    //        dump($type);
            $type = strtolower($type["extension"]);

            if ($type == 'xlsx') {
                $type = 'Excel2007';
            } elseif ($type == 'xls') {
                $type = 'Excel5';
            }
            //最大加載時間 ‘0’代表加載結束爲止
            ini_set('max_execution_time', '0');
            // 判斷使用哪種格式
            $objReader = PHPExcel_IOFactory::createReader($type);
    //        dump($objReader);
            $objPHPExcel = $objReader->load($path);
            //獲取工作薄
            $sheet = $objPHPExcel->getSheet(0);
            // 取得總行數
            $highestRow = $sheet->getHighestRow();
            // 取得總列數
            $highestColumn = $sheet->getHighestColumn();
            //循環讀取excel文件,讀取一條,插入一條
            $data=array();
            //從第一行開始讀取數據
            for($j=1;$j<=$highestRow;$j++){
                //從A列讀取數據
                for($k='A';$k<=$highestColumn;$k++){
                    // 讀取單元格
                    $data[$j][]=$objPHPExcel->getActiveSheet()->getCell("$k$j")->getValue();
                }
            }
            //以數組的形式打印excel表中的數據
    //        echo '<pre>';
    //        print_r($data);
    //        echo '</pre>';
    //        dump($data);
            return $data;
        }

          2、在index.php中寫入import方法

                01)、在index.php中定義和使用命名空間              

        use PHPExcel;
        use PHPExcel_IOFactory;
        use think\Request;
        use think\View;

                02)、在index.php中寫入import方法    

          public function  import(){
                $data = array();
                if (Request::instance()->isPost()){

                // 獲取表單上傳文件
                $file = request()->file('excel');
                //dump($file);

                //獲取$file中的數據信息方法
                //$filename  = $file->getFilename();
                //dump($filename);

                // 移動到框架應用根目錄/public/uploads/ 目錄下
                if($file){
                    $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
                    if($info){
                        // 成功上傳後 獲取上傳信息
                        $path  =  './uploads/'.$info->getSaveName();
//                        dump($path);
                    }else{
                        // 上傳失敗獲取錯誤信息
                        echo $file->getError();
                    }
                }

                if($path){
                    $data = Excel::importexcel($path);
                }
            }

            $view = new View();
            $view->list = $data;
            return $view->fetch();
        }

        3、在view中引入頁面

    <!DOCTYPE html>
     <html lang="en">
      <head>
        <meta charset="UTF-8">
        <title>test</title>
        <!-- 最新版本的 Bootstrap 核心 CSS 文件 -->
        <link rel="stylesheet" href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
     </head>
     <body>
       <div class="col-xs-6 col-xs-offset-3" style="margin-top: 20px">
          <form action="" enctype="multipart/form-data" method="post">
             <div class="form-group">
                  <input type="file" name="excel" class="form-control"/>
             </div>
             <div class="form-group">
                  <input type="submit" value="上傳" class="btn btn-primary"/>
             </div>
         </form>

       <div class="panel panel-primary">
         <!-- Default panel contents -->
           <div class="panel-heading">表格數據</div>
          <!-- Table -->
           <table class="table table-striped">
               {foreach $list as $data}
                <tr>
                    {foreach $data as $d}
                      <td>{$d}</td>
                    {/foreach}
                </tr>
               {/foreach}
           </table>
         </div>
       </div>
      </body>
    </html>

                    

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章