From 399c35aef26af18abca0a258b56160157eeea4d4 Mon Sep 17 00:00:00 2001 From: Sunil Jain Date: Sun, 22 Dec 2019 16:30:19 +0530 Subject: [PATCH] BAEL-3658: Read cell value rather than the formula that is evaluating it --- apache-poi/pom.xml | 2 +- .../excel/ReadCellValueNotFormulaHelper.java | 84 ++++++++++++++++++ apache-poi/src/main/resources/test.xlsx | Bin 0 -> 4850 bytes .../ReadCellValueNotFormulaUnitTest.java | 39 ++++++++ 4 files changed, 124 insertions(+), 1 deletion(-) create mode 100644 apache-poi/src/main/java/com/baeldung/poi/excel/ReadCellValueNotFormulaHelper.java create mode 100644 apache-poi/src/main/resources/test.xlsx create mode 100644 apache-poi/src/test/java/com/baeldung/poi/excel/ReadCellValueNotFormulaUnitTest.java 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 0000000000000000000000000000000000000000..64fe14f25b1f43c90b7059c1354c575fa959cdd2 GIT binary patch literal 4850 zcmai22|Sc-_Z~Z83}eYMmJC8d_I(#7J0;n(jBSW8mSii&7GoEcosgI!F@y+Hwia2+ zzD`I&Wcg3^V9|K^ zpn}Vv4W;gKk#B-<+9E892gGA)XSXM*2w4DmFJ6Lb=sxacAc!Mpn7DgVzNilbu&sTo-|fAQ{)+5 zctlTgUztY!fMfGhw^>W?d;9KB%&$lw#vNxGwL-dMYX$9E(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iH?y746u==vGI2S#G(DEna9 z%BT)?J2xw_o)q^lJM=cgb?9cq~*) zJG&EjF5M&}W^E>WoB1*!9*qQH`1cG(OL*c}xc)8vG=NbOAc_Hk_;cMd^L0!tcq}RC zMbG`9UW-q?%)PRGWyLIsHY;ssBfPXKE7*zQyCbjYgKj?uILlHS2g&eOvkHLL)LKla z?zBTEAmFL6xO`7GLR0P83nr)N2vyynS>YPf7&pJ!6ANX`?tqi zIZbY4R}GSe7127ZEPv&>`y63{JQZ+%u#1Pq9tDN@NSv?|Jbu&;~1EwM1w=D+cyx%)40kQhQLq z(&c8>t1a5xz6YmY0ZQIgb)xsYn-Ck`TJnheB|ItWbqh2NHSPY&!3SfDQ=9c}R+W@e zJShwwU8R!|9NMe^6ulE^{`Ll1v~JH-@F+S%9|UvA1f~7R^9qrj-u`z_ryeo5OMY+S|lN52K+ulI6e{+ z&k+7SrKGQWrEKT(cP2E-ZYx4kBi$zM_a>qPV?;Bf^+ z94LMpu|p65{~Ry=1UN1(h(hVNDNr8){6E?BC&KZTL=+*vjfdtJ!v9HWc%A{=#Ccu4fXzsbG%}S6!hDs2<{vsq(2uPPrARx{xw05 Z{+IGLfl&~qVgTR-;gw6ELQN3y?q5xn+ExGn literal 0 HcmV?d00001 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); + } +}