Skip to content

Commit dcca5d8

Browse files
authored
Merge pull request eugenp#8414 from SunilJain95/master
BAEL-3658: Read actual cell value rather than the formula that is evaluating it
2 parents 04a571e + 2835399 commit dcca5d8

File tree

4 files changed

+123
-1
lines changed

4 files changed

+123
-1
lines changed

apache-poi/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
</dependencies>
3333

3434
<properties>
35-
<poi.version>3.15</poi.version>
35+
<poi.version>4.1.1</poi.version>
3636
<jexcel.version>1.0.6</jexcel.version>
3737
</properties>
3838

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package com.baeldung.poi.excel.read.cellvalueandnotformula;
2+
3+
import java.io.File;
4+
import java.io.FileInputStream;
5+
import java.io.IOException;
6+
7+
import org.apache.poi.ss.usermodel.Cell;
8+
import org.apache.poi.ss.usermodel.CellType;
9+
import org.apache.poi.ss.usermodel.FormulaEvaluator;
10+
import org.apache.poi.ss.usermodel.Row;
11+
import org.apache.poi.ss.usermodel.Sheet;
12+
import org.apache.poi.ss.usermodel.Workbook;
13+
import org.apache.poi.ss.util.CellAddress;
14+
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
15+
16+
public class CellValueAndNotFormulaHelper {
17+
18+
public Object getCellValueByFetchingLastCachedValue(String fileLocation, String cellLocation) throws IOException {
19+
Object cellValue = new Object();
20+
21+
FileInputStream inputStream = new FileInputStream(new File(fileLocation));
22+
Workbook workbook = new XSSFWorkbook(inputStream);
23+
24+
Sheet sheet = workbook.getSheetAt(0);
25+
26+
CellAddress cellAddress = new CellAddress(cellLocation);
27+
Row row = sheet.getRow(cellAddress.getRow());
28+
Cell cell = row.getCell(cellAddress.getColumn());
29+
30+
if (cell.getCellType() == CellType.FORMULA) {
31+
switch (cell.getCachedFormulaResultType()) {
32+
case BOOLEAN:
33+
cellValue = cell.getBooleanCellValue();
34+
break;
35+
case NUMERIC:
36+
cellValue = cell.getNumericCellValue();
37+
break;
38+
case STRING:
39+
cellValue = cell.getStringCellValue();
40+
break;
41+
default:
42+
cellValue = null;
43+
}
44+
}
45+
46+
workbook.close();
47+
return cellValue;
48+
}
49+
50+
public Object getCellValueByEvaluatingFormula(String fileLocation, String cellLocation) throws IOException {
51+
Object cellValue = new Object();
52+
53+
FileInputStream inputStream = new FileInputStream(new File(fileLocation));
54+
Workbook workbook = new XSSFWorkbook(inputStream);
55+
56+
Sheet sheet = workbook.getSheetAt(0);
57+
FormulaEvaluator evaluator = workbook.getCreationHelper()
58+
.createFormulaEvaluator();
59+
60+
CellAddress cellAddress = new CellAddress(cellLocation);
61+
Row row = sheet.getRow(cellAddress.getRow());
62+
Cell cell = row.getCell(cellAddress.getColumn());
63+
64+
if (cell.getCellType() == CellType.FORMULA) {
65+
switch (evaluator.evaluateFormulaCell(cell)) {
66+
case BOOLEAN:
67+
cellValue = cell.getBooleanCellValue();
68+
break;
69+
case NUMERIC:
70+
cellValue = cell.getNumericCellValue();
71+
break;
72+
case STRING:
73+
cellValue = cell.getStringCellValue();
74+
break;
75+
default:
76+
cellValue = null;
77+
}
78+
}
79+
80+
workbook.close();
81+
return cellValue;
82+
}
83+
}
4.74 KB
Binary file not shown.
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.baeldung.poi.excel.read.cellvalueandnotformula;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import java.io.IOException;
6+
import java.net.URISyntaxException;
7+
import java.nio.file.Paths;
8+
9+
import org.junit.Before;
10+
import org.junit.Test;
11+
12+
public class CellValueAndNotFormulaUnitTest {
13+
14+
private CellValueAndNotFormulaHelper readCellValueAndNotFormulaHelper;
15+
private String fileLocation;
16+
private static final String FILE_NAME = "test.xlsx";
17+
18+
@Before
19+
public void setup() throws URISyntaxException {
20+
fileLocation = Paths.get(ClassLoader.getSystemResource(FILE_NAME).toURI()).toString();
21+
readCellValueAndNotFormulaHelper = new CellValueAndNotFormulaHelper();
22+
}
23+
24+
@Test
25+
public void givenExcelCell_whenReadCellValueByLastCachedValue_thenProduceCorrectResult() throws IOException {
26+
final double expectedResult = 7.0;
27+
final Object cellValue = readCellValueAndNotFormulaHelper.getCellValueByFetchingLastCachedValue(fileLocation, "C2");
28+
29+
assertEquals(expectedResult, cellValue);
30+
}
31+
32+
@Test
33+
public void givenExcelCell_whenReadCellValueByEvaluatingFormula_thenProduceCorrectResult() throws IOException {
34+
final double expectedResult = 7.0;
35+
final Object cellValue = readCellValueAndNotFormulaHelper.getCellValueByEvaluatingFormula(fileLocation, "C2");
36+
37+
assertEquals(expectedResult, cellValue);
38+
}
39+
}

0 commit comments

Comments
 (0)