java 使用 Apache POI 从 excel 文件中获取图像及其位置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11577274/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 05:38:57  来源:igfitidea点击:

Get an image and its position from excel file using Apache POI

javaexcelapache-poi

提问by ThunderThrash

Is it possible to extract an image's information from an xls spreadsheet using Apache POI?

是否可以使用 Apache POI 从 xls 电子表格中提取图像信息?

In one of my projects, I need to read some images from a .xls file. I can read all images together, but how can I get images position (like columns and rows number or coordinates)? Otherwise I can get images position but I can't know information, like picture name or extension or others, about a specific image at the positions found. How I can get images and positions too?

在我的一个项目中,我需要从 .xls 文件中读取一些图像。我可以一起读取所有图像,但如何获取图像位置(如列和行号或坐标)?否则我可以获得图像位置,但我无法知道有关找到的位置的特定图像的信息,例如图片名称或扩展名或其他信息。我如何才能获得图像和位置?

Here read all images...and here get images positions...

在这里阅读所有图像......并在这里获取图像位置......

回答by Mayank_Thapliyal

Have a look here:

看看这里:

http://poi.apache.org/components/spreadsheet/quick-guide.html#Images

http://poi.apache.org/components/spreadsheet/quick-guide.html#Images

Sample:

样本:

List lst = workbook.getAllPictures();

for (Iterator it = lst.iterator(); it.hasNext(); ) {

    PictureData pict = (PictureData)it.next();

    String ext = pict.suggestFileExtension();

    byte[] data = pict.getData();

    if (ext.equals("jpeg")) {

      FileOutputStream out = new FileOutputStream("pict.jpg");

      out.write(data);

      out.close();

    }
}

After this, you can use tools like ImageInfo which extends Magick to find out various configs. Yo can even convert images to different sizes.

在此之后,您可以使用扩展 Magick 的 ImageInfo 之类的工具来查找各种配置。你甚至可以将图像转换为不同的尺寸。

Take a look at this class as well:

看看这个类:

http://blog.jaimon.co.uk/simpleimageinfo/SimpleImageInfo.java.html

http://blog.jaimon.co.uk/simpleimageinfo/SimpleImageInfo.java.html

-- Hope this helps

- 希望这可以帮助

回答by Vitali Sudnikovich

I hope this code will help)

我希望这段代码会有所帮助)

    XSSFDrawing dp = workbook.getSheetAt(1).createDrawingPatriarch();
    List<XSSFShape> pics = dp.getShapes();
    XSSFPicture inpPic = (XSSFPicture)pics.get(0);

    XSSFClientAnchor clientAnchor = inpPic.getClientAnchor();
    inpPic.getShapeName(); // узнаю название картинки
    PictureData pict = inpPic.getPictureData();
    FileOutputStream out = new FileOutputStream("pict.jpg");
    byte[] data = pict.getData();
    out.write(data);
    out.close();
    System.out.println("col1: " + clientAnchor.getCol1() + ", col2: " + clientAnchor.getCol2() + ", row1: " + clientAnchor.getRow1() + ", row2: " + clientAnchor.getRow2());
    System.out.println("x1: " + clientAnchor.getDx1() + ", x2: " + clientAnchor.getDx2() +  ", y1: " + clientAnchor.getDy1() +  ", y2: " + clientAnchor.getDy2());

回答by Vasil Valchev

And if you don`t want to use iterator (cause some times they are heavy)

如果你不想使用迭代器(因为有时它们很重)

public List readDrawing(Workbook workbook) throws Exception {
    List list = workbook.getAllPictures();

    for (int i=0; i<list.size(); i++) {
        PictureData picture = (PictureData) list.get(i);

        String ext = picture.suggestFileExtension();
        byte[] data = picture.getData();

        FileOutputStream out = new FileOutputStream("imageName" + "." + ext);
        out.write(data);
        out.close();
    }

    return list;
}