PHPExcel插件导入excel功能

 

 

1、把excel先导入到服务器

public function import() {
        require_once ROOT . DS . 'vendor' . DS . 'UploadFile.class.php';  //封装的上传文件类库
        if ($this->request->is(['patch', 'post', 'put'])) {
            $data = $this->request->data;
                    if (!(@$data['import_file'])) {
                            $this->returnError($this->errorCode[202]);
                    }
                // 实例化类库
                    $upfile = new \UploadFile($data['import_file']);
                    $upfile->path = TMP; //保存路径
                    $upfile->typelist =array('xls','xlsx');
                    if($info = $upfile->upload()){  //上传成功
                        $uploadFile = $upfile->path.$upfile->savename;
                      $exts = $upfile->ext;  //后缀名
                   //调用data_import接口,读取excel数据,$uploadFile文件保存的路径+文件名。$exts,文件后缀,$types 根据不同类型区分导入不同的文件
                     $this->data_import($uploadFile, $exts,3,$data['types']);
                   //  pr($upfile);die;
                    } else {
                        $this->returnError($upfile->error);
                    }
        }
    }            

2、读取excel文件的数据接口

$uploadFile文件保存的路径+文件名。
$exts,文件后缀,
$types 根据不同类型区分导入不同的文件
public function data_import($filename, $exts = 'xls',$or ,$types=1){
       header("Content-Type:text/html;charset = utf-8"); 
       //导入PHPExcel类库,因为PHPExcel
       require_once ROOT . DS . 'vendor' . DS .'phpexcel'. DS . 'PHPExcel.php';
        //创建PHPExcel对象,注意,不能少了\
        $PHPExcel = new \PHPExcel();
        //如果excel文件后缀名为.xls,导入这个类
        if ($exts == 'xls') {
           require_once ROOT . DS . 'vendor' . DS .'phpexcel'. DS .'PHPExcel'. DS .'Reader'. DS . 'Excel5.php';
            $PHPReader = new \PHPExcel_Reader_Excel5();
        } else if ($exts == 'xlsx') {
           require_once ROOT . DS . 'vendor' . DS .'phpexcel'. DS .'PHPExcel'. DS .'Reader'. DS . 'Excel2007.php';
            $PHPReader = new \PHPExcel_Reader_Excel2007();
        }
 
 
        //载入文件
        $PHPExcel = $PHPReader->load($filename);
        //获取表中的第一个工作表,如果要获取第二个,把0改为1,依次类推
        $currentSheet = $PHPExcel->getSheet(0);
        //获取总列数
        $allColumn = $currentSheet->getHighestColumn();
       //获取总行数
        $allRow = $currentSheet->getHighestRow();
        //循环获取表中的数据,$currentRow表示当前行,从哪行开始读取数据,索引值从0开始
        for ($currentRow = 2; $currentRow <= $allRow; $currentRow++) {
            //从哪列开始,A表示第一列
            for ($currentColumn = 'A'; $currentColumn <= $allColumn; $currentColumn++) {
                //数据坐标
                $address = $currentColumn . $currentRow;
                //读取到的数据,保存到数组$data中
                $cell = $currentSheet->getCell($address)->getValue();
 
                if ($cell instanceof PHPExcel_RichText) {
                    $cell = $cell->__toString();
                }
                $data[$currentRow - 1][$currentColumn] = $cell;
                //  print_r($cell);
            }
 
        }
        //    pr($data);die;
        if($types==1){  //模板导入
            // 写入数据库操作
            $this->insert_data($data);
        }elseif($types==2){   //资产导入
            // 写入数据库操作
            $this->insert_assets($data);        
        }
        
    }

 

posted on 2021-10-28 11:16  泽一年  阅读(389)  评论(0编辑  收藏  举报

导航