I try to loop images in xls document. I write next code:
HSSFPatriarch patriarch = sheet.getDrawingPatriarch();
if (patriarch != null) {
// Loop through the objects
for (HSSFShape shape : patriarch.getChildren()) {
if (shape instanceof HSSFPicture) {
HSSFPicture picture = (HSSFPicture) shape;
if (picture.getShapeType() == HSSFSimpleShape.OBJECT_TYPE_PICTURE){
if (picture.getImageDimension() != null) {
// how to get cell, which contains this picture
}
}
}
}
}
How to find a cell for each picture in a sheet?
Update, right now I write next working code:
HSSFPatriarch patriarch = sheet.getDrawingPatriarch();
if (patriarch != null) {
// Loop through the objects
for (HSSFShape shape : patriarch.getChildren()) {
if (shape instanceof HSSFPicture) {
HSSFPicture picture = (HSSFPicture) shape;
if (picture.getShapeType() == HSSFSimpleShape.OBJECT_TYPE_PICTURE){
if (picture.getImageDimension() != null) {
// how to get cell, which contains this picture
if (picture.getImageDimension() != null) {
Row row = sheet.getRow(picture.getPreferredSize().getRow1());
if (row != null) {
Cell cell = row.getCell(picture.getPreferredSize().getCol1());
HSSFPictureData pictureData = picture.getPictureData();
byte[] data = pictureData.getData();
File file = new File(PATH + picture.getFileName() + "." + pictureData.suggestFileExtension());
try (FileOutputStream fop = new FileOutputStream(file)) {
if (!file.exists()) {
file.createNewFile();
}
fop.write(data);
fop.flush();
fop.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
}
}
}
If you need, like me, to assotiate a picture with a cell in future, try to use HashMap().
Can some-one write any example for this task? I want to find cells, contains a picture in excel's sheet. The trick in that, what picture is not cell's value.