ReadExcelUtils.java 7.2 KB

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