1- package com .baeldung .poi .excel ;
1+ package com .baeldung .poi .excel . read . cellvalueandnotformula ;
22
33import java .io .File ;
44import java .io .FileInputStream ;
1313import org .apache .poi .ss .util .CellAddress ;
1414import org .apache .poi .xssf .usermodel .XSSFWorkbook ;
1515
16- public class ReadCellValueNotFormulaHelper {
16+ public class CellValueAndNotFormulaHelper {
1717
1818 public Object getCellValueByFetchingLastCachedValue (String fileLocation , String cellLocation ) throws IOException {
1919 Object cellValue = new Object ();
20- FileInputStream inputStream = new FileInputStream (new File (fileLocation ));
2120
21+ FileInputStream inputStream = new FileInputStream (new File (fileLocation ));
2222 Workbook workbook = new XSSFWorkbook (inputStream );
2323
2424 Sheet sheet = workbook .getSheetAt (0 );
2525
26- CellAddress cellReference = new CellAddress (cellLocation );
27-
28- Row row = sheet .getRow (cellReference .getRow ());
29- Cell cell = row .getCell (cellReference .getColumn ());
26+ CellAddress cellAddress = new CellAddress (cellLocation );
27+ Row row = sheet .getRow (cellAddress .getRow ());
28+ Cell cell = row .getCell (cellAddress .getColumn ());
3029
3130 if (cell .getCellType () == CellType .FORMULA ) {
3231 switch (cell .getCachedFormulaResultType ()) {
@@ -37,8 +36,7 @@ public Object getCellValueByFetchingLastCachedValue(String fileLocation, String
3736 cellValue = cell .getNumericCellValue ();
3837 break ;
3938 case STRING :
40- cellValue = cell .getRichStringCellValue ()
41- .getString ();
39+ cellValue = cell .getStringCellValue ();
4240 break ;
4341 default :
4442 cellValue = null ;
@@ -51,16 +49,15 @@ public Object getCellValueByFetchingLastCachedValue(String fileLocation, String
5149
5250 public Object getCellValueByEvaluatingFormula (String fileLocation , String cellLocation ) throws IOException {
5351 Object cellValue ;
54- FileInputStream inputStream = new FileInputStream (new File (fileLocation ));
5552
53+ FileInputStream inputStream = new FileInputStream (new File (fileLocation ));
5654 Workbook workbook = new XSSFWorkbook (inputStream );
5755
5856 Sheet sheet = workbook .getSheetAt (0 );
5957 FormulaEvaluator evaluator = workbook .getCreationHelper ()
6058 .createFormulaEvaluator ();
6159
6260 CellAddress cellAddress = new CellAddress (cellLocation );
63-
6461 Row row = sheet .getRow (cellAddress .getRow ());
6562 Cell cell = row .getCell (cellAddress .getColumn ());
6663
0 commit comments