ExcelUtil.java 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. package com.gyee.impala.common.util;
  2. import lombok.extern.slf4j.Slf4j;
  3. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  4. import org.apache.poi.ss.usermodel.Row;
  5. import org.apache.poi.ss.usermodel.Sheet;
  6. import org.apache.poi.ss.usermodel.Workbook;
  7. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  8. import org.springframework.util.StringUtils;
  9. import java.io.*;
  10. import java.util.*;
  11. /**
  12. */
  13. @Slf4j
  14. public class ExcelUtil {
  15. /**
  16. * 解析场站的部件DI测点
  17. *
  18. * @param fileName 文件名
  19. * @return
  20. */
  21. public static Map<String, List<String>> readExcelToMap(String fileName) {
  22. Map<String, List<String>> map = new HashMap<>();
  23. File file = new File(fileName);
  24. if (!file.isFile() && file.exists()) {
  25. log.error("解析的excel文件不存在!");
  26. return map;
  27. }
  28. Workbook wb;
  29. FileInputStream fis = null;
  30. try {
  31. fis = new FileInputStream(file);
  32. if (checkFile(fileName) == 1) {
  33. wb = new XSSFWorkbook(file);
  34. } else if (checkFile(fileName) == 2) {
  35. wb = new HSSFWorkbook(fis);
  36. } else {
  37. log.error("解析的文件类型错误!");
  38. return map;
  39. }
  40. // 解析文件 第一个sheet用不到
  41. for (int i = 1; i < wb.getNumberOfSheets(); i++) {
  42. Sheet sheet = wb.getSheetAt(i);
  43. if (sheet == null)
  44. continue;
  45. if(i == 2)
  46. break;
  47. String key = null;
  48. List<String> list = new ArrayList<>();
  49. // 第一行是标题,不解析
  50. for (int j = 1; j <= sheet.getLastRowNum(); j++) {
  51. Row row = sheet.getRow(j);
  52. if (row == null)
  53. continue;
  54. String temp = "";
  55. key = row.getCell(0).toString() + row.getCell(1).toString();
  56. for (int k = 2; k < row.getLastCellNum(); k++) {
  57. String data = row.getCell(k).toString();
  58. if (StringUtils.hasText(data))
  59. temp += data;
  60. if (k < row.getLastCellNum() - 1)
  61. temp += ",";
  62. }
  63. list.add(temp);
  64. }
  65. map.put(key, list);
  66. }
  67. } catch (Exception e) {
  68. log.error("excel文件解析异常:" + e.getMessage());
  69. } finally {
  70. if (fis != null) {
  71. try {
  72. fis.close();
  73. } catch (IOException e) {
  74. e.printStackTrace();
  75. }
  76. }
  77. }
  78. return map;
  79. }
  80. /**
  81. * 解析风机基础配置
  82. *
  83. * @param fileName
  84. * @return
  85. */
  86. public static List<String[]> readExcelToList(String fileName) {
  87. List<String[]> list = new ArrayList<>();
  88. File file = new File(fileName);
  89. if (!file.isFile() && file.exists()) {
  90. log.error("解析的excel文件不存在!");
  91. return list;
  92. }
  93. Workbook wb;
  94. FileInputStream fis = null;
  95. try {
  96. fis = new FileInputStream(file);
  97. if (checkFile(fileName) == 1) {
  98. wb = new XSSFWorkbook(file);
  99. } else if (checkFile(fileName) == 2) {
  100. wb = new HSSFWorkbook(fis);
  101. } else {
  102. log.error("解析的文件类型错误!");
  103. return list;
  104. }
  105. // 解析文件 读第一个sheet
  106. Sheet sheet = wb.getSheetAt(0);
  107. // 第一行是标题,不解析
  108. for (int i = 1; i <= sheet.getLastRowNum(); i++) {
  109. Row row = sheet.getRow(i);
  110. if (row == null)
  111. continue;
  112. String[] str = new String[row.getLastCellNum()];
  113. for (int j = 0; j < row.getLastCellNum(); j++) {
  114. String data = row.getCell(j) + "";
  115. str[j] = data;
  116. }
  117. list.add(str);
  118. }
  119. } catch (Exception e) {
  120. e.printStackTrace();
  121. } finally {
  122. if (fis != null) {
  123. try {
  124. fis.close();
  125. } catch (IOException e) {
  126. e.printStackTrace();
  127. }
  128. }
  129. }
  130. return list;
  131. }
  132. /**
  133. * 判断File文件的类型
  134. *
  135. * @param fileName 传入的文件
  136. * @return 1-XLS文件,2-XLSX文件,3-文件类型错误
  137. */
  138. public static int checkFile(String fileName) {
  139. if (fileName.endsWith(".xlsx")) {
  140. return 1;
  141. }
  142. if (fileName.endsWith(".xls")) {
  143. return 2;
  144. }
  145. return 3;
  146. }
  147. }