代码
主要工具类 需要引入依赖:
java
package com.chenkang.demo.util;
import cn.wps.officeDocument.x2017.etCustomData.CellImagesDocument;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.openxml4j.opc.PackagePart;
import org.apache.poi.openxml4j.opc.PackageRelationshipCollection;
import org.apache.poi.xssf.usermodel.XSSFPictureData;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
/**
* @author chenkang
* @since 2024/6/20 21:34
*/
public class WpsImageUtil {
/**
*图片ID和 XSSFPictureData
*
* @param workbook workbook
* @return List<Map < String, String>>
* @throws Exception 异常
*/
public static Map<String, XSSFPictureData> getPictureMap(XSSFWorkbook workbook) throws Exception {
OPCPackage opcPackage = workbook.getPackage();
Map<String,XSSFPictureData> result = new HashMap<>(4);
List<PackagePart> partsByContentType = opcPackage.getPartsByContentType("application/vnd.wps-officedocument.cellimage+xml");
if(partsByContentType.isEmpty()){
return result;
}
PackagePart packagePart = partsByContentType.get(0);
List<XSSFPictureData> allPictures = workbook.getAllPictures();
Map<String, String> ridAndPidMap = getRidAndPidMap(packagePart);
Map<String, String> ridAndPathMap = getRidAndPathMap(packagePart);
ridAndPidMap.forEach((key, value) -> {
String path = ridAndPathMap.get(value);
Optional<XSSFPictureData> first = allPictures.stream().filter(pictureData -> pictureData.getPackagePart().getPartName().getName().equals(path)).findFirst();
result.put(key,first.orElse(null));
});
return result;
}
/**
* //relationships 绑定了rid 和 图片 路径得地址
* 获取rid和path的关系
* @param packagePart cellImagePart
* @return Map
* @throws Exception 异常
*/
public static Map<String, String> getRidAndPathMap(PackagePart packagePart) throws Exception {
Map<String, String> ridAndPathMap = new HashMap<>(4);
PackageRelationshipCollection relationships = packagePart.getRelationships();
relationships.forEach(relationship -> ridAndPathMap.put(relationship.getId(), relationship.getTargetURI().getPath()));
return ridAndPathMap;
}
/**
* {ID_581F75328A584939A51CC44E17945975:rid1,ID_6702DEA2ADBA44AE8C65065BD13FF23D:rid1}
* 行rid 以及图片id关系
*
* @param cellImagePart cellImagePart
* @return Map
* @throws Exception 异常
*/
public static Map<String, String> getRidAndPidMap(PackagePart cellImagePart) throws Exception {
CellImagesDocument cellImagesDocument = CellImagesDocument.Factory.parse(cellImagePart.getInputStream());
CellImagesDocument.CellImages cellImages = cellImagesDocument.getCellImages();
Map<String, String> result = new HashMap<>(4);
cellImages.getCellImageList().forEach(cellImage -> {
result.put(cellImage.getPic().getNvPicPr().getCNvPr().getName().getStringValue(), cellImage.getPic().getBlipFill().getBlip().getEmbed());
});
return result;
}
}
java
package com.chenkang.demo.util;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class StringExtractor {
public static String extractID(String input) {
// 定义正则表达式模式
String pattern = "ID_[A-Z0-9]+";
Pattern compiledPattern = Pattern.compile(pattern);
Matcher matcher = compiledPattern.matcher(input);
// 查找匹配的字符串
if (matcher.find()) {
return matcher.group();
} else {
return null;
}
}
}
测试:
java
package com.chenkang.demo.learn;
import com.chenkang.demo.util.StringExtractor;
import com.chenkang.demo.util.WpsImageUtil;
import org.apache.poi.xssf.usermodel.XSSFPictureData;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.File;
import java.util.Map;
/**
* @author chenkang
* @since 2024/6/6 14:09
*/
public class LockLearn {
public static void main(String[] args) throws Exception {
File file = new File("C:\\Users\\18151\\Desktop\\test.xlsx");
XSSFWorkbook sheets = new XSSFWorkbook(file);
XSSFSheet sheetAt = sheets.getSheetAt(0);
String id=sheetAt.getRow(1).getCell(1).getStringCellValue();
Map<String, XSSFPictureData> pictureMap = WpsImageUtil.getPictureMap(sheets);
System.out.println(pictureMap);
System.out.println(pictureMap.get(StringExtractor.extractID(id)));
}
}
项目需要引入依赖, 可以手动打入maven仓库帮助解析wps图片信息
链接:https://pan.baidu.com/s/1CVf0kArBevXJ23q0q5y01Q?pwd=tc3h
提取码:tc3h
--来自百度网盘超级会员V6的分享
具体思路详见文章java读取wps嵌入式图片思路-CSDN博客