当前位置:   article > 正文

java easypoi导入Excel文件案例_easypoi 插入文件

easypoi 插入文件
  1. <dependency>
  2. <groupId>cn.afterturn</groupId>
  3. <artifactId>easypoi-base</artifactId>
  4. <version>3.2.0</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>cn.afterturn</groupId>
  8. <artifactId>easypoi-annotation</artifactId>
  9. <version>3.2.0</version>
  10. </dependency>
  11. <dependency>
  12. <groupId>cn.afterturn</groupId>
  13. <artifactId>easypoi-web</artifactId>
  14. <version>3.2.0</version>
  15. </dependency>

        

  1. public AjaxJson importData(HttpServletRequest request,MultipartFile mFile) {
  2. List<Map<String, Object>> dataSource = new ArrayList<>();
  3. AjaxJson ajaxJson = new AjaxJson();
  4. int rowNum = 0;
  5. CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(request.getSession().getServletContext());
  6. if (multipartResolver.isMultipart(request)){
  7. MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
  8. //开始导入文件
  9. mFile = multipartRequest.getFile("mFile");
  10. String eName = "";
  11. if(mFile==null){
  12. ajaxJson.setSuccess(false);
  13. ajaxJson.setMsg("请上传数据源文件!");
  14. return ajaxJson;
  15. }
  16. // 获得上传文件的文件名
  17. String fileName = mFile.getOriginalFilename();
  18. // 获取文件扩展名
  19. eName = fileName.substring(fileName.lastIndexOf(".") + 1);
  20. if(!"xls".equals(eName)&&!"xlsx".equals(eName)){
  21. ajaxJson.setSuccess(false);
  22. ajaxJson.setMsg("请上传正确格式的数据源文件!");
  23. return ajaxJson;
  24. }
  25. //数据是否正确;
  26. Workbook workbook = null;
  27. try {
  28. InputStream inputStream = mFile.getInputStream();
  29. if ("xls".equals(eName)) {
  30. // 2003
  31. workbook = new HSSFWorkbook(inputStream);
  32. } else {
  33. // 2007
  34. workbook = new XSSFWorkbook(inputStream);
  35. }
  36. // 获取工作薄第一张表
  37. Sheet sheet = workbook.getSheetAt(0);
  38. // 获取名称
  39. String sheetName = sheet.getSheetName().trim();
  40. // 获取第一行
  41. Row row = sheet.getRow(0);
  42. if(null == row){
  43. ajaxJson.setSuccess(false);
  44. ajaxJson.setMsg("模板错误,请重新上传!");
  45. return ajaxJson;
  46. }
  47. // 获得有效行数
  48. rowNum = sheet.getLastRowNum();
  49. if(0==rowNum){
  50. ajaxJson.setSuccess(false);
  51. ajaxJson.setMsg("数据源表中数据不得小于1条!");
  52. return ajaxJson;
  53. }
  54. for (int i = 1; i <= rowNum; i++) {
  55. row = sheet.getRow(i);
  56. if(null==row.getCell(0)){
  57. row.createCell(0);
  58. }
  59. row.getCell(0).setCellType(Cell.CELL_TYPE_STRING);
  60. if(null==row.getCell(1)){
  61. row.createCell(1);
  62. }
  63. row.getCell(1).setCellType(Cell.CELL_TYPE_STRING);
  64. if(null==row.getCell(2)){
  65. row.createCell(2);
  66. }
  67. row.getCell(2).setCellType(Cell.CELL_TYPE_STRING);
  68. if(null==row.getCell(3)){
  69. row.createCell(3);
  70. }
  71. row.getCell(3).setCellType(Cell.CELL_TYPE_STRING);
  72. if(null==row.getCell(4)){
  73. row.createCell(4);
  74. }
  75. row.getCell(4).setCellType(Cell.CELL_TYPE_STRING);
  76. if(null==row.getCell(5)){
  77. row.createCell(5);
  78. }
  79. row.getCell(5).setCellType(Cell.CELL_TYPE_STRING);
  80. Pmcused pmcused = new Pmcused();
  81. if(row.getCell(0)!=null && !"".equals(row.getCell(0).getStringCellValue())) {
  82. pmcused.setId(row.getCell(0).getStringCellValue());//ID
  83. }
  84. if(row.getCell(1)!=null && !"".equals(row.getCell(1).getStringCellValue())){
  85. pmcused.setCreateTime(row.getCell(1).getStringCellValue());
  86. }
  87. if(row.getCell(2)!=null && !"".equals(row.getCell(2).getStringCellValue())){
  88. pmcused.setStartTime(row.getCell(2).getStringCellValue());
  89. }
  90. if(row.getCell(3)!=null && !"".equals(row.getCell(3).getStringCellValue())){
  91. pmcused.setEndTime(row.getCell(3).getStringCellValue());
  92. }
  93. if(row.getCell(4)!=null && !"".equals(row.getCell(4).getStringCellValue())){
  94. pmcused.setOrgCode(row.getCell(4).getStringCellValue());
  95. }
  96. if(row.getCell(5)!=null && !"".equals(row.getCell(5).getStringCellValue())){
  97. pmcused.setOrgName(row.getCell(5).getStringCellValue());
  98. }
  99. if(row.getCell(6)!=null && !"".equals(row.getCell(6).getStringCellValue())){
  100. pmcused.setHospLv(row.getCell(6).getStringCellValue());
  101. }
  102. if(row.getCell(7)!=null && !"".equals(row.getCell(7).getStringCellValue())){
  103. pmcused.setPmcNum(row.getCell(7).getStringCellValue());
  104. }
  105. if(row.getCell(8)!=null && !"".equals(row.getCell(8).getStringCellValue())){
  106. pmcused.setTotal(row.getCell(8).getStringCellValue());
  107. }
  108. if((!"".equals(row.getCell(4).getStringCellValue())) && (!"".equals(row.getCell(5).getStringCellValue()))){
  109. HashMap<String, Object> map = new HashMap<>();
  110. map.put("id",pmcused.getId());
  111. map.put("createTime",pmcused.getCreateTime());
  112. map.put("startTime",pmcused.getStartTime());
  113. map.put("endTime",pmcused.getEndTime());
  114. map.put("orgCode",pmcused.getOrgCode());
  115. map.put("orgName",pmcused.getOrgName());
  116. map.put("hospLv",pmcused.getHospLv());
  117. map.put("pmcNum",pmcused.getPmcNum());
  118. map.put("total",pmcused.getTotal());
  119. try {
  120. //这里是操作数据库,根据实际需求进行取舍
  121. pmcused.setFlag("0");
  122. pmcDataImportMapper.importDataSource(pmcused);
  123. //将读取的数据存入dataSource集合中,用于页面展示
  124. dataSource.add(map);
  125. }catch (Exception e){
  126. throw new ServiceException(ServiceCode.ERR_INSERT,"插入数据失败!");
  127. }
  128. }
  129. }
  130. } catch (Exception e) {
  131. throw new ServiceException(ServiceCode.ERR_BAD_REQUEST,"网络异常,请联系管理员!");
  132. }
  133. }
  134. ajaxJson.setSuccess(true);
  135. ajaxJson.setMsg("导入成功,共记:"+rowNum+"条");
  136. ajaxJson.setObj(dataSource);
  137. return ajaxJson;
  138. }

注:本案例以以下表格进行设计,实际以自己需求设计

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/不正经/article/detail/540309
推荐阅读
相关标签
  

闽ICP备14008679号