简体   繁体   中英

In Java,using Apache POI, how can I pull cell data as is, not the formula?

Some cells have data concatenated from other cells. In trying to write a csv file from the parent file, some cells are written to the csv file by their formula not, the value of said formula. I need the resulting value, not the formula itself.

Try this:

private Object getCellValue(HSSFCell cell) {
    if (cell == null) { return null; }
    if (cell.getCellType() == Cell.CELL_TYPE_FORMULA) {
        switch (cell.getCachedFormulaResultType()) {
            case Cell.CELL_TYPE_NUMERIC: return cell.getNumericCellValue();
            case Cell.CELL_TYPE_STRING: return cell.getStringCellValue().replaceAll("'", "");
        }
    }
    return null;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM