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>

                    

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值