Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/372.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用Apache Poi从Excel工作表中获取单元格值_Java_Apache Poi - Fatal编程技术网

Java 使用Apache Poi从Excel工作表中获取单元格值

Java 使用Apache Poi从Excel工作表中获取单元格值,java,apache-poi,Java,Apache Poi,如何在java中使用poi获取单元格值 我的代码是这样的 String cellformula_total__percentage= "(1-E" + (rowIndex + 2) + "/" + "D" + (rowIndex + 2) + ")*100"; cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC); cell.setCellStyle(this.valueRightAlignStyleLightBlueBackground); cell.set

如何在java中使用poi获取单元格值

我的代码是这样的

String cellformula_total__percentage= "(1-E" + (rowIndex + 2) + "/" + "D" + (rowIndex + 2) + ")*100";
cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
cell.setCellStyle(this.valueRightAlignStyleLightBlueBackground);
cell.setCellFormula("abs(" + cellformula_total__percentage + ")");
但如果在这种情况下存在错误,我如何检查我的单元格值是否包含错误值,如#DIV/0!我怎样才能用N/A替换它呢

    for(Row row : sheet) {          
        for(Cell cell : row) {              
            System.out.print(cell.getStringCellValue());

        }
    }       
对于特定类型的单元格,您可以尝试:

switch (cell.getCellType()) {
case Cell.CELL_TYPE_STRING:
    cellValue = cell.getStringCellValue();
    break;

case Cell.CELL_TYPE_FORMULA:
    cellValue = cell.getCellFormula();
    break;

case Cell.CELL_TYPE_NUMERIC:
    if (DateUtil.isCellDateFormatted(cell)) {
        cellValue = cell.getDateCellValue().toString();
    } else {
        cellValue = Double.toString(cell.getNumericCellValue());
    }
    break;

case Cell.CELL_TYPE_BLANK:
    cellValue = "";
    break;

case Cell.CELL_TYPE_BOOLEAN:
    cellValue = Boolean.toString(cell.getBooleanCellValue());
    break;

}

您必须使用FormulaEvaluator,如图所示。这将返回一个值,该值要么是单元格中的值,要么是公式的结果(如果单元格包含这样的公式):

FileInputStream fis = new FileInputStream("/somepath/test.xls");
Workbook wb = new HSSFWorkbook(fis); //or new XSSFWorkbook("/somepath/test.xls")
Sheet sheet = wb.getSheetAt(0);
FormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator();

// suppose your formula is in B3
CellReference cellReference = new CellReference("B3"); 
Row row = sheet.getRow(cellReference.getRow());
Cell cell = row.getCell(cellReference.getCol()); 

if (cell!=null) {
    switch (evaluator.evaluateFormulaCell(cell)) {
        case Cell.CELL_TYPE_BOOLEAN:
            System.out.println(cell.getBooleanCellValue());
            break;
        case Cell.CELL_TYPE_NUMERIC:
            System.out.println(cell.getNumericCellValue());
            break;
        case Cell.CELL_TYPE_STRING:
            System.out.println(cell.getStringCellValue());
            break;
        case Cell.CELL_TYPE_BLANK:
            break;
        case Cell.CELL_TYPE_ERROR:
            System.out.println(cell.getErrorCellValue());
            break;

        // CELL_TYPE_FORMULA will never occur
        case Cell.CELL_TYPE_FORMULA: 
            break;
    }
}
如果您需要精确的contant(即如果单元格包含公式,则为formla),则会显示此内容

编辑: 添加了一些示例来帮助您

首先,你得到一个单元格(只是一个例子)

如果只想使用公式将值设置到单元格中(不知道结果):

 String formula ="ABS((1-E"+(rowIndex + 2)+"/D"+(rowIndex + 2)+")*100)";    
 cell.setCellFormula(formula);    
 cell.setCellStyle(this.valueRightAlignStyleLightBlueBackground);
如果要在单元格中出现错误时更改消息,则必须更改公式,如

IF(ISERR(ABS((1-E3/D3)*100));"N/A"; ABS((1-E3/D3)*100))
(此公式检查评估是否返回错误,然后显示字符串“N/A”,如果不是错误,则显示评估)

如果要获得公式对应的值,则必须使用计算器。

希望这有帮助,

Guillaume

据我所知,如果单元格中没有公式,则不必“必须”使用FormulaEvaluator:)如果单元格包含文本或数值,则可以通过
cell.getStringCellValue()获取它们
cell.getNumericCellValue()。您的答案适用于单元格包含公式的情况,但没有迹象表明OP@posdef:你说得对,我没有必要。但是,如果单元格不包含公式,则“evaluateFormulaCell”方法仍会返回该值。我发现调用这个方法比执行特殊条件检查更简单,因为这样我总是得到单元格的值。然而,如果你确信你没有公式,这确实是无效的。@PATRY:你有一个很好的观点。。。我指出这一点只是为了确保没有误解。@PARTY。。感谢您的帮助,但如果存在公式的单元格类型,并且在对该单元格公式求值后,它将包含类似DIV/0的错误值!错误:如何知道单元格包含此错误以及如何用“N/A”替换此错误?如果发生错误,单元格的类型为cell.cell\u type\u error。可以通过getErrorCellValue()方法获取错误的值。在此之后,这取决于您的需要:如果您只需要一条标准错误消息(“N/A”),那么只需检查内容的性质即可。如果您需要不同的错误消息,请使用HashMap将excel错误消息映射到您自己的错误消息(这只是一个示例)。。。
IF(ISERR(ABS((1-E3/D3)*100));"N/A"; ABS((1-E3/D3)*100))