BAEL-3658: Read cell value rather than the formula that is evaluating it
This commit is contained in:
parent
7f460e45f0
commit
d2186b0dc7
|
@ -1,4 +1,4 @@
|
||||||
package com.baeldung.poi.excel;
|
package com.baeldung.poi.excel.read.cellvalueandnotformula;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileInputStream;
|
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.ss.util.CellAddress;
|
||||||
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
||||||
|
|
||||||
public class ReadCellValueNotFormulaHelper {
|
public class CellValueAndNotFormulaHelper {
|
||||||
|
|
||||||
public Object getCellValueByFetchingLastCachedValue(String fileLocation, String cellLocation) throws IOException {
|
public Object getCellValueByFetchingLastCachedValue(String fileLocation, String cellLocation) throws IOException {
|
||||||
Object cellValue = new Object();
|
Object cellValue = new Object();
|
||||||
FileInputStream inputStream = new FileInputStream(new File(fileLocation));
|
|
||||||
|
|
||||||
|
FileInputStream inputStream = new FileInputStream(new File(fileLocation));
|
||||||
Workbook workbook = new XSSFWorkbook(inputStream);
|
Workbook workbook = new XSSFWorkbook(inputStream);
|
||||||
|
|
||||||
Sheet sheet = workbook.getSheetAt(0);
|
Sheet sheet = workbook.getSheetAt(0);
|
||||||
|
|
||||||
CellAddress cellReference = new CellAddress(cellLocation);
|
CellAddress cellAddress = new CellAddress(cellLocation);
|
||||||
|
Row row = sheet.getRow(cellAddress.getRow());
|
||||||
Row row = sheet.getRow(cellReference.getRow());
|
Cell cell = row.getCell(cellAddress.getColumn());
|
||||||
Cell cell = row.getCell(cellReference.getColumn());
|
|
||||||
|
|
||||||
if (cell.getCellType() == CellType.FORMULA) {
|
if (cell.getCellType() == CellType.FORMULA) {
|
||||||
switch (cell.getCachedFormulaResultType()) {
|
switch (cell.getCachedFormulaResultType()) {
|
||||||
|
@ -37,8 +36,7 @@ public class ReadCellValueNotFormulaHelper {
|
||||||
cellValue = cell.getNumericCellValue();
|
cellValue = cell.getNumericCellValue();
|
||||||
break;
|
break;
|
||||||
case STRING:
|
case STRING:
|
||||||
cellValue = cell.getRichStringCellValue()
|
cellValue = cell.getStringCellValue();
|
||||||
.getString();
|
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
cellValue = null;
|
cellValue = null;
|
||||||
|
@ -51,8 +49,8 @@ public class ReadCellValueNotFormulaHelper {
|
||||||
|
|
||||||
public Object getCellValueByEvaluatingFormula(String fileLocation, String cellLocation) throws IOException {
|
public Object getCellValueByEvaluatingFormula(String fileLocation, String cellLocation) throws IOException {
|
||||||
Object cellValue;
|
Object cellValue;
|
||||||
FileInputStream inputStream = new FileInputStream(new File(fileLocation));
|
|
||||||
|
|
||||||
|
FileInputStream inputStream = new FileInputStream(new File(fileLocation));
|
||||||
Workbook workbook = new XSSFWorkbook(inputStream);
|
Workbook workbook = new XSSFWorkbook(inputStream);
|
||||||
|
|
||||||
Sheet sheet = workbook.getSheetAt(0);
|
Sheet sheet = workbook.getSheetAt(0);
|
||||||
|
@ -60,7 +58,6 @@ public class ReadCellValueNotFormulaHelper {
|
||||||
.createFormulaEvaluator();
|
.createFormulaEvaluator();
|
||||||
|
|
||||||
CellAddress cellAddress = new CellAddress(cellLocation);
|
CellAddress cellAddress = new CellAddress(cellLocation);
|
||||||
|
|
||||||
Row row = sheet.getRow(cellAddress.getRow());
|
Row row = sheet.getRow(cellAddress.getRow());
|
||||||
Cell cell = row.getCell(cellAddress.getColumn());
|
Cell cell = row.getCell(cellAddress.getColumn());
|
||||||
|
|
|
@ -1,39 +0,0 @@
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -0,0 +1,39 @@
|
||||||
|
package com.baeldung.poi.excel.read.cellvalueandnotformula;
|
||||||
|
|
||||||
|
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 CellValueAndNotFormulaUnitTest {
|
||||||
|
|
||||||
|
private CellValueAndNotFormulaHelper readCellValueAndNotFormulaHelper;
|
||||||
|
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();
|
||||||
|
readCellValueAndNotFormulaHelper = new CellValueAndNotFormulaHelper();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenExcelCell_whenReadCellValueByLastCachedValue_thenProduceCorrectResult() throws IOException {
|
||||||
|
final double expectedResult = 7.0;
|
||||||
|
final Object cellValue = readCellValueAndNotFormulaHelper.getCellValueByFetchingLastCachedValue(fileLocation, "C2");
|
||||||
|
|
||||||
|
assertEquals(expectedResult, cellValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenExcelCell_whenReadCellValueByEvaluatingFormula_thenProduceCorrectResult() throws IOException {
|
||||||
|
final double expectedResult = 7.0;
|
||||||
|
final Object cellValue = readCellValueAndNotFormulaHelper.getCellValueByEvaluatingFormula(fileLocation, "C2");
|
||||||
|
|
||||||
|
assertEquals(expectedResult, cellValue);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue