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

相关推荐
王俊山IT8 分钟前
C++学习笔记----10、模块、头文件及各种主题(一)---- 模块(5)
开发语言·c++·笔记·学习
为将者,自当识天晓地。10 分钟前
c++多线程
java·开发语言
小政爱学习!12 分钟前
封装axios、环境变量、api解耦、解决跨域、全局组件注入
开发语言·前端·javascript
daqinzl18 分钟前
java获取机器ip、mac
java·mac·ip
k093327 分钟前
sourceTree回滚版本到某次提交
开发语言·前端·javascript
激流丶33 分钟前
【Kafka 实战】如何解决Kafka Topic数量过多带来的性能问题?
java·大数据·kafka·topic
神奇夜光杯35 分钟前
Python酷库之旅-第三方库Pandas(202)
开发语言·人工智能·python·excel·pandas·标准库及第三方库·学习与成长
Themberfue37 分钟前
Java多线程详解⑤(全程干货!!!)线程安全问题 || 锁 || synchronized
java·开发语言·线程·多线程·synchronized·
plmm烟酒僧39 分钟前
Windows下QT调用MinGW编译的OpenCV
开发语言·windows·qt·opencv
测试界的酸菜鱼1 小时前
Python 大数据展示屏实例
大数据·开发语言·python