我在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值?
你不能使用'cell.setCellType(Cell.CELL_TYPE_BLANK);' –
我不知道如何调查细胞在第一位... – lanes