BAEL-3658: Read cell value rather than the formula that is evaluating it

This commit is contained in:
Sunil Jain
2019-12-26 18:01:37 +05:30
parent 7f460e45f0
commit d2186b0dc7
3 changed files with 47 additions and 50 deletions
@@ -1,4 +1,4 @@
package com.baeldung.poi.excel;
package com.baeldung.poi.excel.read.cellvalueandnotformula;
import java.io.File;
import java.io.FileInputStream;
@@ -13,20 +13,19 @@ import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.util.CellAddress;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class ReadCellValueNotFormulaHelper {
public class CellValueAndNotFormulaHelper {
public Object getCellValueByFetchingLastCachedValue(String fileLocation, String cellLocation) throws IOException {
Object cellValue = new Object();
FileInputStream inputStream = new FileInputStream(new File(fileLocation));
FileInputStream inputStream = new FileInputStream(new File(fileLocation));
Workbook workbook = new XSSFWorkbook(inputStream);
Sheet sheet = workbook.getSheetAt(0);
CellAddress cellReference = new CellAddress(cellLocation);
Row row = sheet.getRow(cellReference.getRow());
Cell cell = row.getCell(cellReference.getColumn());
CellAddress cellAddress = new CellAddress(cellLocation);
Row row = sheet.getRow(cellAddress.getRow());
Cell cell = row.getCell(cellAddress.getColumn());
if (cell.getCellType() == CellType.FORMULA) {
switch (cell.getCachedFormulaResultType()) {
@@ -37,8 +36,7 @@ public class ReadCellValueNotFormulaHelper {
cellValue = cell.getNumericCellValue();
break;
case STRING:
cellValue = cell.getRichStringCellValue()
.getString();
cellValue = cell.getStringCellValue();
break;
default:
cellValue = null;
@@ -51,8 +49,8 @@ public class ReadCellValueNotFormulaHelper {
public Object getCellValueByEvaluatingFormula(String fileLocation, String cellLocation) throws IOException {
Object cellValue;
FileInputStream inputStream = new FileInputStream(new File(fileLocation));
FileInputStream inputStream = new FileInputStream(new File(fileLocation));
Workbook workbook = new XSSFWorkbook(inputStream);
Sheet sheet = workbook.getSheetAt(0);
@@ -60,7 +58,6 @@ public class ReadCellValueNotFormulaHelper {
.createFormulaEvaluator();
CellAddress cellAddress = new CellAddress(cellLocation);
Row row = sheet.getRow(cellAddress.getRow());
Cell cell = row.getCell(cellAddress.getColumn());