tp3.2中excel表格数据导入数据库

第一步:将压缩包PHPExcel解压后放到./ThinkPHP/Library/Vendor目录下面。

第二步:控制器里面的方法:


        public function addActivitydo(){
            if (!empty($_FILES)){
            $upload = new \Think\Upload();                      // 实例化上传类
            $upload->maxSize   =     10485760 ;                 // 设置附件上传大小
            $upload->exts      =     array('xls','xlsx');       // 设置附件上传类型
            $upload->rootPath   =       './Uploads/Excel';//上传根目录
            $upload->autoSub   = false;                         // 将自动生成以photo后面加时间的形式文件夹,关闭
            // 上传文件
            $info   =   $upload->upload();
            $exts   = $info['file']['ext'];
                                             // 获取文件后缀
            $filename = $upload->rootPath.$info['file']['savename'];        // 生成文件路径名
            if(!$info) {                                                     // 上传错误提示错误信息
                $this->error($upload->getError());
            }else{
                vendor("PHPExcel.PHPExcel");                              // 导入PHPExcel类库
                $PHPExcel = new \PHPExcel();                                 // 创建PHPExcel对象,注意,不能少了\
                if ($exts == 'xls') {                                        // 如果excel文件后缀名为.xls,导入这个类
                    vendor("PHPExcel.PHPExcel.Reader.Excel5");
                    $PHPReader = new \PHPExcel_Reader_Excel5();
                } else

                    if ($exts == 'xlsx') {
                        vendor("PHPExcel.PHPExcel.Reader.Excel2007");
                        $PHPReader = new \PHPExcel_Reader_Excel2007();

                    }
                $PHPExcel=$PHPReader->load($filename);
                $currentSheet = $PHPExcel->getSheet(0);                      // 获取表中的第一个工作表,如果要获取第二个,把0改为1,依次类推
                $allColumn = $currentSheet->getHighestColumn();              // 获取总列数
                $allRow = $currentSheet->getHighestRow();                    // 获取总行数
                $data=array();
                for($j=1;$j<=$allRow;$j++){
                    //从A列读取数据
                    for($k='A';$k<=$allColumn;$k++){
                        // 读取单元格
                        $data[$j][]=$PHPExcel->getActiveSheet()->getCell("$k$j")->getValue();
                    }
                }
               for ($i = 2; $i <= $allRow; $i++) {
                    $data_p['number'] =$PHPExcel->getActiveSheet()->getCell("A" . $i)->getValue();
                    $data_p['money'] =$PHPExcel->getActiveSheet()->getCell("B" .$i)->getValue();
                    $data_p['lifetime'] =intval(($PHPExcel->getActiveSheet()->getCell("C" .$i)->getValue()-25569)* 3600 * 24);//此处时间之所以这样处理,是因为excel表格日期导入数据库不是正确的时间戳
                     $ex=M('activity')->add($data_p);
              }
            if($ex){                                         
               $this->success("导入成功");
            }else{
                $this->error("导入失败,原因可能是excel表中格式错误","5");// 提示错误
               }
            }
        }else {
          $this->display();
        }    

        }

第三步:视图层



   

        
       

           
           
       

       

           

               
           

       

   


              
   



你可能感兴趣的:(TP)