Get image position in Apache POI from Excel xls HSSF

My need is to get the image data in bytes, then bind the details and finally display the result on the screen in the target position and size for my users ...

The API shows that we can get a list of all the images using the workbook object as follows:

List<HSSFPictureData> picturesData = workbook.getAllPictures(); 

and also we can get all the details of the binding (see details of HSSFClientAnchor here ).

The problem is that the HSSFPicture.getPictureIndex () referenced by the anchor does not match the index of the HSSFPictureData collection of the getAllPictures () method above ...

... now the question arises: how to match instances of HSSFClientAnchor with specific instances of HSSFPictureData or vice versa?

+2
source share
1 answer
 List<HSSFShape> shapes = this.tSheet.sheet().getDrawingPatriarch().getChildren(); for (int i = 0; i < shapes.size(); i++) { if(shapes.get(i) instanceof HSSFPicture) { HSSFPicture pic = (HSSFPicture) shapes.get(i); HSSFPictureData picdata = this.tSheet.sheet().getWorkbook().getAllPictures().get(pic.getPictureIndex()); int pictureIndex = this.newSheet.getWorkbook().addPicture( picdata.getData(), picdata.getFormat()); this.newSheet.createDrawingPatriarch().createPicture((HSSFClientAnchor)pic.getAnchor()r, pictureIndex); } } 

Note: Pic data from one sheet to another sheet will be read above the code. This.tSheet.sheet () is the source code for this.newSheet - a new sheet

+1
source

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


All Articles