Get image and its position from excel file using Apache POI

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

In one of my projects, I need to read some images from a .xls file. I can read all the images together, but how can I get the position of the image (for example, the number of columns and the number of rows or coordinates)? Otherwise, I can get the position of the image, but I can not know information, such as the name of the image or extension or others, about a particular image in the positions found. How can I get images and positions too?

Read all images here ... and get image positions here ...

+6
source share
2 answers

Look at here:

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

Example:

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 that, you can use tools like ImageInfo, which extends Magic to find out different configurations. Yo can even convert images to different sizes.

See also this class:

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

- Hope this helps

+4
source

And if you do not want to use an iterator (because sometimes 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; } 
0
source

Source: https://habr.com/ru/post/920950/


All Articles