From 3003bb0f50cc56c33eb1b19d88689f73955c4be3 Mon Sep 17 00:00:00 2001 From: RuoYi Date: Fri, 13 Aug 2021 10:56:35 +0800 Subject: [PATCH] =?UTF-8?q?Excel=E6=B3=A8=E8=A7=A3=E5=9B=BE=E7=89=87?= =?UTF-8?q?=E5=AF=BC=E5=85=A5=E5=85=BC=E5=AE=B9xls?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/ruoyi/common/utils/poi/ExcelUtil.java | 62 ++++++++++++++++--- 1 file changed, 53 insertions(+), 9 deletions(-) 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 ccc7b205a..3d95f3175 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 getSheetPictrues(XSSFSheet sheet, XSSFWorkbook workbook) + 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 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 +}