ReadExcelUtils.java 11 KB

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