我正在使用SpringBoot(java)开发应用程序,我想读取excell,但是当显示数据时excel时,年份不正确,并且excell中的输入“ boolean”在Java中显示String。
此刻,我把这条路变成了硬核。
控制器
@ApiOperation("Upload XLSX file")
@RequestMapping(method = RequestMethod.GET, value = "/insert", produces = MediaType.APPLICATION_JSON_VALUE)
public MyMessage create(@RequestBody MultipartFile multipart) {
try {
InputStream multipartFile = new FileInputStream("C:\\Users\\emartinp\\Desktop\\Nominacion.xlsx");
Workbook wb = WorkbookFactory.create(multipartFile);
Sheet sheet = wb.getSheetAt(0);
Header header = sheet.getHeader();
int rowsCount = sheet.getLastRowNum();
System.out.println("Total Number of Rows: " + (rowsCount + 1));
for (int i = 0; i <= rowsCount; i++) {
Row row = sheet.getRow(i);
int colCounts = row.getLastCellNum();
System.out.println("Total Number of Cols: " + colCounts);
for (int j = 0; j < colCounts; j++) {
Cell cell = row.getCell(j);
switch (cell.getCellType()) {
case Cell.CELL_TYPE_NUMERIC:
System.out.println("es num" + cell.getNumericCellValue());
break;
case Cell.CELL_TYPE_STRING:
System.out.println("es string" + cell.getStringCellValue());
break;
case Cell.CELL_TYPE_BOOLEAN:
System.out.println("boolean" + cell.getBooleanCellValue());
break;
}
// System.out.println("[" + i + "," + j + "]=" + cell.getStringCellValue());
}
}
} catch (Exception ex) {
System.out.println("Error " + ex);
} finally {
}
return null;
}
结果
您可以看到java的excell中的boolean类型为String ...
然后year
“ 1905” Java读为2003.0
Month
01 Java阅读2.0
Boolean
在excell Java读取的String中为false。
最后spam -1
...