码迷,mamicode.com
首页 > 其他好文 > 详细

解析excel文件

时间:2015-05-19 13:21:03      阅读:144      评论:0      收藏:0      [点我收藏+]

标签:

public static List<PlatRule> readExcel(File file) throws Exception{
          InputStream inputStream = new FileInputStream(file);  
         String fileName = file.getName();  
         Workbook wb = null;  
         if(fileName.endsWith("xls")){  
             wb = new HSSFWorkbook(inputStream);//解析xls格式  
         }else if(fileName.endsWith("xlsx")){  
             wb = WorkbookFactory.create(inputStream);//解析xlsx格式  
         }  
         Sheet sheet = wb.getSheetAt(0);//第一个工作表
         int firstRowIndex = sheet.getFirstRowNum()+2;  
         int lastRowIndex = sheet.getLastRowNum();
         PlatRule rule;
         List<PlatRule> ruleList = new ArrayList<PlatRule>();
         for(int rIndex = firstRowIndex; rIndex <= lastRowIndex; rIndex ++){
             rule= new PlatRule();
             Row row = sheet.getRow(rIndex);
             if(row != null){  
                 rule.setRuleName(row.getCell(0)==null?"":row.getCell(0).toString());
                 rule.setRuleType(row.getCell(1)==null?"":row.getCell(1).toString());
                 rule.setRiskType(row.getCell(3)==null?"":row.getCell(3).toString());
                 rule.setRuleNo(row.getCell(4)==null?"":row.getCell(4).toString());
                 rule.setPackages(row.getCell(6)==null?"":row.getCell(6).toString());
                 rule.setObjects(row.getCell(7)==null?"":row.getCell(7).toString());
                 rule.setFunctions(row.getCell(8)==null?"":row.getCell(8).toString());
                 rule.setDescription(row.getCell(9)==null?"":row.getCell(9).toString());
             }
             ruleList.add(rule);
             rule=null;
         }
         return ruleList;
     }


解析excel文件

标签:

原文地址:http://my.oschina.net/u/1246433/blog/416588

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!