|
@@ -1,18 +1,17 @@
|
|
|
package cn.cslg.pas.common.utils;
|
|
|
|
|
|
import cn.cslg.pas.domain.PatentData;
|
|
|
-import org.apache.poi.hssf.usermodel.HSSFPicture;
|
|
|
-import org.apache.poi.hssf.usermodel.HSSFShape;
|
|
|
-import org.apache.poi.hssf.usermodel.HSSFSheet;
|
|
|
-import org.apache.poi.hssf.usermodel.HSSFWorkbook;
|
|
|
+import org.apache.poi.hssf.usermodel.*;
|
|
|
import org.apache.poi.ooxml.POIXMLDocumentPart;
|
|
|
import org.apache.poi.ss.usermodel.PictureData;
|
|
|
import org.apache.poi.ss.usermodel.Row;
|
|
|
import org.apache.poi.ss.usermodel.Sheet;
|
|
|
import org.apache.poi.ss.usermodel.Workbook;
|
|
|
import org.apache.poi.xssf.usermodel.*;
|
|
|
+import org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTMarker;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
+import java.awt.*;
|
|
|
import java.io.*;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.List;
|
|
@@ -99,7 +98,7 @@ public class ReadExcelUtils {
|
|
|
Sheet sheet = workbook.getSheetAt(0);
|
|
|
//读取总行数
|
|
|
int rows = sheet.getPhysicalNumberOfRows();
|
|
|
- if (rows - 1 <= row) {
|
|
|
+ if (rows <= row) {
|
|
|
ThrowException.throwXiaoShiException("row超出Excel文档中数量");
|
|
|
}
|
|
|
|
|
@@ -114,9 +113,9 @@ public class ReadExcelUtils {
|
|
|
|
|
|
//开始装载专利摘要附图(判断用07还是03的方法获取图片)
|
|
|
if (filePath.endsWith(".xls") || filePath.endsWith(".XLS")) {
|
|
|
- pictureData = getPictures1((HSSFSheet) sheet, row - 1);
|
|
|
+ pictureData = getPictures1((HSSFSheet) sheet, row);
|
|
|
} else if (filePath.endsWith(".xlsx") || filePath.endsWith(".XLSX")) {
|
|
|
- pictureData = getPictures2((XSSFSheet) sheet, row - 1);
|
|
|
+ pictureData = getPictures2((XSSFSheet) sheet, row);
|
|
|
}
|
|
|
workbook.close();
|
|
|
|
|
@@ -140,10 +139,15 @@ public class ReadExcelUtils {
|
|
|
public static PictureData getPictures1(HSSFSheet sheet, Integer row) throws IOException {
|
|
|
if (sheet.getDrawingPatriarch() != null) {
|
|
|
List<HSSFShape> list = sheet.getDrawingPatriarch().getChildren();
|
|
|
- HSSFShape shape = list.get(row);
|
|
|
- if (shape instanceof HSSFPicture) {
|
|
|
- HSSFPicture picture = (HSSFPicture) shape;
|
|
|
- return picture.getPictureData();
|
|
|
+ for (HSSFShape shape : list) {
|
|
|
+ if (shape instanceof HSSFPicture) {
|
|
|
+ HSSFPicture picture = (HSSFPicture) shape;
|
|
|
+ HSSFClientAnchor cAnchor = (HSSFClientAnchor) picture.getAnchor();
|
|
|
+ int row1 = cAnchor.getRow1();
|
|
|
+ if (row1 == row) {
|
|
|
+ return picture.getPictureData();
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
return null;
|
|
@@ -161,12 +165,20 @@ public class ReadExcelUtils {
|
|
|
if (part instanceof XSSFDrawing) {
|
|
|
XSSFDrawing drawing = (XSSFDrawing) part;
|
|
|
List<XSSFShape> shapes = drawing.getShapes();
|
|
|
- //获取指定行row的图片形状
|
|
|
- XSSFShape shape = shapes.get(row);
|
|
|
- XSSFPicture picture = (XSSFPicture) shape;
|
|
|
- return picture.getPictureData();
|
|
|
+ for (XSSFShape shape : shapes) {
|
|
|
+ //解决图片空指针报错问题 lig 2021-06-03
|
|
|
+ XSSFClientAnchor anchor = (XSSFClientAnchor) shape.getAnchor();
|
|
|
+ //XSSFClientAnchor anchor = picture.getPreferredSize();
|
|
|
+ CTMarker marker = anchor.getFrom();
|
|
|
+ int key = marker.getRow();
|
|
|
+ if (key == row) {
|
|
|
+ XSSFPicture picture = (XSSFPicture) shape;
|
|
|
+ return picture.getPictureData();
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
return null;
|
|
|
}
|
|
|
|