diff --git a/apache-poi/pom.xml b/apache-poi/pom.xml index a114946c47..333339ed33 100644 --- a/apache-poi/pom.xml +++ b/apache-poi/pom.xml @@ -32,7 +32,7 @@ - 3.15 + 4.1.1 1.0.6 diff --git a/apache-poi/src/main/java/com/baeldung/poi/excel/ReadCellValueNotFormulaHelper.java b/apache-poi/src/main/java/com/baeldung/poi/excel/ReadCellValueNotFormulaHelper.java new file mode 100644 index 0000000000..f5117dcda9 --- /dev/null +++ b/apache-poi/src/main/java/com/baeldung/poi/excel/ReadCellValueNotFormulaHelper.java @@ -0,0 +1,84 @@ +package com.baeldung.poi.excel; + +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; + +import org.apache.poi.ss.usermodel.Cell; +import org.apache.poi.ss.usermodel.CellType; +import org.apache.poi.ss.usermodel.FormulaEvaluator; +import org.apache.poi.ss.usermodel.Row; +import org.apache.poi.ss.usermodel.Sheet; +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 Object getCellValueByFetchingLastCachedValue(String fileLocation, String cellLocation) throws IOException { + Object cellValue = new Object(); + 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()); + + if (cell.getCellType() == CellType.FORMULA) { + switch (cell.getCachedFormulaResultType()) { + case BOOLEAN: + cellValue = cell.getBooleanCellValue(); + break; + case NUMERIC: + cellValue = cell.getNumericCellValue(); + break; + case STRING: + cellValue = cell.getRichStringCellValue() + .getString(); + break; + default: + cellValue = null; + } + } + + workbook.close(); + return cellValue; + } + + public Object getCellValueByEvaluatingFormula(String fileLocation, String cellLocation) throws IOException { + Object cellValue; + FileInputStream inputStream = new FileInputStream(new File(fileLocation)); + + Workbook workbook = new XSSFWorkbook(inputStream); + + Sheet sheet = workbook.getSheetAt(0); + FormulaEvaluator evaluator = workbook.getCreationHelper() + .createFormulaEvaluator(); + + CellAddress cellAddress = new CellAddress(cellLocation); + + Row row = sheet.getRow(cellAddress.getRow()); + Cell cell = row.getCell(cellAddress.getColumn()); + + switch (evaluator.evaluateFormulaCell(cell)) { + case BOOLEAN: + cellValue = cell.getBooleanCellValue(); + break; + case NUMERIC: + cellValue = cell.getNumericCellValue(); + break; + case STRING: + cellValue = cell.getStringCellValue(); + break; + default: + cellValue = null; + } + + workbook.close(); + return cellValue; + } +} diff --git a/apache-poi/src/main/resources/test.xlsx b/apache-poi/src/main/resources/test.xlsx new file mode 100644 index 0000000000..64fe14f25b Binary files /dev/null and b/apache-poi/src/main/resources/test.xlsx differ diff --git a/apache-poi/src/test/java/com/baeldung/poi/excel/ReadCellValueNotFormulaUnitTest.java b/apache-poi/src/test/java/com/baeldung/poi/excel/ReadCellValueNotFormulaUnitTest.java new file mode 100644 index 0000000000..df7ab81874 --- /dev/null +++ b/apache-poi/src/test/java/com/baeldung/poi/excel/ReadCellValueNotFormulaUnitTest.java @@ -0,0 +1,39 @@ +package com.baeldung.poi.excel; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import java.io.IOException; +import java.net.URISyntaxException; +import java.nio.file.Paths; + +import org.junit.Before; +import org.junit.Test; + +public class ReadCellValueNotFormulaUnitTest { + + private ReadCellValueNotFormulaHelper readCellValueNotFormulaHelper; + private String fileLocation; + private static final String FILE_NAME = "test.xlsx"; + + @Before + public void setup() throws URISyntaxException { + fileLocation = Paths.get(ClassLoader.getSystemResource(FILE_NAME).toURI()).toString(); + readCellValueNotFormulaHelper = new ReadCellValueNotFormulaHelper(); + } + + @Test + public void testCachedValueMethod() throws IOException { + final double expectedResult = 7.0; + final Object cellValue = readCellValueNotFormulaHelper.getCellValueByFetchingLastCachedValue(fileLocation, "C2"); + + assertEquals(expectedResult, cellValue); + } + + @Test + public void testFormulaEvaluationMethod() throws IOException { + final double expectedResult = 7.0; + final Object cellValue = readCellValueNotFormulaHelper.getCellValueByEvaluatingFormula(fileLocation, "C2"); + + assertEquals(expectedResult, cellValue); + } +}