java导入excel表格,并赋上对应的值

//前台先上传excel到服务器  再获取上传到服务器excel的URL 创建输入流 并通过URL打开流
        SysOssEntity ossEntity = sysOssService.selectById(storeRecordsEntity.getOssId());
        URL url = new URL(ossEntity.getUrl());
        InputStream inputStream = url.openStream();
        XSSFWorkbook wb0 = new XSSFWorkbook(inputStream);
        Sheet sht0 = wb0.getSheetAt(0);
        List<StoreRecordsEntity> temp = new ArrayList();

        String[] headers = {"仓库编号", "品名", "单位", "编号", "分类", "仓库名", "库存"};
        IEExcelUtils.checkXSSFHeader(headers,wb0);

        //非法数据品名
        List<String> illegalDataProductNames = new ArrayList<>();
        //对Sheet中的每一行进行迭代
        for (Row r : sht0) {
            //如果当前行的行号(从0开始)未达到2(第三行)则从新循环
            if (r.getRowNum() < 1) {
                continue;
            }
            StoreRecordsEntity recordsEntity = new StoreRecordsEntity();
            //仓库编号为空,跳过
            if (r.getCell(0)==null){
                illegalDataProductNames.add(r.getCell(1).getStringCellValue());
                continue;
            }
            r.getCell(0).setCellType(CellType.STRING);
            recordsEntity.setRepertoryId(Long.valueOf(r.getCell(0).getStringCellValue()));
            recordsEntity.setProductName(r.getCell(1).getStringCellValue());
            recordsEntity.setUnitName(r.getCell(2).getStringCellValue());
            //编号为空,跳过
            if (r.getCell(3)==null){
                illegalDataProductNames.add(r.getCell(1).getStringCellValue());
                continue;
            }
            r.getCell(3).setCellType(CellType.STRING);
            recordsEntity.setProductNo(r.getCell(3).getStringCellValue());
            recordsEntity.setCategoryName(r.getCell(4).getStringCellValue());
            recordsEntity.setRepertoryName(r.getCell(5).getStringCellValue());
            //库存为空,跳过
            if (r.getCell(6)==null){
                illegalDataProductNames.add(r.getCell(1).getStringCellValue());
                continue;
            }
            r.getCell(6).setCellType(CellType.STRING);
            BigDecimal a = new BigDecimal(r.getCell(6).getStringCellValue());
            recordsEntity.setCurrentNums(a);
            Map<String, Object> params = new HashMap<>();
            params.put("productNo", recordsEntity.getProductNo());
            params.put("repertory_id", recordsEntity.getRepertoryId());
            StoreRecordsEntity recordsEntity1 = recordsService.queryInventory2(params);
            //成本价
            BigDecimal unitPrice = BigDecimalUtils.div(BigDecimal.ROUND_HALF_UP,recordsEntity1.getTotalPrice(),recordsEntity1.getUnitNums());
            recordsEntity.setUnitPrice(unitPrice);
            //数据有缺失,直接跳过
            if (recordsEntity1==null){
                illegalDataProductNames.add(r.getCell(1).getStringCellValue());
                continue;
            }
            recordsEntity.setProductId(recordsEntity1.getProductId());
            recordsEntity.setRepertoryId(recordsEntity1.getRepertoryId());
            recordsEntity.setUnitId(recordsEntity1.getUnitId());
            temp.add(recordsEntity);
        }
        /*
         * 获取所有包含 productName、productNo、unit_nums 的StoreRecordsEntity集合
         * 遍历temp 根据productNo查询storeRecordsEntities里对应的数据
         * 并且equals productName是否一样
         * 把对应数据的unit_nums设置给originalQuantity(原数量)
         */
        List<StoreRecordsEntity> storeRecordsEntities = recordsService.queryInventoryNumsAll();
        temp.forEach(list -> {
            storeRecordsEntities.forEach(entity -> {
                if (list.getProductNo().equals(entity.getProductNo()) && list.getProductName().equals(entity.getProductName())) {
                    list.setOriginalQuantity(entity.getUnitNums());
                }
            });
        });
        inputStream.close();
        //如果记录的非法数据大于0,即代表有非法数据,则返回这个。没有非法数据就不需要返回
        return R.ok(illegalDataProductNames.size()>0?illegalDataProductNames.toString()+"因数据非法被去除":"导入成功").put("list", temp);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用Apache POI库来读取和写入Excel文件。下面是一个使用SSM框架和Apache POI库导入Excel表格的示例: 1. 在pom.xml文件中添加Apache POI依赖: ```xml <!-- Apache POI依赖 --> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>4.1.2</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>4.1.2</version> </dependency> ``` 2. 创建一个Excel导入的Controller: ```java @Controller @RequestMapping("/excel") public class ExcelController { @Autowired private UserService userService; @RequestMapping(value = "/import", method = RequestMethod.POST) public String importExcel(@RequestParam("file") MultipartFile file) throws IOException { // 读取Excel文件 Workbook workbook = WorkbookFactory.create(file.getInputStream()); Sheet sheet = workbook.getSheetAt(0); // 遍历行 for (Row row : sheet) { // 跳过标题行 if (row.getRowNum() == 0) { continue; } // 创建用户对象 User user = new User(); // 设置用户对象的属性 user.setName(row.getCell(0).getStringCellValue()); user.setAge((int) row.getCell(1).getNumericCellValue()); user.setGender(row.getCell(2).getStringCellValue()); // 保存用户对象到数据库 userService.save(user); } return "redirect:/user/list"; } } ``` 3. 在前端页面中添加一个文件上传表单: ```html <form action="/excel/import" method="POST" enctype="multipart/form-data"> <input type="file" name="file" /> <button type="submit">导入</button> </form> ``` 这样就可以通过上传Excel表格导入数据数据库中了。需要注意的是,上传的Excel表格必须按照指定的格式,并且该格式需要与Controller中的代码相对应

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值