您的位置:首页 > 房产 > 家装 > java 利用poi读取wps嵌入式图片,自测

java 利用poi读取wps嵌入式图片,自测

2024/12/23 8:40:58 来源:https://blog.csdn.net/weixin_38845058/article/details/139843208  浏览:    关键词:java 利用poi读取wps嵌入式图片,自测
  1. 代码

        主要工具类 需要引入依赖:

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;}}
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;}}
}

测试:

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博客

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com