Yellow colored cells does not return values as it displayed. It seems that the value is integer in excel, but actual value in POI is a double number. There's no formula, displaying options in the cell at all.
Here's the code that verifies acutal value.
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.1.2</version>
</dependency>
File file = new File("newSCIS.xlsx");
Workbook workbook = WorkbookFactory.create(file);
Sheet sheet = workbook.getSheetAt(0);
for (int i = 1; i <= 6; i ) {
Row row = sheet.getRow(i);
Cell cell = row.getCell(1);
cell.setCellType(CellType.STRING);
String value = cell.getStringCellValue();
System.out.printf(" s \n", value);
}
Why is this happening?
CodePudding user response:
If you are getting a decimal number you can simply parse it to an int using
Math.round(floatNum);
CodePudding user response:
If you are sure that the cell is numeric and the value doesn't contain decimal values, instead of:
cell.setCellType(CellType.STRING);
String value = cell.getStringCellValue();
you can do:
int value = (int) cell.getNumericCellValue();
System.out.print(value);
If you are not sure if the cell type is CellType.STRING
or CellType.NUMERIC
you can do:
public static void printValue(Cell cell) {
int value;
if (cell.getCellType() == CellType.NUMERIC) {
value = (int) cell.getNumericCellValue();
} else if (cell.getCellType() == CellType.STRING) {
value = (int) Double.parseDouble(cell.getStringCellValue());
} else {
throw new IllegalArgumentException(
String.format("%s not supported", cell.getCellType().name()));
}
System.out.print(value);
}