最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

利用Java實(shí)現(xiàn)讀取WPS?Excel中嵌入的圖片

 更新時(shí)間:2024年11月17日 10:21:37   作者:猴子請(qǐng)來(lái)的坑逼  
許多數(shù)據(jù)文件中可能包含嵌入式圖片,這些圖片對(duì)于數(shù)據(jù)分析和可視化非常重要,下面我們就來(lái)看看如何使用Java讀取WPS?Excel中嵌入的圖片吧

引言

許多數(shù)據(jù)文件中可能包含嵌入式圖片,這些圖片對(duì)于數(shù)據(jù)分析和可視化非常重要。然而,從 WPS 在 Excel 中讀取這些圖片可能會(huì)有一些技術(shù)挑戰(zhàn)。在本文中,我將展示如何從 WPS Excel 文件中讀取嵌入的圖片,并提供代碼示例。

提取圖片資源的方法

以下是用于從 WPS Excel 文件中讀取圖片資源的方法。我們使用了 Apache POI 庫(kù)來(lái)處理 Excel 文件,并使用了 cn.hutool.json 庫(kù)來(lái)處理 XML 數(shù)據(jù)。這些庫(kù)可以幫助我們解析 Excel 文件中的嵌入式圖片。

主函數(shù)

首先,我們定義了主函數(shù) main,用于測(cè)試圖片提取功能:

public static void main(String[] args) {
    PicturesUtils picturesUtils = new PicturesUtils();
    byte[] fileData = picturesUtils.getFileStream(new File("你的文件路徑"));
    Map<String, XSSFPictureData> pictures = picturesUtils.getPictures(fileData);
    pictures.forEach((id, xssfPictureData) -> {
        System.out.println("id:" + id);
        String fileName = xssfPictureData.getPackagePart().getPartName().getName();
        System.out.println("fileName:" + fileName);
        File file = new File("D:\\" + fileName);
        File dir = file.getParentFile();
        dir.mkdirs();
        try {
            Files.write(Path.of(file.getPath()), xssfPictureData.getData());
        } catch (IOException e) {
            e.printStackTrace();
        }
    });
}

在這個(gè)函數(shù)中,我們創(chuàng)建了一個(gè) PicturesUtils 實(shí)例,并從文件路徑中獲取文件數(shù)據(jù)。然后,我們調(diào)用 getPictures 方法來(lái)提取圖片資源,并遍歷結(jié)果將圖片寫(xiě)入本地文件。

獲取浮動(dòng)圖片

接下來(lái),我們展示了如何從 Excel 文件中獲取浮動(dòng)圖片:

public static Map<String, XSSFPictureData> getFloatingPictures(XSSFSheet xssfSheet) {
    Map<String, XSSFPictureData> mapFloatingPictures = new HashMap<>();
    XSSFDrawing drawingPatriarch = xssfSheet.getDrawingPatriarch();
    if (drawingPatriarch != null) {
        List<XSSFShape> shapes = drawingPatriarch.getShapes();
        for (XSSFShape shape : shapes) {
            if (shape instanceof XSSFPicture picture) {
                XSSFClientAnchor anchor = (XSSFClientAnchor) picture.getAnchor();
                XSSFPictureData pictureData = picture.getPictureData();
                String key = anchor.getRow1() + "-" + anchor.getCol1();
                mapFloatingPictures.put(key, pictureData);
            }
        }
    }
    return mapFloatingPictures;
}

該方法接收一個(gè) XSSFSheet 對(duì)象,并返回一個(gè)包含浮動(dòng)圖片的映射。它遍歷工作表中的所有形狀,并將圖片數(shù)據(jù)存儲(chǔ)在映射中。

處理圖片數(shù)據(jù)

最后,我們展示了如何處理 Excel 文件中的圖片數(shù)據(jù),包括嵌入式圖片和浮動(dòng)式圖片:

public Map<String, XSSFPictureData> getPictures(byte[] data) {
    try {
        Map<String, String> mapConfig = processZipEntries(new ByteArrayInputStream(data));
        Map<String, XSSFPictureData> mapPictures = processPictures(new ByteArrayInputStream(data), mapConfig);
        Iterator<Sheet> sheetIterator = WorkbookFactory.create(new ByteArrayInputStream(data)).sheetIterator();
        while (sheetIterator.hasNext()) {
            mapPictures.putAll(getFloatingPictures((XSSFSheet) sheetIterator.next()));
        }
        return mapPictures;
    } catch (IOException e) {
        return new HashedMap<>();
    }
}

