首页 > 解决方案 > 使用 Apache POI 确认 Excel 中的正确单元格

问题描述

我需要验证 Excel 单元格是否具有正确的值才能继续,我对许多字符串和单元格进行了验证。我需要验证四个单元格。它正在工作。

有什么方法可以用更少的线做同样的事情吗?最好的方法是什么?

单元格必须有 b3=Número do Cartão ,c3= Número do Pedido ,d3= Motivo do Cancelamento ,e3 = Observação

像这样: Excel 行

private boolean validaLinha(InputStream arquivoCancelamento) throws IOException{

    XSSFWorkbook wb = new XSSFWorkbook(arquivoCancelamento);
    Sheet sheet = wb.getSheetAt(0);
    FormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator();
    String celulaB3 = "N\u00FAmero do cart\u00E3o ";
    String celulaC3 = "N\u00FAmero Pedido";
    String celulaD3 = "Motivo de Cancelamento";
    String celulaE3 = "observa\u00E7\u00E3o";

    boolean val = false;

    CellReference cellReferenceB3 = new CellReference("B3");
    CellReference cellReferenceC3= new CellReference("C3");
    CellReference cellReferenceD3 = new CellReference("D3");
    CellReference cellReferenceE3 = new CellReference("E3");
    CellReference cellReferenceB4 = new CellReference("B4");

    Row row = sheet.getRow(cellReferenceC3.getRow());

    Cell cellB3 = row.getCell(cellReferenceB3.getCol());
    Cell cellC3 = row.getCell(cellReferenceC3.getCol());
    Cell cellD3 = row.getCell(cellReferenceD3.getCol());
    Cell cellE3 = row.getCell(cellReferenceE3.getCol());
    Cell cellB4 = row.getCell(cellReferenceB4.getCol());


    if (cellReferenceB3!=null && celulaB3.equals(cellB3.getStringCellValue() )) {
        return true;}
        if (cellReferenceC3!=null && celulaC3.equals(cellC3.getStringCellValue() )) {
            return true;}

            if (cellReferenceD3!=null && celulaC3.equals(cellD3.getStringCellValue() )) {
                return true;}
                if (cellReferenceE3!=null && celulaC3.equals(cellE3.getStringCellValue() )) {
                    return true;}
                    if (cellReferenceB4!=null || cellB4.getCellType() != Cell.CELL_TYPE_BLANK) {
                        return true;}
    else {
    return false;
    }
}

标签: javaapache-poi

解决方案


推荐阅读