ExcelUtil.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. package com.simuwang.base.common.util;
  2. import cn.hutool.core.collection.CollUtil;
  3. import cn.hutool.core.date.DateUtil;
  4. import cn.hutool.core.util.StrUtil;
  5. import com.simuwang.base.common.conts.DateConst;
  6. import org.apache.poi.hssf.usermodel.*;
  7. import org.apache.poi.ss.usermodel.*;
  8. import org.apache.poi.ss.util.CellRangeAddress;
  9. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  10. import org.jsoup.nodes.Element;
  11. import org.jsoup.select.Elements;
  12. import org.slf4j.Logger;
  13. import org.slf4j.LoggerFactory;
  14. import java.io.*;
  15. import java.text.NumberFormat;
  16. import java.util.Date;
  17. import java.util.List;
  18. import java.util.Map;
  19. import java.util.UUID;
  20. import org.apache.commons.compress.archivers.ArchiveEntry;
  21. import org.apache.commons.compress.archivers.ArchiveInputStream;
  22. import org.apache.commons.compress.archivers.ArchiveStreamFactory;
  23. import org.apache.commons.compress.utils.IOUtils;
  24. public class ExcelUtil {
  25. private static final Logger logger = LoggerFactory.getLogger(ExcelUtil.class);
  26. public static boolean isExcel(String name) {
  27. return name.endsWith("xls") || name.endsWith("xlsx");
  28. }
  29. public static boolean isPdf(String name) {
  30. return name.endsWith("pdf") || name.endsWith("PDF");
  31. }
  32. public static boolean isZip(String filePath) {
  33. return filePath.endsWith("zip") || filePath.endsWith("ZIP");
  34. }
  35. public static Sheet getSheet(File file, int sheetIndex) {
  36. if (file == null || !file.exists()) {
  37. return null;
  38. }
  39. InputStream is = file2InStream(file);
  40. String[] arr = file.getName().split("\\.");
  41. String ext = arr[arr.length - 1];
  42. Sheet sheet = null;
  43. try {
  44. sheet = getSheet(is, ext, sheetIndex);
  45. } catch (Exception e) {
  46. logger.error(e.getMessage());
  47. if (e.getMessage().contains("XSSF instead of HSSF")) {
  48. is = file2InStream(file);
  49. try {
  50. sheet = getSheet(is, "xlsx", sheetIndex);
  51. } catch (IOException e1) {
  52. e1.printStackTrace();
  53. }
  54. } else if (e.getMessage().contains("HSSF instead of XSSF")) {
  55. is = file2InStream(file);
  56. try {
  57. sheet = getSheet(is, "xls", sheetIndex);
  58. } catch (IOException e1) {
  59. e1.printStackTrace();
  60. }
  61. } else if ("xls".equals(ext) && e.getMessage().contains("left 4 bytes remaining still to be read")) {
  62. file = changeXls(file);
  63. is = file2InStream(file);
  64. try {
  65. sheet = getSheet(is, "xls", sheetIndex);
  66. logger.info("文件转换成功!");
  67. } catch (IOException e1) {
  68. e1.printStackTrace();
  69. }
  70. } else {
  71. sheet = null;
  72. }
  73. }
  74. return sheet;
  75. }
  76. public static Sheet getSheet(InputStream is, String ext, int sheetIndex) throws IOException {
  77. if (ext == null) {
  78. ext = "xls";
  79. }
  80. ext = ext.toLowerCase();
  81. Sheet sheet = null;
  82. if ("xls".equals(ext)) {
  83. Workbook wb = new HSSFWorkbook(is);
  84. sheet = getSheet(wb, sheetIndex, null);
  85. } else if ("xlsx".equals(ext)) {
  86. Workbook wb = new XSSFWorkbook(is);
  87. sheet = getSheet(wb, sheetIndex, null);
  88. }
  89. return sheet;
  90. }
  91. private static Sheet getSheet(Workbook wb, int sheetIndex, String sheetName) throws IOException {
  92. if (wb != null) {
  93. Sheet sheet = null;
  94. if (sheetIndex > -1) {
  95. sheet = wb.getSheetAt(sheetIndex);
  96. } else if (StrUtil.isNotBlank(sheetName) && !"null".equals(sheetName.toLowerCase())) {
  97. sheet = wb.getSheet(sheetName);
  98. }
  99. wb.close();
  100. // 同时会把文件输入流关闭
  101. return sheet;
  102. }
  103. return null;
  104. }
  105. public static InputStream file2InStream(File file) {
  106. if (!file.exists()) {
  107. return null;
  108. } else {
  109. FileInputStream is = null;
  110. try {
  111. is = new FileInputStream(file);
  112. } catch (FileNotFoundException var3) {
  113. var3.printStackTrace();
  114. }
  115. return is;
  116. }
  117. }
  118. public static File changeXls(File file) {
  119. try {
  120. jxl.Workbook workbook = jxl.Workbook.getWorkbook(file);
  121. String fileName = "copy-" + file.getName();
  122. File file2 = new File(file.getParent() + "/" + fileName);
  123. if (file2.exists()) {
  124. return file2;
  125. }
  126. jxl.write.WritableWorkbook wwb = jxl.Workbook.createWorkbook(file2, workbook);
  127. wwb.write();
  128. wwb.close();
  129. workbook.close();
  130. return file2;
  131. } catch (Exception e) {
  132. e.printStackTrace();
  133. return null;
  134. }
  135. }
  136. public static String getCellValue(Cell cell) {
  137. if (cell == null) {
  138. return null;
  139. }
  140. String cellValue = "";
  141. switch (cell.getCellTypeEnum()) {
  142. case STRING:
  143. cellValue = cell.getStringCellValue();
  144. break;
  145. case NUMERIC:
  146. if (org.apache.poi.ss.usermodel.DateUtil.isCellDateFormatted(cell)) {
  147. // 如果是日期格式的数字
  148. Date dateCellValue = cell.getDateCellValue();
  149. cellValue = DateUtil.format(dateCellValue, DateConst.YYYY_MM_DD);
  150. } else {
  151. // 否则是纯数字
  152. NumberFormat numberFormat = NumberFormat.getNumberInstance();
  153. numberFormat.setMaximumFractionDigits(15);
  154. double formulaResult = cell.getNumericCellValue();
  155. cellValue = numberFormat.format(formulaResult).replaceAll(",", "");
  156. }
  157. break;
  158. case BOOLEAN:
  159. cellValue = String.valueOf(cell.getBooleanCellValue());
  160. break;
  161. case FORMULA:
  162. // 处理公式结果
  163. try {
  164. cellValue = String.valueOf(cell.getNumericCellValue());
  165. } catch (IllegalStateException e) {
  166. cellValue = cell.getStringCellValue();
  167. }
  168. break;
  169. case BLANK:
  170. break;
  171. case ERROR:
  172. cellValue = "ERROR: " + cell.getErrorCellValue();
  173. break;
  174. default:
  175. cellValue = "";
  176. break;
  177. }
  178. // 去掉换行符号
  179. cellValue = StrUtil.isNotBlank(cellValue) ? cellValue.replaceAll("[\\r\\n]+", "") : "";
  180. return cellValue;
  181. }
  182. public static List<String> extractCompressedFiles(String zipFilePath, String destFilePath) {
  183. List<String> filePathList = CollUtil.newArrayList();
  184. File destFile = new File(destFilePath);
  185. if (!destFile.exists()) {
  186. destFile.mkdirs();
  187. }
  188. try (BufferedInputStream fis = new BufferedInputStream(new FileInputStream(zipFilePath));
  189. ArchiveInputStream ais = new ArchiveStreamFactory().createArchiveInputStream(fis)) {
  190. ArchiveEntry entry;
  191. while ((entry = ais.getNextEntry()) != null) {
  192. File entryFile = new File(destFilePath, entry.getName());
  193. if (entry.isDirectory()) {
  194. entryFile.mkdirs();
  195. } else {
  196. try (FileOutputStream fos = new FileOutputStream(entryFile)) {
  197. IOUtils.copy(ais, fos);
  198. filePathList.add(entryFile.getPath());
  199. }
  200. }
  201. }
  202. } catch (Exception e) {
  203. e.printStackTrace();
  204. }
  205. return filePathList;
  206. }
  207. public static void writeDataToSheet(Sheet sheet, Elements rows) {
  208. int rowSize = rows.size();
  209. for (int rowNum = 0; rowNum < rowSize; rowNum++) {
  210. Row sheetRow = sheet.createRow(rowNum);
  211. Element elementRow = rows.get(rowNum);
  212. Elements cells = elementRow.select("td");
  213. if (cells.size() == 0) {
  214. cells = elementRow.select("th");
  215. }
  216. int cellSize = cells.size();
  217. for (int cellNum = 0; cellNum < cellSize; cellNum++) {
  218. Cell sheetRowCell = sheetRow.createCell(cellNum);
  219. sheetRowCell.setCellValue(cells.get(cellNum).text());
  220. }
  221. }
  222. }
  223. /**
  224. * 获取优先级高的字段值
  225. *
  226. * @param sheetRow 行
  227. * @param priorityPosition 字段位置
  228. * @param basePosition 优先级搞的字段位置
  229. * @return 优先级高的字段值
  230. */
  231. public static String getPriorityFieldValue(Row sheetRow, Integer priorityPosition, Integer basePosition) {
  232. boolean hasPriorityValue = priorityPosition != null && sheetRow.getCell(priorityPosition) != null && StrUtil.isNotBlank(ExcelUtil.getCellValue(sheetRow.getCell(priorityPosition)));
  233. if (hasPriorityValue) {
  234. return ExcelUtil.getCellValue(sheetRow.getCell(priorityPosition));
  235. }
  236. return basePosition != null && sheetRow.getCell(basePosition) != null ? ExcelUtil.getCellValue(sheetRow.getCell(basePosition)) : null;
  237. }
  238. /**
  239. * 去掉逗号
  240. *
  241. * @param numberData 数字型字符串
  242. * @return 无逗号的数字型字符串
  243. */
  244. public static String numberDataStripCommas(String numberData) {
  245. if (StrUtil.isBlank(numberData)) {
  246. return null;
  247. }
  248. // pdf解析到的值带有",",比如:"10,656,097.37"
  249. String data = numberData.replaceAll(",", "");
  250. if (!StringUtil.isNumeric(data)) {
  251. return null;
  252. }
  253. return data;
  254. }
  255. public static HSSFWorkbook getHSSFWorkbook(String sheetName, List<String> title, Map<String, List<List<String>>> valueMap, HSSFWorkbook wb) {
  256. // 第一步,创建一个HSSFWorkbook,对应一个Excel文件
  257. if (wb == null) {
  258. wb = new HSSFWorkbook();
  259. }
  260. try {
  261. // 第二步,在workbook中添加一个sheet,对应Excel文件中的sheet
  262. HSSFSheet sheet = wb.createSheet(sheetName);
  263. // 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制
  264. HSSFRow row = sheet.createRow(0);
  265. // 第四步,创建单元格,并设置值表头 设置表头居中
  266. HSSFCellStyle style = wb.createCellStyle();
  267. style.setAlignment(HorizontalAlignment.LEFT);
  268. style.setWrapText(true);
  269. sheet.setColumnWidth(0, 10000);
  270. sheet.setColumnWidth(1, 10000);
  271. sheet.setColumnWidth(2, 5000);
  272. sheet.setColumnWidth(3, 8000);
  273. sheet.setColumnWidth(4, 8000);
  274. sheet.setColumnWidth(5, 5000);
  275. //声明列对象
  276. HSSFCell cell = null;
  277. //创建标题
  278. for (int i = 0; i < title.size(); i++) {
  279. cell = row.createCell(i);
  280. cell.setCellValue(title.get(i));
  281. cell.setCellStyle(style);
  282. }
  283. List<List<String>> values = valueMap.get(sheetName);
  284. //创建内容
  285. for (int i = 0; i < values.size(); i++) {
  286. row = sheet.createRow(i + 1);
  287. for (int j = 0; j < values.get(i).size(); j++) {
  288. //将内容按顺序赋给对应的列对象
  289. row.createCell(j).setCellValue(values.get(i).get(j));
  290. }
  291. }
  292. } catch (Exception e) {
  293. logger.error(e.getMessage(), e);
  294. }
  295. return wb;
  296. }
  297. }