ReadExcelUtils.java 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. package cn.cslg.pas.common.utils;
  2. import cn.cslg.pas.common.vo.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.io.File;
  10. import java.io.FileInputStream;
  11. import java.io.IOException;
  12. import java.io.InputStream;
  13. import java.text.SimpleDateFormat;
  14. import java.util.Date;
  15. import java.util.HashMap;
  16. import java.util.List;
  17. import java.util.Map;
  18. /**
  19. * @Author xiexiang
  20. * @Date 2023/5/30
  21. */
  22. @Service
  23. public class ReadExcelUtils {
  24. /**
  25. * 检测Excel文件合法性
  26. *
  27. * @param tempFile 临时文件
  28. * @return 返回文件总行数
  29. */
  30. public static Integer textExcel(File tempFile, String sourceId) throws IOException {
  31. //判断文件是否存在
  32. if (!tempFile.exists() || tempFile.getPath().trim().equals("")) {
  33. ThrowException.throwXiaoShiException("文件上传失败,服务器忙请稍后再试!");
  34. }
  35. // 检测是否为excel文件
  36. String suffix = tempFile.getPath().substring(tempFile.getPath().lastIndexOf("."));
  37. if (!suffix.equals(".xls") && !suffix.equals(".xlsx") && !suffix.equals(".XLS") && !suffix.equals(".XLSX")) {
  38. //删除临时文件tempFile
  39. new File(tempFile.getPath()).delete();
  40. ThrowException.throwXiaoShiException("文件格式错误,请上传Excel文件!");
  41. }
  42. InputStream fis = new FileInputStream(tempFile);
  43. //使用poi框架解析处理Excel文件
  44. Workbook workbook = null;
  45. //区分不同版本Excel,使用各自对应的工具类
  46. if (suffix.equals(".xls") || suffix.equals(".XLS")) {
  47. workbook = new HSSFWorkbook(fis);
  48. } else if (suffix.equals(".xlsx") || suffix.equals(".XLSX")) {
  49. workbook = new XSSFWorkbook(fis);
  50. }
  51. //读取第几个sheet
  52. Sheet sheet = workbook.getSheetAt(0);
  53. //读取总行数
  54. int rows = sheet.getPhysicalNumberOfRows();
  55. if (rows <= 1) {
  56. //删除临时文件tempFile
  57. fis.close();
  58. new File(tempFile.getPath()).delete();
  59. ThrowException.throwXiaoShiException("文件内容格式不正确,请检查总行数是否有专利内容");
  60. }
  61. //获取第一行抬头
  62. Row firstRow = sheet.getRow(0);
  63. boolean flag1 = false; //是否有 "公开(公告)号"
  64. boolean flag2 = false; //是否有 "申请号"
  65. //遍历第一行单元格抬头,检查合法性
  66. String title = "", source = "";
  67. if (sourceId.equals("1")) {
  68. source = "智慧芽";
  69. title = "公开(公告)号";
  70. } else if (sourceId.equals("2")) {
  71. source = "合享";
  72. title = "公开(公告)号";
  73. } else {
  74. source = "Patentics";
  75. title = "公开号";
  76. }
  77. for (Cell cell : firstRow) {
  78. if (cell.getStringCellValue().equals(title)) {
  79. flag1 = true;
  80. }
  81. if (cell.getStringCellValue().equals("申请号")) {
  82. flag2 = true;
  83. }
  84. }
  85. if (!flag1 || !flag2) {
  86. //删除临时文件tempFile
  87. fis.close();
  88. new File(tempFile.getPath()).delete();
  89. ThrowException.throwXiaoShiException("文件内容格式不正确,您选择【" + source + "】来源,Excel第一行抬头必须有【" + title + "】和【申请号】");
  90. }
  91. //关闭流
  92. fis.close();
  93. //返回文件总行数-1(即专利总数量)
  94. return rows - 1;
  95. }
  96. /**
  97. * 获取一行专利的全部数据(专利内容数据 + 摘要附图)
  98. *
  99. * @param tempFile Excel临时文件
  100. * @param row 行数
  101. * @return 返回装载专利数据(专利内容数据 + 摘要附图)的对象
  102. */
  103. public static PatentData readExcelOneRow(File tempFile, Sheet sheet, int row) throws IOException {
  104. //创建返回最终结果的对象 patentData
  105. PatentData patentData = new PatentData();
  106. //装载专利数据(除了摘要附图)的map:(key:表头如 "公开(公告)号" value:表头对应内容如 "CN1307082B")
  107. Map<Object, Object> map = new HashMap<>();
  108. //装载摘要附图的对象
  109. PictureData pictureData = null;
  110. //开始装载专利数据
  111. Row firstRow = sheet.getRow(0);
  112. Row needRow = sheet.getRow(row);
  113. //获得总列数
  114. int columns = firstRow.getLastCellNum();
  115. for (int i = 0; i < columns; i++) {
  116. map.put(firstRow.getCell(i) + "", ExcelUtils.getValue(needRow.getCell(i)) + "");
  117. }
  118. //开始装载专利摘要附图(判断用07还是03的方法获取图片)
  119. String suffix = tempFile.getName().substring(tempFile.getName().lastIndexOf("."));
  120. if (suffix.equals(".xls") || suffix.equals(".XLS")) {
  121. pictureData = getPictures1((HSSFSheet) sheet, row);
  122. } else if (suffix.equals(".xlsx") || suffix.equals(".XLSX")) {
  123. pictureData = getPictures2((XSSFSheet) sheet, row);
  124. }
  125. //返回结果对象装载结果
  126. patentData.setMap(map);
  127. patentData.setPictureData(pictureData);
  128. return patentData;
  129. }
  130. public static Sheet readExcel(File tempFile) {
  131. Sheet sheet = null;
  132. try {
  133. InputStream inputStream = new FileInputStream(tempFile);
  134. //POI可以处理Excel文件
  135. Workbook workbook = null;
  136. //当文件以.xls结尾时
  137. String suffix = tempFile.getName().substring(tempFile.getName().lastIndexOf("."));
  138. if (suffix.equals(".xls") || suffix.equals(".XLS")) {
  139. workbook = new HSSFWorkbook(inputStream);
  140. } else if (suffix.equals(".xlsx") || suffix.equals(".XLSX")) {
  141. workbook = new XSSFWorkbook(inputStream);
  142. }
  143. //读取第几个sheet
  144. sheet = workbook.getSheetAt(0);
  145. //关闭流
  146. inputStream.close();
  147. } catch (IOException e) {
  148. e.printStackTrace();
  149. }
  150. return sheet;
  151. }
  152. /**
  153. * 03版本Excel取附图
  154. *
  155. * @param sheet Excel工作簿
  156. * @return 返回附图map
  157. */
  158. public static PictureData getPictures1(HSSFSheet sheet, Integer row) throws IOException {
  159. if (sheet.getDrawingPatriarch() != null) {
  160. List<HSSFShape> list = sheet.getDrawingPatriarch().getChildren();
  161. for (HSSFShape shape : list) {
  162. if (shape instanceof HSSFPicture) {
  163. HSSFPicture picture = (HSSFPicture) shape;
  164. HSSFClientAnchor cAnchor = (HSSFClientAnchor) picture.getAnchor();
  165. int row1 = cAnchor.getRow1();
  166. if (row1 == row) {
  167. return picture.getPictureData();
  168. }
  169. }
  170. }
  171. }
  172. return null;
  173. }
  174. /**
  175. * 07版本Excel取附图
  176. *
  177. * @param sheet Excel工作簿
  178. * @return 返回附图map
  179. */
  180. public static PictureData getPictures2(XSSFSheet sheet, Integer row) throws IOException {
  181. List<POIXMLDocumentPart> list = sheet.getRelations();
  182. for (POIXMLDocumentPart part : list) {
  183. if (part instanceof XSSFDrawing) {
  184. XSSFDrawing drawing = (XSSFDrawing) part;
  185. List<XSSFShape> shapes = drawing.getShapes();
  186. for (XSSFShape shape : shapes) {
  187. //解决图片空指针报错问题 lig 2021-06-03
  188. XSSFClientAnchor anchor = (XSSFClientAnchor) shape.getAnchor();
  189. //XSSFClientAnchor anchor = picture.getPreferredSize();
  190. CTMarker marker = anchor.getFrom();
  191. int excelRow = marker.getRow();
  192. if (excelRow == row) {
  193. XSSFPicture picture = (XSSFPicture) shape;
  194. return picture.getPictureData();
  195. }
  196. }
  197. }
  198. }
  199. return null;
  200. }
  201. public static String getValue(Cell cell) {
  202. if (cell != null) {
  203. if (cell.getCellType() == CellType.NUMERIC && HSSFDateUtil.isCellDateFormatted(cell)) {
  204. Date date = cell.getDateCellValue();
  205. SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
  206. String dateString = dateFormat.format(date);
  207. return dateString;
  208. }
  209. else{
  210. return cell.toString();
  211. }
  212. }
  213. return "";
  214. }
  215. }