'how to replace an image with another image in excel using POI
I need help replacing an image with another image in an excel sheet using Apache POI.
int myPictureId=0;
List<XSSFShape> pictures = drawing.getShapes();
for(XSSFShape shape : pictures) {
XSSFPicture picture = (XSSFPicture)shape; //Get picture
byte[] data = picture.getPictureData().getData(); //Get picture data
XSSFClientAnchor anchor = picture.getClientAnchor(); //Get anchor
ByteArrayInputStream byteInput = new ByteArrayInputStream(data);
BufferedImage image = ImageIO.read(byteInput);
data = getByteArrayFromImage(image, 400, data, picture.getPictureData().suggestFileExtension());
myPictureId = workbook.addPicture(new ByteArrayInputStream(data), Workbook.PICTURE_TYPE_PNG);
picture = drawing.createPicture(anchor, myPictureId);
}
I am using the getByteArrayFromImage method to compress my image and replace the original image with the compressed one. I also tried using XSSFClientAnchor to attach the compressed image to the original image but excel is overlapping the original image with the compressed image.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
