BAEL-3658: Read cell value rather than the formula that is evaluating it
This commit is contained in:
parent
5508f6f43e
commit
399c35aef2
|
@ -32,7 +32,7 @@
|
|||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<poi.version>3.15</poi.version>
|
||||
<poi.version>4.1.1</poi.version>
|
||||
<jexcel.version>1.0.6</jexcel.version>
|
||||
</properties>
|
||||
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
Binary file not shown.
|
@ -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);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue