首页 > 解决方案 > Java 无法正确读取 excel 文件并且无法正确识别 vars

问题描述

我正在使用SpringBoot(java)做应用程序,我想读取excel,但是当显示excel数据时,年份不正确,并且excel中的输入“布尔”显示java中的字符串。

那一刻我把路径铁杆。

控制器

@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 read是2003.0 Month01 Java read 2.0 Booleanfalse in Excel中Java read String。

最后spam -1...

标签: javaexcel

解决方案


推荐阅读