Analysis.java 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. package com.mokamrp.privates.help;
  2. import com.mokamrp.privates.entity.VoltaHandle;
  3. import com.mokamrp.privates.utils.StringUtils;
  4. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  5. import org.apache.poi.ss.usermodel.*;
  6. import org.apache.poi.xssf.usermodel.XSSFCell;
  7. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  8. import org.springframework.web.multipart.MultipartFile;
  9. import java.io.*;
  10. import java.util.ArrayList;
  11. import java.util.HashMap;
  12. import java.util.Map;
  13. /**
  14. * @Author:huang
  15. * @Date:2019-09-10 10:29
  16. * @Description:excel文件解析类
  17. */
  18. public class Analysis {
  19. public Analysis() {
  20. throw new Error("工具类不允许实例化!");
  21. }
  22. /**
  23. * 获取并解析excel文件,返回一个二维集合
  24. * @param file 上传的文件
  25. * @return 二维集合(第一重集合为行,第二重集合为列,每一行包含该行的列集合,列集合包含该行的全部单元格的值)
  26. */
  27. public static VoltaHandle<ArrayList<Map<String,String>>> analysis(MultipartFile file, Map<Integer, String> field) {
  28. VoltaHandle<ArrayList<Map<String,String>>> res = new VoltaHandle<>();
  29. ArrayList<Map<String,String>> row = new ArrayList<>();
  30. if(field.size()<=0){
  31. res.setErr("表格每列绑定参数为空,请先绑定");
  32. return res;
  33. }
  34. //获取文件名称
  35. String fileName = file.getOriginalFilename();
  36. System.out.println(fileName);
  37. try {
  38. //获取输入流
  39. InputStream in = file.getInputStream();
  40. //判断excel版本
  41. Workbook workbook = null;
  42. if (judegExcelEdition(fileName)) {
  43. workbook = new XSSFWorkbook(in);
  44. } else {
  45. workbook = new HSSFWorkbook(in);
  46. }
  47. //获取第一张工作表
  48. Sheet sheet = workbook.getSheetAt(0);
  49. //从第二行开始获取
  50. for (int i = 1; i < sheet.getPhysicalNumberOfRows(); i++) {
  51. //循环获取工作表的每一行
  52. Row sheetRow = sheet.getRow(i);
  53. if (sheetRow == null){
  54. continue;
  55. }
  56. //.如果每行首列为空 直接视为本行没数据
  57. if(Analysis.getCellValue(sheetRow.getCell(0)).isEmpty()){
  58. continue;
  59. }
  60. //循环获取每一列
  61. Map<String,String> cell = new HashMap<>();
  62. for (int j = 0; j < sheet.getRow(0).getPhysicalNumberOfCells(); j++) {
  63. if (field.get(j) == null){
  64. continue;
  65. }
  66. String[] param = field.get(j).split("\\|");
  67. String paramKey = param[0];
  68. String paramValue = null;
  69. String paramRule = null;
  70. if (param.length > 1){
  71. paramRule = param[1];
  72. }
  73. String cellStr = Analysis.getCellValue(sheetRow.getCell(j));
  74. //如果列数据为空,检查是否允许此列数据为空
  75. if (cellStr.isEmpty()) {
  76. if (paramRule == null){
  77. String collName = Analysis.getCellValue(sheet.getRow(0).getCell(j));
  78. res.setErr("第"+i+"行,("+collName+")列数据不可为空,请检查表格");
  79. return res;
  80. }else if (paramRule.equals("default")) {
  81. continue;
  82. }else {
  83. paramValue = paramRule;
  84. }
  85. }else{
  86. paramValue = cellStr;
  87. }
  88. if(j>=field.size()){
  89. break;
  90. }
  91. cell.put(paramKey,paramValue);
  92. }
  93. if (!cell.isEmpty()){
  94. //将装有每一列的集合装入大集合
  95. row.add(cell);
  96. }
  97. //关闭资源
  98. workbook.close();
  99. }
  100. } catch (FileNotFoundException e) {
  101. res.setErr("未找到文件");
  102. return res;
  103. } catch (IOException e) {
  104. res.setErr("上传时报");
  105. return res;
  106. }
  107. res.setData(row);
  108. return res;
  109. }
  110. /**
  111. * 判断上传的excel文件版本(xls为2003,xlsx为2017)
  112. * @param fileName 文件路径
  113. * @return excel2007及以上版本返回true,excel2007以下版本返回false
  114. */
  115. private static boolean judegExcelEdition(String fileName){
  116. if (fileName.matches("^.+\\.(?i)(xls)$")){
  117. return false;
  118. }else {
  119. return true;
  120. }
  121. }
  122. public static String getCellValue(Cell cell) {
  123. String value = "";
  124. if (cell != null) {
  125. // 以下是判断数据的类型
  126. switch (cell.getCellTypeEnum()) {
  127. case NUMERIC: // 数字
  128. cell.setCellType(CellType.STRING);
  129. value = cell.getStringCellValue() + "";
  130. break;
  131. case STRING: // 字符串
  132. value = cell.getStringCellValue();
  133. break;
  134. case BOOLEAN: // Boolean
  135. value = cell.getBooleanCellValue() + "";
  136. break;
  137. case FORMULA: // 公式
  138. value = cell.getCellFormula() + "";
  139. break;
  140. case BLANK: // 空值
  141. value = "";
  142. break;
  143. case ERROR: // 故障
  144. value = "非法字符";
  145. break;
  146. default:
  147. value = "";
  148. break;
  149. }
  150. }
  151. return value.trim();
  152. }
  153. }