在該方法中,我們使用 processZipEntries 和 processPictures 方法來(lái)處理 Zip 文件中的條目和圖片數(shù)據(jù)。然后,通過(guò)遍歷 Excel 文件中的所有工作表,獲取浮動(dòng)圖片。

完整代碼

實(shí)際在程序中讀取的內(nèi)容為=DISPIMG(“ID_03BC802DDAB24510A9883DB157EAC0F8”,1)公式

將公式中的id提取出來(lái)ID_03BC802DDAB24510A9883DB157EAC0F8,最后就可以使用下面方法拿到當(dāng)前id獲取到的圖片資源 jdk版本17

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.XML;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.map.HashedMap;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.openxml4j.opc.PackagePartName;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.apache.poi.xssf.usermodel.*;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;


/**
 * @author bianhl
 * @version 1.0
 * @description 獲取圖片資源
 * @date 2024年4月28日08:44:42
 */
@Slf4j
public class PicturesUtils {

    public static void main(String[] args) {
        PicturesUtils picturesUtils = new PicturesUtils();
        byte[] fileData = picturesUtils.getFileStream(new File("你的文件路徑"));
        Map<String, XSSFPictureData> pictures = picturesUtils.getPictures(fileData);
        pictures.forEach((id, xssfPictureData) -> {
            System.out.println("id:" + id);
            String fileName = xssfPictureData.getPackagePart().getPartName().getName();
            System.out.println("fileName:" + fileName);
            File file = new File("C:\\Users\\XXX\\Desktop\\" + fileName);
            File dir = file.getParentFile();
            dir.mkdirs();
            try {
                Files.write(Path.of(file.getPath()), xssfPictureData.getData());
            } catch (IOException e) {
                e.printStackTrace();
            }
        });
    }

    /**
     * 獲取浮動(dòng)圖片,以 map 形式返回,鍵為行列格式 x-y。
     *
     * @param xssfSheet WPS 工作表
     * @return 浮動(dòng)圖片的 map
     */
    public static Map<String, XSSFPictureData> getFloatingPictures(XSSFSheet xssfSheet) {
        Map<String, XSSFPictureData> mapFloatingPictures = new HashMap<>();
        XSSFDrawing drawingPatriarch = xssfSheet.getDrawingPatriarch();
        if (drawingPatriarch != null) {
            List<XSSFShape> shapes = drawingPatriarch.getShapes();
            for (XSSFShape shape : shapes) {
                if (shape instanceof XSSFPicture picture) {
                    XSSFClientAnchor anchor = (XSSFClientAnchor) picture.getAnchor();
                    XSSFPictureData pictureData = picture.getPictureData();
                    String key = anchor.getRow1() + "-" + anchor.getCol1();
                    mapFloatingPictures.put(key, pictureData);
                }
            }
        }
        return mapFloatingPictures;
    }

    /**
     * 處理 WPS 文件中的圖片數(shù)據(jù),返回圖片信息 map。
     *
     * @param stream    輸入流
     * @param mapConfig 配置映射
     * @return 圖片信息的 map
     * @throws IOException
     */
    private Map<String, XSSFPictureData> processPictures(ByteArrayInputStream stream, Map<String, String> mapConfig) throws IOException {
        Map<String, XSSFPictureData> mapPictures = new HashedMap<>();
        Workbook workbook = WorkbookFactory.create(stream);
        List<XSSFPictureData> allPictures = (List<XSSFPictureData>) workbook.getAllPictures();
        for (XSSFPictureData pictureData : allPictures) {
            PackagePartName partName = pictureData.getPackagePart().getPartName();
            String uri = partName.getURI().toString();
            if (mapConfig.containsKey(uri)) {
                String strId = mapConfig.get(uri);
                mapPictures.put(strId, pictureData);
            }
        }
        return mapPictures;
    }

