Java实现Excel文件的导入功能 您所在的位置:网站首页 如何利用java导出excel Java实现Excel文件的导入功能

Java实现Excel文件的导入功能

2023-10-05 13:23| 来源: 网络整理| 查看: 265

近期在工作上,遇到了实现Excel文件的导入功能,在此和小伙伴们分享一下过程。 实现Excel文件的导入呢,首先我们需要先上传文件,然后在后端进行解析文件中的内容。这里我们需要用到 poi 的这样一个jar包。 因为我的工作项目使用的是gradle,不是Maven,所有我的jar包都是去网上下载,然后复制到我的lib文件下。 在这里插入图片描述 这些jar包都是应该能够使用到的jar包,至于怎么去下载jar包,不会的可以去度娘搜索。如果使用不了的话,还可能会因为版本问题,去搜索适合自己的版本jar包。 导入完poijar包后,我们就可以进行测试开发了。

后端代码如下: Controller层 @RestController @RequestMapping("/excel") public class ExcelController { @Resource private ExcelService excelService; /** * 首先上传文件模块 */ @RequestMapping(value = "/uploadExcel", method = RequestMethod.POST) public WrappedResult uploadFile(@RequestParam MultipartFile file) { String a = excelService.uploadExcel(file,tenantId,userId); return WrappedResult.successWrapedResult(a); } catch (Exception e) { return WrappedResult.failedValidateWrappedResult(e.getMessage()); } } } Service层 public interface ExcelService { String uploadExcel(MultipartFile file) } Service实现类 @Service public class ExcelServiceImpl implements ExcelService{ @Autowired private ExcelDao excelDao; /** * 先上传文件 */ @Override public String uploadExcel(MultipartFile file) throws Exception { if (file == null) { throw new NullArgumentException(); } String filename = file.getOriginalFilename(); if (filename == null) { throw new NullArgumentException(); } String a = ""; try { // 调用解析文件方法 a = parseRowCell(filename, file.getInputStream()); return a; } catch (IOException e) { throw new Exception(e.getMessage()); } } /** * 解析文件中的数据 */ private String parseRowCell(String filename, InputStream is) { Map map = new HashMap(); try { Workbook workbook = null; // 判断excel的后缀,不同的后缀用不同的对象去解析 // xls是低版本的Excel文件 if (filename.endsWith(".xls")) { workbook = new HSSFWorkbook(is); } // xlsx是高版本的Excel文件 if (filename.endsWith(".xlsx")) { workbook = new XSSFWorkbook(is); } if (workbook == null) { throw new NullArgumentException(); } // 取到excel 中的第一张工作表 Sheet sheet = workbook.getSheetAt(0); if (sheet == null) { throw new NullArgumentException(); } // 工作表中第一行是表头,不获取,从第二行开始获取 for (int rowNum = 1; rowNum continue; } /** * 以下的取数据,具体看你的Excel文件中有多少列数据,以此类推 */ // 取到这一行的第一列数据 (设备名称),赋值给deviceName String deviceName = ""; if (row.getCell(0) != null) { row.getCell(0).setCellType(CellType.STRING); deviceName = row.getCell(0).getStringCellValue().trim(); } // 取到这一行的第二列数据 (设备类型),赋值给deviceType String deviceType = ""; if (row.getCell(1) != null) { row.getCell(1).setCellType(CellType.STRING); deviceType = row.getCell(1).getStringCellValue().trim(); } // 取到这一行的第三列数据 (设备型号),赋值给specification String specification = ""; if (row.getCell(2) != null) { row.getCell(2).setCellType(CellType.STRING); specification = row.getCell(2).getStringCellValue().trim(); } // 取到这一行的第四列数据 (总数量),赋值给total String total = ""; if (row.getCell(3) != null) { row.getCell(3).setCellType(CellType.STRING); total = row.getCell(3).getStringCellValue().trim(); } if (StringUtils.isAllBlank(deviceName, deviceType, specification, total)) { continue; } if (StringUtils.isAnyBlank(deviceName, deviceType, specification, total)) { throw new NullArgumentException(); } /** 这段可忽略 // 获取完每一行的数据后,存入对应的业务表中 String id = UUID.randomUUID().toString().replace("-", ""); map.put("id", id); map.put("deviceName", deviceName); map.put("deviceType", deviceType); map.put("specification", specification); map.put("total", total); map.put("tenantId", tenantId); map.put("createUser", tenantId); excelDao.saveDeviceInfo(map); */ } return "true"; } catch (IOException e) { return e.getMessage(); } } }

因为我前端使用的是Vue,前端代码也一起贴出来,有需要的可以自己随便看看。

前端代码: 导入

这里使用的是Vue 中Upload组件,其中使用到一些API属性,需要的可自行去iview官网学习。 链接: Upload组件使用 在这里插入图片描述

// 接收后台传回来的参数,true 为导入成功,false 为导入失败 handleSuccess (res) { // debugger if (res.successful == true) { this.$Message.info('导入文件成功'); } else { this.$Message.error('导入文件失败(请检查文件中数据是否有为空)'); } this.getShebtzxx(); }, // 导入文件前验证文件是否为Excel,不是直接返回false handleBeforeUpload (file) { // debugger if (!file.name.endsWith(".xls") && !file.name.endsWith(".xlsx")) { this.$Message.error('请选择Excel文件导入'); return false; } },

结束,告辞 希望这篇文章能帮助你!



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有