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

时间:2021-10-14 09:01:57

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...

在这里阅读所有图像......这里得到图像位置......

2 个解决方案

#1


4  

Have a look here:

看看这里:

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

http://poi.apache.org/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.

在此之后,您可以使用像ImageInfo这样的工具来扩展Magick以找出各种配置。哟甚至可以将图像转换为不同的大小。

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

- 希望这可以帮助

#2


0  

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

#1


4  

Have a look here:

看看这里:

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

http://poi.apache.org/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.

在此之后,您可以使用像ImageInfo这样的工具来扩展Magick以找出各种配置。哟甚至可以将图像转换为不同的大小。

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

- 希望这可以帮助

#2


0  

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