java解析excel2003以及2007 版本

 /**
     * 从模板中取出数据
     * @param actionMapping
     * @param actionform
     * @param request
     * @param response
     * @return
     * @throws IOException 
     * @throws FileNotFoundException 
     */
    public  ActionForward performGetT21_EAP_baseInfo_Add_template(EapaccbaseDTO accbase) throws FileNotFoundException, IOException {
        
        ArrayList<T21_EAP_acc_punish_info> punishList=new ArrayList<T21_EAP_acc_punish_info>();
        ArrayList<T21_EAP_accountabilityPunish> t21_eap_acc_base_infoList=new ArrayList<T21_EAP_accountabilityPunish>();
        T21_EAP_accountabilityPunish t21_eap_acc_base_info =new T21_EAP_accountabilityPunish();    
        T21_EAP_acc_punish_info t21_eap_acc_punish_info =new T21_EAP_acc_punish_info();
        //保存员工处罚基础信息
        readTheTemplateDateInfo(t21_eap_acc_base_info,accbase);
        //保存员工处罚明细信息
        readTheTemplateDatePunish(t21_eap_acc_base_info,accbase);
        return null;
    }
    /**
     * 读取_附件2-员工处罚信息表基础数据
     * @throws IOException 
     * @throws FileNotFoundException 
     */
    public void readTheTemplateDateInfo(T21_EAP_accountabilityPunish t21_eap_acc_base_info,EapaccbaseDTO accbase) throws FileNotFoundException, IOException{ 
        //被处罚时机构层级
        LinkedHashMap belongorglevelMap = cm.getMapFromCache("T211005");    
        //被处罚是岗位层级
        LinkedHashMap postlevelMap = cm.getMapFromCache("T10034");    
        //是否解除处分
        Map punishStateMap = cm.getMapFromCache("T10045");
        Map jobstatusMap = cm.getMapFromCache("T10027");
        Map idcardtypeMap =cm.getMapFromCache("T10035");        
        //性别
        Map sexMap = cm.getMapFromCache("T10003");
        //用工身份
        Map identityMap = cm.getMapFromCache("T10024");
        //政治面貌
        Map politicalstatusMap = cm.getMapFromCache("T10025");
        //最高学历
        Map highesteduMap = cm.getMapFromCache("T10011");
        //处罚机构
        Map punishorgtypeMap = cm.getMapFromCache("T10026");
        //被处罚时职务层级
        Map joblevelMap = cm.getMapFromCache("T10037");
        //问题类别
        Map problemMap = cm.getMapFromCache("T10023");
        
        List<T21_EAP_accountabilityPunish> t21_eap_acc_base_infoList =new ArrayList<T21_EAP_accountabilityPunish>();
        String filename =accbase.getAttachFJ0();
    
             FileInputStream file = new FileInputStream(filename);
                
             try {
                Workbook wb=null;
                String suffix=filename.substring(filename.lastIndexOf('.')+1);
                Sheet sheet =null;
                
                if(suffix.equals("xls")){                            
                        wb = new HSSFWorkbook(file);
                        sheet = (HSSFSheet) wb.getSheetAt(0);
                        HSSFRow row ;
                        HSSFCell cell1;
                        
                }else if(suffix.equals("xlsx")){
                       wb=new  XSSFWorkbook(file);    
                        sheet = (XSSFSheet) wb.getSheetAt(0);
                       XSSFRow row ;
                       XSSFCell cell1;
                      
                }
            
                //也可以用以下方式来获取excel的工作表,采用工作表的索引值
               
                int rows=sheet.getLastRowNum();
                int line=sheet.getRow(3).getPhysicalNumberOfCells();        
                String str5=sheet.getRow(2).getCell(0).toString();
                
                String[] str51s=sheet.getRow(2).getCell(0).toString().split("位:");
                int startnNum = str51s[0].length()+2;
                
                String[] str52s=sheet.getRow(2).getCell(0).toString().split("填报时间");
                int endNum = str52s[0].length();    
                
                String[] str=sheet.getRow(rows-1).getCell(0).toString().split(":");
                String str2=str[1].substring(0,str[1].indexOf("联系电话"));    
             

                //创建人 
                 t21_eap_acc_base_info.setCreator(str2.trim());
              
                //String createorgname = str5.substring(startnNum,endNum).trim();
                
                
                //填报单位
               // t21_eap_acc_base_info.setCreateorgname(createorgname);
                //获取创建机构id
                t21_eap_acc_base_info.setCreateorgcode(accbase.getOrgankey());
                  
                //获取创建机构
                String createorgname ="select organname  from T00_organ where organname ='"+t21_eap_acc_base_info.getCreateorgcode()+"'";            
                t21_eap_acc_base_info.setCreateorgname((String)sqlMap.queryForObject("getStringValue",createorgname));
                //创建部门
                t21_eap_acc_base_info.setCreatedeptname(accbase.getTMainDept());
                String createdeptcode ="select organkey  from T00_organ where organname ='"+t21_eap_acc_base_info.getCreatedeptname()+"'"+"and uporgankey='"+accbase.getOrgankey()+"'and field5='1' ";            
                t21_eap_acc_base_info.setCreatedeptcode((String)sqlMap.queryForObject("getStringValue",createdeptcode));
                //创建时间
                t21_eap_acc_base_info.setCreatetime(DateUtils.getCurrTime());
                //填报时间
                String tbTime =getCellFormatValue(sheet.getRow(2).getCell(0)).replaceAll(" ","").replaceAll(" ","");    
                String time=tbTime.replaceAll("[年,月]","-").replaceAll("[^\\x00-\\xff]","").substring(1);
                t21_eap_acc_base_info.setReporttime(time);
                if("--".equals(t21_eap_acc_base_info.getReporttime())){
                    t21_eap_acc_base_info.setReporttime("");
                }
                //被处罚人姓名
                t21_eap_acc_base_info.setUsername(getCellFormatValue(sheet.getRow(3).getCell(1)));
                //被处罚人员工号
                t21_eap_acc_base_info.setUsercode(getCellFormatValue(sheet.getRow(3).getCell(5)));
                t21_eap_acc_base_info.setSex(getCellFormatValue(sheet.getRow(4).getCell(1)));
                //用工身份
                t21_eap_acc_base_info.setIdentity(getCellFormatValue(sheet.getRow(4).getCell(5)));
                //证件类型
                t21_eap_acc_base_info.setIdcardtype(getCellFormatValue(sheet.getRow(5).getCell(1)));
                //证件号码
                t21_eap_acc_base_info.setIdcard(getCellFormatValue(sheet.getRow(5).getCell(5)));
                // 政治面貌
                t21_eap_acc_base_info.setPoliticalstatus(getCellFormatValue(sheet.getRow(6).getCell(1)));
                //最高学历
                t21_eap_acc_base_info.setHighestedu(getCellFormatValue(sheet.getRow(6).getCell(5)));
                //被处罚行为发生时所在机构名称
                t21_eap_acc_base_info.setHappenorgname(getCellFormatValue(sheet.getRow(7).getCell(1)));
                String happenorgcode ="select organkey  from T00_organ where organname ='"+t21_eap_acc_base_info.getHappenorgname()+"'";            
                t21_eap_acc_base_info.setHappenorgcode((String)sqlMap.queryForObject("getStringValue",happenorgcode));
                //所在机构层级
                t21_eap_acc_base_info.setOrglevel(getCellFormatValue(sheet.getRow(7).getCell(5)));
                t21_eap_acc_base_info.setBelongorglevel(getCellFormatValue(sheet.getRow(7).getCell(5)));
                //被处罚行为发生时所在部门名称
                t21_eap_acc_base_info.setHappendeptname(getCellFormatValue(sheet.getRow(8).getCell(1)));
                String happendeptcode ="select organkey  from T00_organ where organname ='"+t21_eap_acc_base_info.getHappendeptname()+"'"+"and uporgankey='"+t21_eap_acc_base_info.getHappenorgcode()+"'and field5='1' ";;            
                t21_eap_acc_base_info.setHappendeptcode((String)sqlMap.queryForObject("getStringValue",happendeptcode));                
                //被处罚时岗位
                t21_eap_acc_base_info.setPosition(getCellFormatValue(sheet.getRow(8).getCell(5)));
                //被处罚时职务层级
                t21_eap_acc_base_info.setJoblevel(getCellFormatValue(sheet.getRow(9).getCell(1)));
                //被处罚时时岗位层级
                t21_eap_acc_base_info.setPostlevel(getCellFormatValue(sheet.getRow(9).getCell(5)));
                //是否具有董(理)事、监事、高管任职资格
                t21_eap_acc_base_info.setIssenior(getCellFormatValue(sheet.getRow(10).getCell(1)));
                //处罚文号
                t21_eap_acc_base_info.setFileno(getCellFormatValue(sheet.getRow(10).getCell(5)));
                //违法违规违纪基本事实
                t21_eap_acc_base_info.setBasetruth(getCellFormatValue(sheet.getRow(11).getCell(1)));
                //处罚依据
                t21_eap_acc_base_info.setPunishbasis(getCellFormatValue(sheet.getRow(12).getCell(1)));
                //问题类别
                t21_eap_acc_base_info.setProblem(getCellFormatValue(sheet.getRow(13).getCell(1)));
                //检查项目来源
                t21_eap_acc_base_info.setProjname(getCellFormatValue(sheet.getRow(14).getCell(1)));
                if("".equals(t21_eap_acc_base_info.getProjname())){
                     t21_eap_acc_base_info.setProjname("--");
                }
                
                //将取到的数据转换类别
                String accbaseid= accbase.getUnid();
                t21_eap_acc_base_info.setAccbaseid(accbaseid);
                
                //被处罚时机构层级
                     Iterator<String> itBelongorglevel = belongorglevelMap.keySet().iterator();
                     while(itBelongorglevel.hasNext()){
                        String key = itBelongorglevel.next();
                        String value = (String) belongorglevelMap.get(key);
                        if(t21_eap_acc_base_info.getBelongorglevel().equals(value)){
                            t21_eap_acc_base_info.setBelongorglevel(key);
                        }           
                     }
                    //被处罚是岗位层级
                     Iterator<String> itPostlevel = postlevelMap.keySet().iterator();
                     while(itPostlevel.hasNext()){
                         String key = itPostlevel.next();
                         String value = (String)postlevelMap.get(key);
                        if(t21_eap_acc_base_info.getPostlevel().equals(value)){
                            t21_eap_acc_base_info.setPostlevel(key);
                        }
                    }                    
                     //是否解除处分
                     Iterator<String> itPunishState= punishStateMap.keySet().iterator();
                     while(itPunishState.hasNext()){
                         String key = itPunishState.next();
                         String value = (String)punishStateMap.get(key);
                        if(t21_eap_acc_base_info.getPunishState().equals(value)){
                            t21_eap_acc_base_info.setPunishState(key);
                        }          
                   }
                    //在职状态
                     Iterator<String> itJobstatus= jobstatusMap.keySet().iterator();
                     while(itJobstatus.hasNext()){
                         String key = itJobstatus.next();
                         String value = (String)jobstatusMap.get(key);
                        if(t21_eap_acc_base_info.getJobstatus().equals(value)){
                            t21_eap_acc_base_info.setJobstatus(key);
                        }          
                   }
                     
                    //证件类型
                     Iterator<String> itIdcardtype= idcardtypeMap.keySet().iterator();
                     while(itIdcardtype.hasNext()){
                         String key = itIdcardtype.next();
                         String value = (String)idcardtypeMap.get(key);
                        if(t21_eap_acc_base_info.getIdcardtype().equals(value)){
                            t21_eap_acc_base_info.setIdcardtype(key);
                        }                        
                   }                

                    //用工身份
                     Iterator<String> itIdentity= identityMap.keySet().iterator();
                     while(itIdentity.hasNext()){
                         String key = itIdentity.next();
                         String value = (String)identityMap.get(key);
                        if(t21_eap_acc_base_info.getIdentity().equals(value)){
                            t21_eap_acc_base_info.setIdentity(key);
                        }                 
                   }                
                     
                     //性别
                     Iterator<String> itSex = sexMap.keySet().iterator();
                     while(itSex.hasNext()){
                         String key = itSex.next();
                         String value = (String) sexMap.get(key);                
                        if(t21_eap_acc_base_info.getSex().equals(value)){
                            t21_eap_acc_base_info.setSex(key);
                        }
                     }
                     
                     //政治面貌
                     Iterator<String> itPoliticalstatus = politicalstatusMap.keySet().iterator();
                     while(itPoliticalstatus.hasNext()){
                         String key = itPoliticalstatus.next();
                         String value = (String) politicalstatusMap.get(key);
                        if(t21_eap_acc_base_info.getPoliticalstatus().equals(value)){
                            t21_eap_acc_base_info.setPoliticalstatus(key);
                        }
                         
                     }
                
                     //最高学历
                     Iterator<String> itHighestedu = highesteduMap.keySet().iterator();
                     while(itHighestedu.hasNext()){
                         String key = itHighestedu.next();
                         String value = (String) highesteduMap.get(key);
                        if(t21_eap_acc_base_info.getHighestedu().equals(value)){
                            t21_eap_acc_base_info.setHighestedu(key);
                        }
                     }
                                     
                    //处罚机构 
                     Iterator<String> itPunishorgtype = punishorgtypeMap.keySet().iterator();
                     while(itPunishorgtype.hasNext()){
                         String key = itPunishorgtype.next();
                         String value = (String) punishorgtypeMap.get(key);
                        if(t21_eap_acc_base_info.getPunishorgtype().equals(value)){
                            t21_eap_acc_base_info.setPunishorgtype(key);
                        }
                     }
                     
                    //被处罚时职务层级
                     Iterator<String> itJoblevel = joblevelMap.keySet().iterator();
                     while(itJoblevel.hasNext()){
                         String key = itJoblevel.next();
                         String value = (String) joblevelMap.get(key);
                        if(t21_eap_acc_base_info.getJoblevel().equals(value)){
                            t21_eap_acc_base_info.setJoblevel(key);
                        }
                     }
                     
                    //问题类别
                     Iterator<String> itProblem = problemMap.keySet().iterator();
                     while(itProblem.hasNext()){
                         String key = itProblem.next();
                         String value = (String) problemMap.get(key);
                        if(t21_eap_acc_base_info.getProblem().equals(value)){
                            t21_eap_acc_base_info.setProblem(key);
                        }
                        
                     }
                    
                         //是否具有董(理)事、监事、高管任职资格
                         if("否".equals(t21_eap_acc_base_info.getIssenior())){
                             t21_eap_acc_base_info.setIssenior("0");
                         }else if("是".equals(t21_eap_acc_base_info.getIssenior())){
                             t21_eap_acc_base_info.setIssenior("1");
                         }                     
                         
                         t21_eap_acc_base_info.setDeleteflag("0");
                         t21_eap_acc_base_info.setState("03");
                         t21_eap_acc_base_info.setJobstatus("01");
                         
                                  
                        t21_eap_acc_punishDAO.getT21_EAP_info_entry_save(sqlMap, t21_eap_acc_base_info);    
        } catch (Exception e) {
            logger.error(e.getMessage());
        } finally{
            file.close();
        }
    
        
    }
    
    /**
     * @throws IOException 
     * @throws FileNotFoundException 
     * 
     * 
     */
    public void readTheTemplateDatePunish(T21_EAP_accountabilityPunish t21_eap_acc_base_info,EapaccbaseDTO accbase) throws FileNotFoundException, IOException{ 

        //处罚种类
        LinkedHashMap punishtypeMap = cm.getMapFromCache("T10013");    
        //处罚方式
         //刑事处罚
        LinkedHashMap xingshiPunishMap = cm.getMapFromCache("T10042");
        //行政处罚
        LinkedHashMap xingzhenPunishMap = cm.getMapFromCache("T10043");
        //党纪处罚
        LinkedHashMap dangjiPunishMap = cm.getMapFromCache("T10044");
        //内部处分
        LinkedHashMap neibuPunishMap = cm.getMapFromCache("T10014");    
         //其他处罚
        LinkedHashMap otherPunishMap = cm.getMapFromCache("T10020");
        
        String filename =accbase.getAttachFJ0();
        FileInputStream file = new FileInputStream(filename);
         try {
            Workbook wb=null;
            String suffix=filename.substring(filename.lastIndexOf('.')+1);
            Sheet sheet =null;
            if(suffix.equals("xls")){                            
                    wb = new HSSFWorkbook(file);
                    sheet = (HSSFSheet) wb.getSheetAt(0);
                    HSSFRow row ;
                    HSSFCell cell1;
            }else if(suffix.equals("xlsx")){
                   wb=new  XSSFWorkbook(file);    
                    sheet = (XSSFSheet) wb.getSheetAt(0);
                   XSSFRow row ;
                   XSSFCell cell1;
            }                        
            
                //也可以用以下方式来获取excel的工作表,采用工作表的索引值
                int rows=sheet.getLastRowNum();
                int line=sheet.getRow(3).getPhysicalNumberOfCells();
                int rowsEnd =rows-4;
                //循环的次数
                int rowsQu=rowsEnd-16;
                
                int i;
                for(i=0;i<=rowsQu;i++){
                    
                    T21_EAP_acc_punish_info t21_eap_acc_base_punish_Line = new T21_EAP_acc_punish_info();
                    t21_eap_acc_base_punish_Line.setPunishtype(getCellFormatValue(sheet.getRow(16+i).getCell(0)));
                    t21_eap_acc_base_punish_Line.setPunishway(getCellFormatValue(sheet.getRow(16+i).getCell(1)));
                    t21_eap_acc_base_punish_Line.setPunishmoney(getCellFormatValue(sheet.getRow(16+i).getCell(2)));
                    t21_eap_acc_base_punish_Line.setPunishtime(getCellFormatValue(sheet.getRow(16+i).getCell(3)));
                    t21_eap_acc_base_punish_Line.setPunishtimelimit(getCellFormatValue(sheet.getRow(16+i).getCell(4)));
                    t21_eap_acc_base_punish_Line.setRelievetime(getCellFormatValue(sheet.getRow(16+i).getCell(5)));
                    
                    //处罚种类
                        Iterator<String> itPunishtype = punishtypeMap.keySet().iterator();
                        while(itPunishtype.hasNext()){
                            String Typekey = itPunishtype.next();
                            String TypeValue = (String)punishtypeMap.get(Typekey);
                               //if start 1
                               if(t21_eap_acc_base_punish_Line.getPunishtype().equals(TypeValue)){
                                   t21_eap_acc_base_punish_Line.setPunishtype(Typekey);
                                     Iterator<String> itXingshiPunish = xingshiPunishMap.keySet().iterator();
                                     while(itXingshiPunish.hasNext()){
                                         String key = itXingshiPunish.next();
                                         String value = (String)xingshiPunishMap.get(key);
                                        if(t21_eap_acc_base_punish_Line.getPunishway().equals(value)){
                                            t21_eap_acc_base_punish_Line.setPunishway(key);
                                        }                
                                  }
                                     
                                     Iterator<String> itXingzhenPunish = xingzhenPunishMap.keySet().iterator();
                                     while(itXingzhenPunish.hasNext()){
                                         String key = itXingzhenPunish.next();
                                         String value = (String)xingzhenPunishMap.get(key);
                                        if(t21_eap_acc_base_punish_Line.getPunishway().equals(value)){
                                            t21_eap_acc_base_punish_Line.setPunishway(key);
                                        }                
                                  }        
                                  
                                  
                                      Iterator<String> itDangjiPunish = dangjiPunishMap.keySet().iterator();
                                         while(itDangjiPunish.hasNext()){
                                             String key = itDangjiPunish.next();
                                             String value = (String)dangjiPunishMap.get(key);
                                            if(t21_eap_acc_base_punish_Line.getPunishway().equals(value)){
                                                t21_eap_acc_base_punish_Line.setPunishway(key);
                                            }     
                                      }         
                                  
                                      Iterator<String> itNeibuPunish = neibuPunishMap.keySet().iterator();
                                         while(itNeibuPunish.hasNext()){
                                             String key = itNeibuPunish.next();
                                             String value = (String)neibuPunishMap.get(key);
                                            if(t21_eap_acc_base_punish_Line.getPunishway().equals(value)){
                                                t21_eap_acc_base_punish_Line.setPunishway(key);
                                            }         
                                      }    
                                  
                                      Iterator<String> itOtherPunish= otherPunishMap.keySet().iterator();
                                         while(itOtherPunish.hasNext()){
                                             String key = itOtherPunish.next();
                                             String value = (String)otherPunishMap.get(key);
                                            if(t21_eap_acc_base_punish_Line.getPunishway().equals(value)){
                                                t21_eap_acc_base_punish_Line.setPunishway(key);
                                            }         
                                       }                                            
                               }//if end 1
                     }//while end 
                        

                        String accpunishid= uuidGenerator.generate();
                        t21_eap_acc_base_punish_Line.setAccbaseid(accbase.getUnid());
                        t21_eap_acc_base_punish_Line.setAccpunishid(accpunishid);                       
                     
                        t21_eap_acc_punishDAO.getT21_EAP_punishInfo_entry_save(sqlMap, t21_eap_acc_base_punish_Line);

                }//for end
                
         } catch (Exception e) {
                logger.error(e.getMessage());
            } finally{
                file.close();
            }
    }
    
    
    public SqlMapClient getSqlMap() {
        return sqlMap;
    }
    public void setSqlMap(SqlMapClient sqlMap) {
        this.sqlMap = sqlMap;
    }
    public DownloadFileUtil getDownload_util() {
        return download_util;
    }
    public void setDownload_util(DownloadFileUtil downloadUtil) {
        download_util = downloadUtil;
    }
    public AttachFJUtil getAttach_util() {
        return attach_util;
    }
    public void setAttach_util(AttachFJUtil attachUtil) {
        attach_util = attachUtil;
    }
    public UUIDGenerator getUuidGenerator() {
        return uuidGenerator;
    }
    public void setUuidGenerator(UUIDGenerator uuidGenerator) {
        this.uuidGenerator = uuidGenerator;
    }
    public CatchManager getCm() {
        return cm;
    }
    public void setCm(CatchManager cm) {
        this.cm = cm;
    }
    public T21_EAP_acc_punishDAO getT21_eap_acc_punishDAO() {
        return t21_eap_acc_punishDAO;
    }
    public void setT21_eap_acc_punishDAO(T21_EAP_acc_punishDAO t21EapAccPunishDAO) {
        t21_eap_acc_punishDAO = t21EapAccPunishDAO;
    }
    
    
    public static String getCellFormatValue(Cell cell)throws Exception {
        String cellvalue = "";
        if (cell != null) {
            
            switch (cell.getCellType()) {
            
            case HSSFCell.CELL_TYPE_NUMERIC:
            case HSSFCell.CELL_TYPE_FORMULA: {
                
                if (HSSFDateUtil.isCellDateFormatted(cell)) {

                    Date date = cell.getDateCellValue();
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                    cellvalue = sdf.format(date);

                }else {
                    
                    cellvalue = NumberToTextConverter.toText(cell.getNumericCellValue()) ;
                    
                }
                break;
            }
            case HSSFCell.CELL_TYPE_STRING:
                cellvalue = cell.getRichStringCellValue().getString();
                break;
            default:
                cellvalue = "";
            }
        }else {
            cellvalue = "";
        }
    
        return cellvalue.trim();
    }

    
}

猜你喜欢

转载自blog.csdn.net/jing5481/article/details/53331600
今日推荐