如何使用apache-poi处理空单元格

时间:2014-11-21 11:58:30

标签: java excel apache-poi

如何使用poi jar处理空单元格?这是代码:

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;
}

从空单元格中获取数据时,我收到switch语句错误:(s.getRow(row).getCell(i).getCellType())

1 个答案:

答案 0 :(得分:1)

您只需返回null null和空白值,然后读取它。

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