Apache Poi-读取Excel文件时,如何知道哪个单元出错

时间:2019-02-27 13:31:49

标签: java apache apache-poi

我正在尝试找到一种方法,以便在读取excel文件时知道错误的cellAddress。在每次读取cellAddress时不进行记录的情况下,如何知道哪个单元出错?以及如何继续阅读该行以发现是否存在其他错误?

我的代码:

final Person person = new Person();
List<Cell> cells = new ArrayList<Cell>();

int lastColumn = Math.max(row.getLastCellNum(), 43);

for (int colNum = 2; colNum < lastColumn; colNum++) {
    Cell cell = row.getCell(colNum, Row.CREATE_NULL_AS_BLANK);
    if ((cell == null) || cell.getCellType() == Cell.CELL_TYPE_STRING &&
            cell.getStringCellValue().trim().isEmpty()) {
        cell.setCellType(Cell.CELL_TYPE_BLANK);
    }
    cells.add(cell);
}

// I memorize by incrementing each time the value of the cellAddress in case there's an error
CellAddress cellAddress = null;
try {
    cellAddress = cells.get(1).getAddress();
    person.setTitle(cells.get(1).getStringCellValue());
    cellAddress = cells.get(2).getAddress();
    person.setLastName(cells.get(2).getStringCellValue());
    cellAddress = cells.get(3).getAddress();
    person.setFirstName(cells.get(3).getStringCellValue());
} catch (IllegalStateException e) {

    LOG.info("Cell in error : {}", cellAddress);
}

感谢您的帮助。我是Java的初学者。

0 个答案:

没有答案