![](/img/trans.png)
[英]Apache POI formula to check any of the cell from the range is not blank
[英]POI: output blank if formula returns blank
我在Excel中有一個像這樣的公式:
=IF(A1="foo";"";"0")
如果公式返回空值,則我不希望POI創建的結果csv文件中沒有值。 如果公式返回一個0
我想一個0
在我的csv文件。
這是我的代碼的一部分(這始終是將代碼剝離多少的問題):
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();
關於這一點, 文檔非常清楚:
double getNumericCellValue()
以數字形式獲取單元格的值。
對於字符串,我們拋出異常。 對於空白單元格,我們返回0。對於公式或誤差單元格,我們返回預先計算的值;對於單元格,則返回預先計算的值。
如果單元格包含NULL值,如何分析?
我認為您的問題是公式返回的字符為“”或“ 0”,但是您將其視為數字。
cell.getCellType() == Cell.CELL_TYPE_BLANK
對於空白Cell
應該為true,否則為false。
編輯:我忘了你有一個公式單元格。 在這種情況下,請擴展為: cell.getCellType() == Cell.CELL_TYPE_FORMULA ? Cell.getCachedFormulaResultType() == Cell.CELL_TYPE_BLANK : cell.getCellType() == Cell.CELL_TYPE_BLANK
cell.getCellType() == Cell.CELL_TYPE_FORMULA ? Cell.getCachedFormulaResultType() == Cell.CELL_TYPE_BLANK : cell.getCellType() == Cell.CELL_TYPE_BLANK
另外,您可能不應該過濾空白單元格,而是非數字單元格。
@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
塊。 當然,現在我仍然必須整理代碼...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.