    /**
     * 獲取 WPS 文檔中的圖片,包括嵌入式圖片和浮動(dòng)式圖片。
     *
     * @param data 二進(jìn)制數(shù)據(jù)
     * @return 圖片信息的 map
     * @throws IOException
     */
    public Map<String, XSSFPictureData> getPictures(byte[] data) {
        try {
            Map<String, String> mapConfig = processZipEntries(new ByteArrayInputStream(data));
            Map<String, XSSFPictureData> mapPictures = processPictures(new ByteArrayInputStream(data), mapConfig);
            Iterator<Sheet> sheetIterator = WorkbookFactory.create(new ByteArrayInputStream(data)).sheetIterator();
            while (sheetIterator.hasNext()) {
                mapPictures.putAll(getFloatingPictures((XSSFSheet) sheetIterator.next()));
            }
            return mapPictures;
        } catch (IOException e) {
            return new HashedMap<>();
        }
    }

    /**
     * 處理 Zip 文件中的條目,更新圖片配置信息。
     *
     * @param stream Zip 輸入流
     * @return 配置信息的 map
     * @throws IOException
     */
    private Map<String, String> processZipEntries(ByteArrayInputStream stream) throws IOException {
        Map<String, String> mapConfig = new HashedMap<>();
        ZipInputStream zipInputStream = new ZipInputStream(stream);
        ZipEntry zipEntry;
        while ((zipEntry = zipInputStream.getNextEntry()) != null) {
            try {
                final String fileName = zipEntry.getName();
                if ("xl/cellimages.xml".equals(fileName)) {
                    processCellImages(zipInputStream, mapConfig);
                } else if ("xl/_rels/cellimages.xml.rels".equals(fileName)) {
                    return processCellImagesRels(zipInputStream, mapConfig);
                }
            } finally {
                zipInputStream.closeEntry();
            }
        }
        return new HashedMap<>();
    }

    /**
     * 處理 Zip 文件中的 cellimages.xml 文件,更新圖片配置信息。
     *
     * @param zipInputStream Zip 輸入流
     * @param mapConfig      配置信息的 map
     * @throws IOException
     */
    private void processCellImages(ZipInputStream zipInputStream, Map<String, String> mapConfig) throws IOException {
        String content = IOUtils.toString(zipInputStream, StandardCharsets.UTF_8);
        JSONObject jsonObject = XML.toJSONObject(content);
        if (jsonObject != null) {
            JSONObject cellImages = jsonObject.getJSONObject("etc:cellImages");
            if (cellImages != null) {
                JSONArray cellImageArray = null;
                Object cellImage = cellImages.get("etc:cellImage");
                if (cellImage != null && cellImage instanceof JSONArray) {
                    cellImageArray = (JSONArray) cellImage;
                } else if (cellImage != null && cellImage instanceof JSONObject cellImageObj) {
                    if (cellImageObj != null) {
                        cellImageArray = new JSONArray();
                        cellImageArray.add(cellImageObj);
                    }
                }
                if (cellImageArray != null) {
                    processImageItems(cellImageArray, mapConfig);
                }
            }
        }
    }

    /**
     * 處理 cellImageArray 中的圖片項(xiàng),更新圖片配置信息。
     *
     * @param cellImageArray 圖片項(xiàng)的 JSONArray
     * @param mapConfig      配置信息的 map
     */
    private void processImageItems(JSONArray cellImageArray, Map<String, String> mapConfig) {
        for (int i = 0; i < cellImageArray.size(); i++) {
            JSONObject imageItem = cellImageArray.getJSONObject(i);
            if (imageItem != null) {
                JSONObject pic = imageItem.getJSONObject("xdr:pic");
                if (pic != null) {
                    processPic(pic, mapConfig);
                }
            }
        }
    }

    /**
     * 處理 pic 中的圖片信息,更新圖片配置信息。
     *
     * @param pic       圖片的 JSONObject
     * @param mapConfig 配置信息的 map
     */
    private void processPic(JSONObject pic, Map<String, String> mapConfig) {
        JSONObject nvPicPr = pic.getJSONObject("xdr:nvPicPr");
        if (nvPicPr != null) {
            JSONObject cNvPr = nvPicPr.getJSONObject("xdr:cNvPr");
            if (cNvPr != null) {
                String name = cNvPr.getStr("name");
                if (StringUtils.isNotEmpty(name)) {
                    String strImageEmbed = updateImageEmbed(pic);
                    if (strImageEmbed != null) {
                        mapConfig.put(strImageEmbed, name);
                    }
                }
            }
        }
    }

