diff --git a/ruoyi-common/src/main/java/com/ruoyi/common/utils/poi/ExcelUtil.java b/ruoyi-common/src/main/java/com/ruoyi/common/utils/poi/ExcelUtil.java index ccc7b20..3d95f31 100644 --- a/ruoyi-common/src/main/java/com/ruoyi/common/utils/poi/ExcelUtil.java +++ b/ruoyi-common/src/main/java/com/ruoyi/common/utils/poi/ExcelUtil.java @@ -19,6 +19,12 @@ import java.util.Set; import java.util.UUID; import java.util.stream.Collectors; import javax.servlet.http.HttpServletResponse; +import org.apache.poi.hssf.usermodel.HSSFClientAnchor; +import org.apache.poi.hssf.usermodel.HSSFPicture; +import org.apache.poi.hssf.usermodel.HSSFPictureData; +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.ooxml.POIXMLDocumentPart; import org.apache.poi.ss.usermodel.BorderStyle; import org.apache.poi.ss.usermodel.Cell; @@ -185,7 +191,16 @@ public class ExcelUtil { throw new IOException("文件sheet不存在"); } - Map pictures = getSheetPictrues((XSSFSheet) sheet, (XSSFWorkbook) wb); + boolean isXSSFWorkbook = !(wb instanceof HSSFWorkbook); + Map pictures; + if (isXSSFWorkbook) + { + pictures = getSheetPictrues07((XSSFSheet) sheet, (XSSFWorkbook) wb); + } + else + { + pictures = getSheetPictrues03((HSSFSheet) sheet, (HSSFWorkbook) wb); + } // 获取最后一个非空行的行下标,比如总行数为n,则返回的为n-1 int rows = sheet.getLastRowNum(); @@ -318,12 +333,8 @@ public class ExcelUtil { val = reverseDictByExp(Convert.toStr(val), attr.dictType(), attr.separator()); } - else if (ColumnType.IMAGE == attr.cellType()) + else if (ColumnType.IMAGE == attr.cellType() && StringUtils.isNotEmpty(pictures)) { - if (StringUtils.isNull(pictures)) - { - val = ""; - } PictureData image = pictures.get(row.getRowNum() + "_" + entry.getKey()); if (image == null) { @@ -1179,13 +1190,46 @@ public class ExcelUtil } /** - * 获取Excel图片 + * 获取Excel2003图片 + * + * @param sheet 当前sheet对象 + * @param workbook 工作簿对象 + * @return Map key:图片单元格索引(1_1)String,value:图片流PictureData + */ + public static Map getSheetPictrues03(HSSFSheet sheet, HSSFWorkbook workbook) + { + Map sheetIndexPicMap = new HashMap(); + List pictures = workbook.getAllPictures(); + if (!pictures.isEmpty()) + { + for (HSSFShape shape : sheet.getDrawingPatriarch().getChildren()) + { + HSSFClientAnchor anchor = (HSSFClientAnchor) shape.getAnchor(); + if (shape instanceof HSSFPicture) + { + HSSFPicture pic = (HSSFPicture) shape; + int pictureIndex = pic.getPictureIndex() - 1; + HSSFPictureData picData = pictures.get(pictureIndex); + String picIndex = String.valueOf(anchor.getRow1()) + "_" + String.valueOf(anchor.getCol1()); + sheetIndexPicMap.put(picIndex, picData); + } + } + return sheetIndexPicMap; + } + else + { + return sheetIndexPicMap; + } + } + + /** + * 获取Excel2007图片 * * @param sheet 当前sheet对象 * @param workbook 工作簿对象 * @return Map key:图片单元格索引(1_1)String,value:图片流PictureData */ - public static Map getSheetPictrues(XSSFSheet sheet, XSSFWorkbook workbook) + public static Map getSheetPictrues07(XSSFSheet sheet, XSSFWorkbook workbook) { Map sheetIndexPicMap = new HashMap(); for (POIXMLDocumentPart dr : sheet.getRelations()) @@ -1209,4 +1253,4 @@ public class ExcelUtil } return sheetIndexPicMap; } -} \ No newline at end of file +}