SpringBoot导入xls数据

(1)前端:

        <form  enctype="multipart/form-data" method="post" action="/xx/xx">
        文件:<input type="file" name="fileUpload" style="width: 200px" />
        <input type="submit" value="导入xls数据" />
	</form>
	注意这种展示效果比较丑,做好封装在绚丽的弹框中使用。

(2)后端:

    @RequestMapping("/xx/xx")
    public String importCallerInfo(MultipartFile fileUpload, HttpSession session)throws Exception{
        if(!fileUpload.isEmpty()){
            int flag = aaService.importCallerInfo(fileUpload, session);
            if (flag==-1){
                return "redirect:/";
            }

        }
		//如果上传文件为空,跳转到导入原页面。
        return "redirect:/xx/xx/xx.html";
    }
   
       public int importCallerInfo(MultipartFile fileUpload, HttpSession httpSession)throws Exception {
        LoginUser user = (LoginUser) httpSession.getAttribute("user_id");
        if(user==null){
            return -1;
        }
        String fileName = fileUpload.getOriginalFilename();

        File uploadFilePathTmp = new File(uploadFilePath);
        if (!uploadFilePathTmp.exists()) {
            uploadFilePathTmp.mkdirs();
        }

        String filePath = uploadFilePath + File.separator + fileName;

        long temp=0;
        try {
            fileUpload.transferTo(new File(filePath));

            FileInputStream fileInputStream = new FileInputStream(filePath);
            Workbook sheets = new HSSFWorkbook(fileInputStream);
            Sheet sheet = sheets.getSheetAt(0);
            List<Entity> list = new ArrayList<>();
            for (Row row : sheet) {
//                if(row.getRowNum()==0){
//                    continue;
//                }
                //为空的行不解析
                if(row.getCell(0)==null||row.getCell(0).equals("")){
                    continue;
                }
                Entity entity = new Entity();
                entity.setId(UUID.randomUUID().toString().replace("-",""));
                entity.setInsert_time(sdf2.format(new Date()));
                entity.setInsert_user(user.getUsername());
                temp++;
                for (Cell cell : row) {
                    cell.setCellType(CellType.STRING);
                    String str=cell.getStringCellValue();
                    int columnIndex = cell.getColumnIndex();
                    switch (columnIndex){
                        case 0:
                            entity.setAttr01(str);
                            break;
                        case 1:
                            entity.setAttr02(str);
                            break;
                        case 2:
                            entity.setAttr03(str);
                            break;
                        case 3:
                            entity.setAttr04(str);
                            break;
                    }

                }
            
                list.add(entity);
             
				//2000条批量插入数据库一次
                if(temp%2000==0){
                    
                    int i = xDao.betchInsert(list);
                    list.clear();
             
                }
            }

            int i = xDao.betchInsert(list);
          
        } catch (IOException e) {
            e.printStackTrace();
        }

        return 1;
    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

郝少

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值