    /**
     * 獲取嵌入式圖片的 embed 信息。
     *
     * @param pic 圖片的 JSONObject
     * @return embed 信息
     */
    private String updateImageEmbed(JSONObject pic) {
        JSONObject blipFill = pic.getJSONObject("xdr:blipFill");
        if (blipFill != null) {
            JSONObject blip = blipFill.getJSONObject("a:blip");
            if (blip != null) {
                return blip.getStr("r:embed");
            }
        }
        return null;
    }

    /**
     * 處理 Zip 文件中的 relationship 條目,更新配置信息。
     *
     * @param zipInputStream Zip 輸入流
     * @param mapConfig      配置信息的 map
     * @return 配置信息的 map
     * @throws IOException
     */
    private Map<String, String> processCellImagesRels(ZipInputStream zipInputStream, Map<String, String> mapConfig) throws IOException {
        String content = IOUtils.toString(zipInputStream, StandardCharsets.UTF_8);
        JSONObject jsonObject = XML.toJSONObject(content);
        JSONObject relationships = jsonObject.getJSONObject("Relationships");
        if (relationships != null) {
            JSONArray relationshipArray = null;
            Object relationship = relationships.get("Relationship");

            if (relationship != null && relationship instanceof JSONArray) {
                relationshipArray = (JSONArray) relationship;
            } else if (relationship != null && relationship instanceof JSONObject relationshipObj) {
                if (relationshipObj != null) {
                    relationshipArray = new JSONArray();
                    relationshipArray.add(relationshipObj);
                }
            }
            if (relationshipArray != null) {
                return processRelationships(relationshipArray, mapConfig);
            }
        }
        return null;
    }

    /**
     * 處理 relationshipArray 中的關(guān)系項(xiàng),更新配置信息。
     *
     * @param relationshipArray 關(guān)系項(xiàng)的 JSONArray
     * @param mapConfig         配置信息的 map
     * @return 配置信息的 map
     */
    private Map<String, String> processRelationships(JSONArray relationshipArray, Map<String, String> mapConfig) {
        Map<String, String> mapRelationships = new HashedMap<>();
        for (int i = 0; i < relationshipArray.size(); i++) {
            JSONObject relaItem = relationshipArray.getJSONObject(i);
            if (relaItem != null) {
                String id = relaItem.getStr("Id");
                String value = "/xl/" + relaItem.getStr("Target");
                if (mapConfig.containsKey(id)) {
                    String strImageId = mapConfig.get(id);
                    mapRelationships.put(value, strImageId);
                }
            }
        }
        return mapRelationships;
    }

    /**
     * @param file 數(shù)據(jù)文件
     * @return {@link byte[]}
     * @description
     * @author bianhl
     * @date 2024/4/26 13:52
     */
    private byte[] getFileStream(File file) {
        try (InputStream inputStream = new FileInputStream(file)) {
            // 創(chuàng)建 ByteArrayOutputStream 來(lái)暫存流數(shù)據(jù)
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            // 將 inputStream 讀取到 byteArrayOutputStream 中
            byte[] buffer = new byte[1024];
            int length;
            while ((length = inputStream.read(buffer)) != -1) {
                byteArrayOutputStream.write(buffer, 0, length);
            }
            // 將 byteArrayOutputStream 的內(nèi)容獲取為字節(jié)數(shù)組
            return byteArrayOutputStream.toByteArray();
        } catch (IOException e) {
            return null;
        }
    }


}

輸出結(jié)果:

拿到的圖片數(shù)據(jù)

以上就是利用Java實(shí)現(xiàn)讀取WPS Excel中嵌入的圖片的詳細(xì)內(nèi)容,更多關(guān)于Java讀取Excel嵌入的圖片的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評(píng)論

琼结县| 石嘴山市| 甘南县| 漾濞| 昌平区| 尉氏县| 田阳县| 龙泉市| 绵竹市| 贵阳市| 杂多县| 乌恰县| 绥德县| 玉溪市| 翁源县| 祁东县| 连城县| 安徽省| 尖扎县| 蒙自县| 武穴市| 浦城县| 乾安县| 灵台县| 宜城市| 应城市| 读书| 南充市| 泰安市| 五寨县| 金阳县| 海宁市| 张家川| 三亚市| 长治县| 江北区| 晋州市| 涪陵区| 灵台县| 三江| 郴州市|