Jakarta POI is a set of Java apis for accessing Documents in Microsoft format. The Jakarta POI is made up of many components, including an HSSF for manipulating Excel format files. When using POI to import Excel forms, we often encounter the problem of format conversion. The following error occurred when importing the Excel table in the project today:

ERROR 7248 — [iO-8082-exec-10] O.A.C.C.C. [.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.IllegalStateException: Cannot get a STRING value from a NUMERIC cell] with root cause java.lang.IllegalStateException: Cannot get a STRING value from a NUMERIC cell

The Number in the Excel table is set to String. The Number in the Excel table is changed to Number after importing the Excel table.

Cell.getnumericcellvalue () returns a double integer.parseInt can only be converted to string. You can force the format of the cell to be String cell.setCellType(1); // Set it to String String str_temp = cell.getStringCellValue().trim(); Inetger a = integer.parseint (str_temp); / / conversion

Therefore, modify according to this idea and solve the error problem. The modified code is as follows

The correct example program is as follows:

public static List<JObLevel> parseFile2list(MultipartFile file) throws IOException{
    List<JObLevel> jObLevels = new ArrayList<>();
    // Create a document object
    HSSFWorkbook workbook = new HSSFWorkbook(file.getInputStream());
    // Create an Excel form
    HSSFSheet sheet = workbook.getSheetAt(0);
    int physicalNumberofRows = sheet.getPhysicalNumberOfRows();
    for (int i = 1; i < physicalNumberofRows; i++){ HSSFRow row = sheet.getRow(i); HSSFCell c0 = row.getCell(0);
    double numericCellValue = c0.getNumericCellValue();/ / returns a double
    JObLevel jl = new JObLevel();
    c0.setCellType(CellType.STRING);// Set to String
    String str_temp = c0.getStringCellValue();/ / value
    Integer int_temp = Integer.parseInt(str_temp);/ / conversion
    jl.setId(int_temp);
    }
    return jObLevels;
}
Copy the code

Although the error was resolved, it felt like it wasn’t the best solution because it was too cumbersome to get a String value and then convert it. So after consulting colleagues around, using the method of cast, directly into int type.

The sample program is as follows:

public static List<JObLevel> parseFile2list(MultipartFile file) throws IOException{
    List<JObLevel> jObLevels = new ArrayList<>();
    // Create a document object
    HSSFWorkbook workbook = new HSSFWorkbook(file.getInputStream());
    // Create an Excel form
    HSSFSheet sheet = workbook.getSheetAt(0);
    int physicalNumberofRows = sheet.getPhysicalNumberOfRows();
    for (int i = 1; i < physicalNumberofRows; i++){ HSSFRow row = sheet.getRow(i); HSSFCell c0 = row.getCell(0);
    double numericCellValue = c0.getNumericCellValue();/ / returns a double
    JObLevel jl = new JObLevel();
    jl.setId((int)numericCellValue);
    }
    return jObLevels;
}
Copy the code

In comparison, the following cast is much more brief, one line of code!

Every little drop makes a mickle.