I have a requirement to copy Excel row from one sheet to another. But after copying instead of actual values, I am getting different values. There is no problem with cells having string values, but with numeric values. Excel cell can contain any type of value. My code should accept all of them and copy the same in another sheet. Please help me in this case.
if(row.getCell(getLastCell)!=null && ((row.getCell(getLastCell).toString().equalsIgnoreCase(FAIL))|| (row.getCell(getLastCell).toString().equalsIgnoreCase("Invalid CR Statement"))))
{
failuresRow=failuresSheet.createRow(createNewRow++);
for(int ii=0;ii<=getLastCell;ii++)
{
failuresCell=failuresRow.createCell(ii);
failuresCell.setCellValue(row.getCell(ii)+"");
failuresCell.setCellType(row.getCell(ii).getCellType());
failuresCell.setCellStyle(row.getCell(ii).getCellStyle());
}
}
I know that row.getCell(ii)+"" is to convert type Cell to string, but I want to set setCellValue to accept cell which can have any type of data(eg: Date, Bollean, String, Number, .........)
I have tried using DataFormatter and in some other way also as below, but no use.
failuresCell.setCellValue(df.formatCellValue(row.getCell(ii)));
switch (cell.getCellType()) {
case Cell.CELL_TYPE_STRING:
failuresCell.setCellValue(cell.getRichStringCellValue().getString());
break;
case Cell.CELL_TYPE_NUMERIC:
if (DateUtil.isCellDateFormatted(cell)) {
failuresCell.setCellValue(cell.getDateCellValue());
} else {
failuresCell.setCellValue(cell.getNumericCellValue());
}
break;
case Cell.CELL_TYPE_BOOLEAN:
failuresCell.setCellValue(cell.getBooleanCellValue());
break;
case Cell.CELL_TYPE_FORMULA:
failuresCell.setCellValuec(cell.getCellFormula());
break;
default:
// some code
}
If cell have the numeric value 9200278, getting output as 1717. I don't understand where I am doing wrong. Please help me in this case.