@ApiOperation(value = "大单及专业赛道支撑情况导入") @RequestMapping(value = "/ExportOrderTrack",method = RequestMethod.POST) public Result exportOrderTrack(@RequestParam("file") MultipartFile file) throws IOException { String originalFilename = file.getOriginalFilename(); String[] split = originalFilename.split("\\."); String suffixName = split[1]; // 1、获取传入的文件流 InputStream inputStream = file.getInputStream(); Workbook wb = null; if ("xlsx".equals(suffixName)){ wb = new XSSFWorkbook(inputStream); } if ("xls".equals(suffixName)){ wb = new HSSFWorkbook(inputStream); } // 2、获取工作簿中的表,对表中的设置,通过下标去拿第一张表 Sheet sheet = wb.getSheetAt(0); // 3、获取表格总的行数 int rowNumber = sheet.getPhysicalNumberOfRows(); List<KanBanOrderTrack> lists = new ArrayList<>(); // 从第二行开始读取每一行数据,我们设置的模板表头是在第一、第二行,下标从0开始。 for (int rowNum = 2; rowNum < rowNumber; rowNum++) { // 每读取一行,实例化一个对象 KanBanOrderTrack kanBanOrderTrack = new KanBanOrderTrack(); // 获取工作表中第三行的数据 Row rowData = sheet.getRow(rowNum); // 这个地方,要注意,取到的值要进行判空处理,同时也要对值的类型进行转换 if (rowData != null) { kanBanOrderTrack.setId(CommonUtil.generateUUID()); rowData.getCell(0).setCellType(CellType.STRING); rowData.getCell(1).setCellType(CellType.STRING); rowData.getCell(2).setCellType(CellType.STRING); rowData.getCell(3).setCellType(CellType.STRING); rowData.getCell(4).setCellType(CellType.STRING); rowData.getCell(5).setCellType(CellType.STRING); rowData.getCell(6).setCellType(CellType.STRING); kanBanOrderTrack.setProvinceName(rowData.getCell(0).getStringCellValue()); kanBanOrderTrack.setOrderIntegrationCompanySupport(Double.parseDouble(rowData.getCell(1).getStringCellValue())); kanBanOrderTrack.setOrderProvinceCompanySupport(Double.parseDouble(rowData.getCell(2).getStringCellValue())); kanBanOrderTrack.setOrderSupportRate(Double.parseDouble(rowData.getCell(3).getStringCellValue())); kanBanOrderTrack.setTrackIntegrationCompanySupport(Double.parseDouble(rowData.getCell(4).getStringCellValue())); kanBanOrderTrack.setTrackProvinceCompanySupport(Double.parseDouble(rowData.getCell(5).getStringCellValue())); kanBanOrderTrack.setTrackSupportRate(Double.parseDouble(rowData.getCell(6).getStringCellValue())); lists.add(kanBanOrderTrack); } } // 关闭流 inputStream.close(); // 完成批量添加数据 String url = urlUtil.genBaseUrl(CommonConfig.ADD_ORDER_TRACK_URL); oAuth2RestTemplate.postForObject(url, lists, String.class); return Result.ok("导入成功"); }
项目里我自己写的导入
最新推荐文章于 2024-11-09 12:43:52 发布