CELL_TYPE_ERROR有什么用途?

3

我希望了解apache poi中单元格类型CELL_TYPE_ERROR的用法。我尝试了以下代码,但是没有看到错误。

Workbook wb = new XSSFWorkbook();
Row row = sheet1.createRow(0);
Cell cell = row.createCell(0);

cell.setCellType(Cell.CELL_TYPE_ERROR);
cell.setCellValue(234);
System.out.println("error cell value-"+ cell.getNumericCellValue()); //this prints 234.0

此外,我想了解如果我们不手动设置单元格类型,它是否可以为错误类型。

你尝试将工作簿写入文件并使用Excel或Libre-/OpenOffice打开它了吗?我猜,如果有的话,手动将单元格类型设置为错误会在那里可见。除此之外,我认为目的是您可以通过编程方式检查计算错误。例如,如果您有一个带有公式=6/0的单元格的Excel表格,则此单元格应为公式单元格。然后,如果您复制此单元格并粘贴其值,则应该有一个错误单元格(任何其他单元格类型都不适用于此处)。 - Kulu Limpa
我尝试将带有错误单元格的工作簿写入文件,并使用Excel打开它。在那里,错误单元格不会引起任何问题。此外,我创建了一个公式= 1/0并复制了该单元格,新创建的单元格类型仍然是公式。 - Naresh Vavilala
1个回答

7

请查看代码中的注释。

import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.xssf.usermodel.*;
import org.apache.poi.ss.usermodel.*;

import java.io.FileOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

class CellTypeErrorTest {

 public static void main(String[] args) {
  Workbook wb = new XSSFWorkbook();
  Sheet sheet = wb.createSheet("Sheet1");
  Row row = sheet.createRow(0);
  Cell cell = row.createCell(0);

  //The following works, but it makes no sense, because the cell will have no real content.
  //If you wants to see, how this will be shown into the Workbook, then comment out the
  //following code that overwrites the Cell with numeric content.
  cell.setCellType(Cell.CELL_TYPE_ERROR);
  cell.setCellErrorValue(FormulaError.DIV0.getCode());
  System.out.println("error cell value-"+ FormulaError.forInt(cell.getErrorCellValue()).getString());

  //If you put real content in the cell, then the CELL_TYPE_ERROR goes away, if the content 
  //not produces ERROR.
  cell.setCellValue(234);
  System.out.println(cell.getCellType()); //0 == CELL_TYPE_NUMERIC

  //If you put a Formula in the Cell, it will not be evaluated automatically.
  //So there is no error, even the formula will produce error if it will be evaluated.
  cell = row.createCell(1);
  cell.setCellFormula("1/0");
  System.out.println(cell.getCellType()); //2 == CELL_TYPE_FORMULA

  //It you need to check if a formula produces error, then you have to evaluate it.
  FormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator();
  CellValue cellValue = evaluator.evaluate(cell);
  System.out.println(cellValue.getCellType()); //5 == CELL_TYPE_ERROR
  if (cellValue.getCellType() == Cell.CELL_TYPE_ERROR) {
   System.out.println("error cell value-"+ FormulaError.forInt(cellValue.getErrorValue()).getString());
  }

  try {
   FileOutputStream fileOut = new FileOutputStream("workbook.xlsx");
   wb.write(fileOut);
   fileOut.close();
  } catch (FileNotFoundException fnfex) {
  } catch (IOException ioex) {
  }

 }
}

结论:

使用Cell.CELL_TYPE_ERROR可以检测单元格内容是否产生错误。手动设置它大多没有意义。

对于没有实际内容的单元格,可以使用cell.setCellErrorValue手动设置它。但这大多没有意义,因为如果单元格获得了实际内容,而且不会产生错误,则CellType会自动更改为另一种类型。

POI不会自动评估单元格公式。具有公式的单元格的CellTypes始终为Cell.CELL_TYPE_FORMULA。因此,要检查单元格公式是否产生错误,我们必须手动进行评估,然后检查评估后的CellValue的CellType。请参见:http://poi.apache.org/spreadsheet/eval.html

问候

Axel


感谢您的回答。所以,如果我理解正确,只有在我们手动将单元格类型设置为“ERROR”并且在评估产生错误的公式的少数情况下,单元格类型才是错误的。此外,对于不是使用POI创建的工作簿,这种单元格类型是否具有任何意义?我认为这种单元格类型仅在apche poi api的上下文中有用。 - Naresh Vavilala
嗯,我的结论与你的相反。我已经将我的结论写入了我的答案中。 - Axel Richter
感谢您澄清这个问题! - Naresh Vavilala

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接