So my Java Code is like this to read excel sheet cell values, but I could not be able to get the value of date as string instead it is auto corrected to numbers. Also note that all these values of excel are calculated by formula at their respective cell. What is missing?
public class DatasheetReader {
public static void main(String args[]) throws Exception {
String path = "D:\\Workspace\\Analytics\\TestDataSheets\\IRPTestData.xlsx";
String sheet = "CompleteSimulationData";
getCellData(path, sheet);
}
public static Object[][] getCellData(String datSheetPath, String sheetName)throws Exception {
FileInputStream fis = new FileInputStream(new java.io.File(datSheetPath));
XSSFWorkbook workbook = new XSSFWorkbook(fis);
XSSFSheet sheet = workbook.getSheet(sheetName);
int rowCount = sheet.getPhysicalNumberOfRows();
int columnCount = sheet.getRow(0).getPhysicalNumberOfCells();
// System.out.println(rowCount);
// System.out.println(columnCount);
Object[][] cellData = new Object[rowCount][columnCount];
Object[][] dataProviderArray = new Object[rowCount - 1][columnCount];
for (int i = 0; i < rowCount; i++) {
System.out.println(" ");
for (int j = 0; j < columnCount; j++) {
// IF - for blanks in excel
if (i != 0) {
// IF - for not null values
if (sheet.getRow(i).getCell(j) != null) {
Cell cell = sheet.getRow(i).getCell(j);
int cellType = cell.getCachedFormulaResultType();
if(cellType==Cell.CELL_TYPE_NUMERIC){
System.out.println(cell.getNumericCellValue()+" "+cell.getCachedFormulaResultType());
}else if(cellType==Cell.CELL_TYPE_STRING){
System.out.println(cell.getRichStringCellValue()+" "+cell.getCachedFormulaResultType());
}
} else {
cellData[i][j] = "";
}
} else {
continue;
}
//System.out.println(cellData[i][j]);
}
}
for (int i = 1; i < rowCount; i++) {
for (int j = 0; j < columnCount; j++) {
//System.out.print(cellData[i][j] + " ");
dataProviderArray[i - 1][j] = cellData[i][j];
}
}
workbook.close();
return dataProviderArray;
}}
All these values are cached values calculated from formula.
All values are getting as per expectations except the date, it come as 42887.0
instead of Jun-2017
basically all values are not alone values in excel cell rather these are values calculated from formulas, for date POI returns its type as CELL_TYPE_NUMERIC
, Don't understand how to handle this, as unable to use DataFormat of POI because then all values which I'm going to read getting as formula?