2014-11-21 55 views
2

如何使用poi jars處理空單元?這是代碼:如何使用apache-poi處理空單元

public static String getdata(String SheetName, String ObjectLabel, int row) throws InvalidFormatException, IOException{ 
    String objvalue = null; 
    FileInputStream fi = new FileInputStream(dir + "\\src\\pack\\TaurusRefactored\\TaurusRegistration1.xls"); 
    Workbook w = WorkbookFactory.create(fi); 
    Sheet s = w.getSheet(SheetName); 
    int rowcount = s.getLastRowNum(); 
    int columncount = s.getRow(0).getLastCellNum(); 
    for (int i = 0; i < columncount; i++) { 
     String objlabl = s.getRow(0).getCell(i).getStringCellValue(); 
     if (objlabl.equals(ObjectLabel)) { 
      switch (s.getRow(row).getCell(i).getCellType()) { 
      case Cell.CELL_TYPE_STRING: 
       objvalue = s.getRow(row).getCell(i).getStringCellValue().trim(); 
       break; 
      case Cell.CELL_TYPE_NUMERIC: 
       objvalue = Integer.toString((int) s.getRow(row).getCell(i).getNumericCellValue()); 
       break; 
      case Cell.CELL_TYPE_BLANK: 
       objvalue = ""; 
      default: 
       break; 
      } 
     } 
    } 
    return objvalue; 
} 

我得到了錯誤的開關語句:(s.getRow(row).getCell(i).getCellType())而從空電池獲取的數據。

+0

這與硒有什麼關係?!?! – SiKing 2014-11-21 21:57:56

+0

[如何在Apache POI中獲取Excel空白單元格值?](http://stackoverflow.com/questions/4929646/how-to-get-an-excel-blank-cell-value-in-apache -poi) – Gagravarr 2014-11-21 22:25:18

回答

1

您只需返回單元格空白值然後讀取它。

Cell cell = row.getCell(cn, Row.RETURN_NULL_AND_BLANK); 
if ((cell == null) || (cell.equals("")) || (cell.getCellType() == cell.CELL_TYPE_BLANK))