ReadExcelUtils.java 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. package cn.cslg.pas.common.utils;
  2. import cn.cslg.pas.domain.PatentData;
  3. import org.apache.poi.hssf.usermodel.*;
  4. import org.apache.poi.ooxml.POIXMLDocumentPart;
  5. import org.apache.poi.ss.usermodel.PictureData;
  6. import org.apache.poi.ss.usermodel.Row;
  7. import org.apache.poi.ss.usermodel.Sheet;
  8. import org.apache.poi.ss.usermodel.Workbook;
  9. import org.apache.poi.xssf.usermodel.*;
  10. import org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTMarker;
  11. import org.springframework.stereotype.Service;
  12. import java.awt.*;
  13. import java.io.*;
  14. import java.util.HashMap;
  15. import java.util.List;
  16. import java.util.Map;
  17. /**
  18. * @Author xiexiang
  19. * @Date 2023/5/30
  20. */
  21. @Service
  22. public class ReadExcelUtils {
  23. /**
  24. * 检测Excel文件合法性
  25. *
  26. * @param filePath 文件路径
  27. * @return 返回文件总行数
  28. */
  29. public static Integer textExcel(String filePath) throws IOException {
  30. //判断文件是否存在
  31. if (filePath == null || filePath.equals("")) {
  32. return -1;
  33. // ThrowException.throwXiaoShiException("文件上传失败,服务器忙请稍后再试!");
  34. }
  35. File file = new File(filePath);
  36. if (!file.exists()) {
  37. return -1;
  38. // ThrowException.throwXiaoShiException("文件上传失败,服务器忙请稍后再试!");
  39. }
  40. // 检测是否为excel文件
  41. if (!filePath.endsWith(".xls") && !filePath.endsWith(".xlsx") && !filePath.endsWith(".XLS") && !filePath.endsWith(".XLSX")) {
  42. return -1;
  43. // ThrowException.throwXiaoShiException("请上传Excel文件!");
  44. }
  45. InputStream fis = new FileInputStream(file);
  46. //使用poi框架解析处理Excel文件
  47. Workbook workbook = null;
  48. //区分不同版本Excel,使用各自对应的工具类
  49. if (filePath.endsWith(".xls") || filePath.endsWith(".XLS")) {
  50. workbook = new HSSFWorkbook(fis);
  51. } else if (filePath.endsWith(".xlsx") || filePath.endsWith(".XLSX")) {
  52. workbook = new XSSFWorkbook(fis);
  53. }
  54. //读取第几个sheet
  55. Sheet sheet = workbook.getSheetAt(0);
  56. //读取总行数
  57. int rows = sheet.getPhysicalNumberOfRows();
  58. if (rows <= 0) {
  59. return -2;
  60. // ThrowException.throwXiaoShiException("文件内容格式不正确!");
  61. }
  62. Row firstRow = sheet.getRow(0);
  63. if (!firstRow.getCell(0).getStringCellValue().equals("公开(公告)号")) {
  64. return -2;
  65. // ThrowException.throwXiaoShiException("文件内容格式不正确!");
  66. }
  67. //返回文件总行数-1(即专利总数量)
  68. return rows - 1;
  69. }
  70. /**
  71. * 获取一行专利的全部数据(专利内容数据 + 摘要附图)
  72. *
  73. * @param filePath Excel文件路径
  74. * @param row 行数
  75. * @return 返回装载专利数据(专利内容数据 + 摘要附图)的对象
  76. */
  77. public static PatentData readExcelOneRow(String filePath, Integer row) {
  78. //返回最终结果的对象
  79. PatentData patentData = new PatentData();
  80. //装载专利数据(除了摘要附图)的map:(key:表头如 "公开(公告)号" value:表头对应内容如 "CN1307082B")
  81. Map<Object, Object> map = new HashMap<>();
  82. //装载摘要附图的对象
  83. PictureData pictureData = null;
  84. File file = new File(filePath);
  85. try {
  86. InputStream inputStream = new FileInputStream(file);
  87. //POI可以处理Excel文件
  88. Workbook workbook = null;
  89. //当文件以.xls结尾时
  90. if (filePath.endsWith(".xls") || filePath.endsWith(".XLS")) {
  91. workbook = new HSSFWorkbook(inputStream);
  92. } else if (filePath.endsWith(".xlsx") || filePath.endsWith(".XLSX")) {
  93. workbook = new XSSFWorkbook(inputStream);
  94. }
  95. //读取第几个sheet
  96. Sheet sheet = workbook.getSheetAt(0);
  97. //读取总行数
  98. int rows = sheet.getPhysicalNumberOfRows();
  99. if (rows <= row) {
  100. ThrowException.throwXiaoShiException("row超出Excel文档中数量");
  101. }
  102. //开始装载专利数据
  103. Row firstRow = sheet.getRow(0);
  104. Row needRow = sheet.getRow(row);
  105. //读取第一行的时候会多读一列
  106. int firstColumns = firstRow.getLastCellNum() - 0;
  107. for (int i = 0; i < firstColumns; i++) {
  108. map.put(firstRow.getCell(i) + "", needRow.getCell(i) + "");
  109. }
  110. //开始装载专利摘要附图(判断用07还是03的方法获取图片)
  111. if (filePath.endsWith(".xls") || filePath.endsWith(".XLS")) {
  112. pictureData = getPictures1((HSSFSheet) sheet, row);
  113. } else if (filePath.endsWith(".xlsx") || filePath.endsWith(".XLSX")) {
  114. pictureData = getPictures2((XSSFSheet) sheet, row);
  115. }
  116. workbook.close();
  117. //返回结果对象装载结果
  118. patentData.setMap(map);
  119. patentData.setPictureData(pictureData);
  120. } catch (IOException e) {
  121. e.printStackTrace();
  122. }
  123. return patentData;
  124. }
  125. /**
  126. * 03版本Excel取附图
  127. *
  128. * @param sheet Excel工作簿
  129. * @return 返回附图map
  130. */
  131. public static PictureData getPictures1(HSSFSheet sheet, Integer row) throws IOException {
  132. if (sheet.getDrawingPatriarch() != null) {
  133. List<HSSFShape> list = sheet.getDrawingPatriarch().getChildren();
  134. for (HSSFShape shape : list) {
  135. if (shape instanceof HSSFPicture) {
  136. HSSFPicture picture = (HSSFPicture) shape;
  137. HSSFClientAnchor cAnchor = (HSSFClientAnchor) picture.getAnchor();
  138. int row1 = cAnchor.getRow1();
  139. if (row1 == row) {
  140. return picture.getPictureData();
  141. }
  142. }
  143. }
  144. }
  145. return null;
  146. }
  147. /**
  148. * 07版本Excel取附图
  149. *
  150. * @param sheet Excel工作簿
  151. * @return 返回附图map
  152. */
  153. public static PictureData getPictures2(XSSFSheet sheet, Integer row) throws IOException {
  154. List<POIXMLDocumentPart> list = sheet.getRelations();
  155. for (POIXMLDocumentPart part : list) {
  156. if (part instanceof XSSFDrawing) {
  157. XSSFDrawing drawing = (XSSFDrawing) part;
  158. List<XSSFShape> shapes = drawing.getShapes();
  159. for (XSSFShape shape : shapes) {
  160. //解决图片空指针报错问题 lig 2021-06-03
  161. XSSFClientAnchor anchor = (XSSFClientAnchor) shape.getAnchor();
  162. //XSSFClientAnchor anchor = picture.getPreferredSize();
  163. CTMarker marker = anchor.getFrom();
  164. int excelRow = marker.getRow();
  165. if (excelRow == row) {
  166. XSSFPicture picture = (XSSFPicture) shape;
  167. return picture.getPictureData();
  168. }
  169. }
  170. }
  171. }
  172. return null;
  173. }
  174. }