2015-04-23 101 views
2

我在Excel中像这样的公式:POI:如果输出空白公式返回空白

=IF(A1="foo";"";"0") 

如果公式返回由POI创建了resultiong csv文件空值我想没有价值。如果公式返回0我想在我的csv文件中使用0

这是我的代码的一部分(它总是多少剥离下来的代码的问题):

Iterator<Row> rowIterator = sheet.rowIterator(); 
while (rowIterator.hasNext()) { 

    Row row = rowIterator.next(); 
    Iterator<Cell> cellIterator = row.cellIterator(); 
    boolean isFirst = true; 

    for (int cn = 0; cn < row.getLastCellNum(); cn++) { 
     Cell cell = row.getCell(cn, Row.CREATE_NULL_AS_BLANK); 
     if (!isFirst) { 
      buffer.write(delimiter.getBytes(charset)); 
     } else { 
      isFirst = false; 
     } 

     // Numeric Cell type (0) 
     // String Cell type (1) 
     // Formula Cell type (2) 
     // Blank Cell type (3) 
     // Boolean Cell type (4) 
     // Error Cell type (5) 
     if (cell.getCellType() == 0 || cell.getCellType() == 2) { 
      try { 
       if (DateUtil.isCellDateFormatted(cell)) { 
        cell.setCellType(Cell.CELL_TYPE_NUMERIC); 
        Date value = cell.getDateCellValue(); 
        SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy"); 
        if (cell.getNumericCellValue() < 1) { 
         sdf.applyPattern("HH:mm:ss"); 
        } 
        buffer.write(sdf.format(value).getBytes(charset)); 
       } else { 
        double valueDouble = cell.getNumericCellValue(); 
        if (valueDouble == Math.ceil(valueDouble)) { 
         buffer.write(String.format("%d", (long) valueDouble).getBytes(charset)); 
        } else { 
         valueDouble = round(valueDouble, roundingPlaces); 
         String value = String.valueOf(valueDouble).replace(".", ","); 
         buffer.write(value.getBytes(charset)); 
        } 
       } 
      } catch (Exception e) { 
       // Formula returns a string 
       cell.setCellType(Cell.CELL_TYPE_STRING); 
       String value = cell.getStringCellValue(); 
       buffer.write(value.getBytes(charset)); 
      } 
     } else { 
      cell.setCellType(Cell.CELL_TYPE_STRING); 
      String value = cell.getStringCellValue(); 
      buffer.write(value.getBytes(charset)); 
     } 
    } 
    buffer.write("\r\n".getBytes(charset)); 
} 

在每个案件的CSV文件0此代码的结果。这是由于线路

double valueDouble = cell.getNumericCellValue(); 

documentation是这点非常清楚:

double getNumericCellValue()

获取细胞的数量的值。

对于字符串,我们抛出异常。对于空白单元格,我们返回0。对于 公式或错误单元格,我们返回预先计算的值;

如何分析单元格是否包含NULL值?

+1

你不能使用'cell.setCellType(Cell.CELL_TYPE_BLANK);' –

+0

我不知道如何调查细胞在第一位... – lanes

回答

1

@llogiq让我走上正轨!

我添加以下代码if语句之前:

 if (cell.getCellType() == 2 && cell.getCachedFormulaResultType() == 1) { 
      logger.log(Level.DEBUG, "Formula returns a string. (1)"); 
      cell.setCellType(Cell.CELL_TYPE_STRING); 
      String value = cell.getStringCellValue(); 
      buffer.write(value.getBytes(charset)); 
     } else if (cell.getCellType() == 0 || cell.getCellType() == 2) { 

现在,在空白字段公式结果在我的csv文件!我甚至可以摆脱讨厌的try catch区块。当然,现在我仍然需要美化代码...

1

我认为你的问题是公式返回一个字符“”或“0”,但你把它当作一个数字。

1

cell.getCellType() == Cell.CELL_TYPE_BLANK应该对空白为Cell为真,否则为false。

编辑:我忘了你有一个公式单元格。在这种情况下,增加到:cell.getCellType() == Cell.CELL_TYPE_FORMULA ? Cell.getCachedFormulaResultType() == Cell.CELL_TYPE_BLANK : cell.getCellType() == Cell.CELL_TYPE_BLANK

此外,您应该不应过滤空白单元格,但非数字单元格。

+0

不幸的是, 'cell.getCellType()'返回'CELL_TYPE_FORMULA'。 – lanes

+0

'cell.getCachedFormulaResultType()'不会导致'CELL_TYPE_BLANk'(不幸的是,这甚至不是可能的结果),而是在'CELL_TYPE_STRING'中。 – lanes