poi讀取excel處理,包含空行處理

之前項目有個讀取excel數據並落庫的處理,因爲沒有處理空行導致失敗。 

//讀取解析文件將其數據存放在List中
    public List ReadFromExcel(String fileName){ 
        List result=new ArrayList();
        File file=new File(fileName);
        try{
            InputStream in = new FileInputStream(file);
            Workbook wb=WorkbookFactory.create(in);
            Sheet sheet=wb.getSheetAt(0);
            int len=sheet.getLastRowNum()+1;
            for(int i=1;i<len;i++){
                Row row= sheet.getRow(i);

               if(IsRowEmpty(row)){
                   continue;
               }
                Row  rowtitle=sheet.getRow(0);
                int lenj=row.getLastCellNum();
                HashMap rowMap=new HashMap();
                HashMap map=new HashMap();
                for(int j=0;j<lenj;j++){
                    Cell  cell = row.getCell(j);
                    Cell cellTitle=rowtitle.getCell(j);
                    String titleStr=getCellValue(cellTitle);
                    String str=getCellValue(cell);
                    map.put(titleStr, str);
                }
                rowMap.put(i-1, map);
                result.add(rowMap);
            }
        }catch(Exception e){
            e.printStackTrace();
        }
        return result;
     }

//獲取單元格的值  
    @SuppressWarnings("unused")
    public String getCellValue( Cell cell) {  
        String cellValue = "";  
        DataFormatter formatter = new DataFormatter();  
        if (cell != null) {  
            //判斷單元格數據的類型,不同類型調用不同的方法  
            switch (cell.getCellType()) {  
                //數值類型  
                case Cell.CELL_TYPE_NUMERIC:  
                    //進一步判斷 ,單元格格式是日期格式   
                    if (DateUtil.isCellDateFormatted(cell)) {
                        Date date=cell.getDateCellValue();
                        cellValue=new SimpleDateFormat("yyyy-MM-dd").format(date);
                        //cellValue = formatter.formatCellValue(cell);  
                    } else {  
                        //數值  
                        double value = cell.getNumericCellValue();  
                        int intValue = (int) value;  
                        cellValue = value - intValue == 0 ? String.valueOf(intValue) : String.valueOf(value);  
                    }  
                    break;  
                case Cell.CELL_TYPE_STRING:  
                    cellValue = cell.getStringCellValue();  
                    break;  
                case Cell.CELL_TYPE_BOOLEAN:  
                    cellValue = String.valueOf(cell.getBooleanCellValue());  
                    break;  
                    //判斷單元格是公式格式,需要做一種特殊處理來得到相應的值  
                case Cell.CELL_TYPE_FORMULA:{  
                    try{  
                        cellValue = String.valueOf(cell.getNumericCellValue());  
                    }catch(IllegalStateException e){  
                        cellValue = String.valueOf(cell.getRichStringCellValue());  
                    }  
                      
                }  
                    break;  
                case Cell.CELL_TYPE_BLANK:  
                    cellValue = "";  
                    break;  
                case Cell.CELL_TYPE_ERROR:  
                    cellValue = "";  
                    break;  
                default:  
                    cellValue = cell.toString().trim();  
                    break;  
            }  
        }  
        return cellValue.trim();  
    }

//判斷讀取excel是否空行  空行時返回true 非空行直接返回false

 public static boolean IsRowEmpty(Row row){
        for(int i=row.getFirstCellNum();i<row.getLastCellNum();i++){
            Cell cell=row.getCell(i);
            if(cell!=null&&cell.getCellType()!=cell.CELL_TYPE_BLANK){
                return false;
            }
        }
        return true;
    }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章