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

代码

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

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

相关推荐
2401_857439692 小时前
SSM 架构下 Vue 电脑测评系统:为电脑性能评估赋能
开发语言·php
SoraLuna3 小时前
「Mac畅玩鸿蒙与硬件47」UI互动应用篇24 - 虚拟音乐控制台
开发语言·macos·ui·华为·harmonyos
xlsw_3 小时前
java全栈day20--Web后端实战(Mybatis基础2)
java·开发语言·mybatis
神仙别闹4 小时前
基于java的改良版超级玛丽小游戏
java
Dream_Snowar4 小时前
速通Python 第三节
开发语言·python
黄油饼卷咖喱鸡就味增汤拌孜然羊肉炒饭4 小时前
SpringBoot如何实现缓存预热?
java·spring boot·spring·缓存·程序员
暮湫5 小时前
泛型(2)
java
超爱吃士力架5 小时前
邀请逻辑
java·linux·后端
南宫生5 小时前
力扣-图论-17【算法学习day.67】
java·学习·算法·leetcode·图论
转码的小石5 小时前
12/21java基础
java