minCastScore = (int)sheet.getRow(i).getCell(j++).getNumericCellValue();
时间: 2024-05-30 09:11:02 浏览: 11
这段代码是从 Excel 表格中读取一个整数类型的值,具体流程为:
1. 先通过 `sheet.getRow(i)` 获取第 i 行的行对象。
2. 然后通过 `getRow(i).getCell(j++)` 获取该行中第 j 列的单元格对象,并将 j 的值加 1。
3. 最后通过 `getNumericCellValue()` 方法获取该单元格中的数值,并强制转换为整数类型赋值给 `minCastScore` 变量。
需要注意的是,如果该单元格中的数据不是数字类型,会抛出 `java.lang.IllegalStateException` 异常。
相关问题
try { score = (int)sheet.getRow(i).getCell(j++).getNumericCellValue(); //获取double类型 }catch (IllegalStateException e){ j--; score = (int)Double.parseDouble(sheet.getRow(i).getCell(j++).toString()); }这段代码怎样执行的·
这段代码是一个异常处理的逻辑。首先,它尝试从 Excel 表格中的第 `i` 行、第 `j` 列(列索引从 0 开始)获取一个数值类型的单元格值,通过 `getNumericCellValue()` 方法返回一个 double 类型的值。
如果获取单元格值的过程中发生了 `IllegalStateException` 异常,说明该单元格的数据类型不是数值类型,而是其他类型(如字符串类型)。在这种情况下,代码将执行 `catch` 块中的逻辑。
`j--` 是为了回退到之前的列索引,因为在 `try` 块中的 `j++` 已经自增过了,需要回退到之前的列索引。
然后,使用 `Double.parseDouble()` 方法将获取到的单元格值转换为字符串类型,并再次将其转换为整数类型,赋值给变量 `score`。
这段代码的目的是在尝试获取数值类型的单元格值时,如果发生异常,则将其作为字符串类型处理,并将其转换为整数类型。这样可以处理 Excel 表格中可能存在的数据类型不一致的情况。
@RequestMapping(value = "/excel", method = RequestMethod.POST) @ResponseBody public String uploadExcel(@RequestParam("file") MultipartFile file, Model model) { try { List<People> employees = new ArrayList<>(); InputStream inputStream = file.getInputStream(); Workbook workbook = WorkbookFactory.create(inputStream); Sheet sheet = workbook.getSheetAt(0); for (int i = 1; i <= sheet.getLastRowNum(); i++) { Row row = sheet.getRow(i); if (row != null) { String name = row.getCell(0).getStringCellValue(); int age = (int) row.getCell(1).getNumericCellValue(); String sex = row.getCell(2).getStringCellValue(); People employee; int bed = (int) row.getCell(3).getNumericCellValue(); String str=Integer.toString(bed); int room_id =(int) row.getCell(4).getNumericCellValue(); if(sex.equals("女")) { employee = new People(name, age, "1", str,room_id); }else { employee = new People(name, age, "0", str,room_id); } employees.add(employee); } } model.addAttribute("employees", employees); for (People employee : employees) { peopleService.PeopleAdd1(employee); } return "result"; } catch (IOException | EncryptedDocumentException e) { e.printStackTrace(); } return "error";
这是一个使用Spring MVC框架进行Excel文件上传的Java代码。通过@RequestMapping注解指定请求路径和请求方法,@RequestParam注解获取上传的文件,@ResponseBody注解表示返回的是字符串类型的数据。在方法中,首先获取上传的Excel文件,然后通过WorkbookFactory类创建Workbook对象,获取Sheet对象,遍历Sheet中每一行数据,获取单元格数据并将其放入People对象中,最后将People对象添加到List集合中。最后,将List集合中的所有People对象添加到数据库中,并返回"result"字符串。如果发生异常,则返回"error"字符串。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)