Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Is it possible to extract an image's 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 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?

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

like image 837
ThunderThrash Avatar asked Jul 20 '12 10:07

ThunderThrash


People also ask

What is XSSF and HSSF?

HSSF (Horrible Spreadsheet Format) − It is used to read and write xls format of MS-Excel files. XSSF (XML Spreadsheet Format) − It is used for xlsx file format of MS-Excel.

Does Apache POI help to read Excel file?

Apache POI XSSF implementation should be used for Excel 2007 OOXML (. xlsx) file format. Apache POI HSSF and XSSF API provides mechanisms to read, write or modify excel spreadsheets. Apache POI also provides SXSSF API that is an extension of XSSF to work with very large excel sheets.


2 Answers

Have a look here:

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.

Take a look at this class as well:

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

-- Hope this helps

like image 104
Mayank_Thapliyal Avatar answered Oct 29 '22 19:10

Mayank_Thapliyal


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());
like image 42
Vitali Sudnikovich Avatar answered Oct 29 '22 17:10

Vitali Sudnikovich