Reputation: 449
I'm making an application that converts our data structure to an excel file. We're currently using POI to do the conversion.
The problem is that the Cell only supports Strings, Numbers and formulas, and our data structure supports images. Is there a way to add images to cells?
Upvotes: 0
Views: 1399
Reputation: 1
This is how I add an image to Excel workbook using Apache POI
try {
InputStream imageStream = new FileInputStream(image.getFile());
byte[] imageBytes = IOUtils.toByteArray(imageStream);
String extension = DocumentUtil.getFileExtension(image.getFile().getAbsolutePath());
int pictureTypeId = getWorkbookPictureTypeId(extension);
int pictureId = workbook.addPicture(imageBytes, pictureTypeId);
XSSFDrawing drawing = (XSSFDrawing) sheet.createDrawingPatriarch();
XSSFClientAnchor imageAnchor = new XSSFClientAnchor();
imageAnchor.setCol1(cell.getColumnIndex());
imageAnchor.setCol2(cell.getColumnIndex()+1);
imageAnchor.setRow1(cell.getRowIndex());
imageAnchor.setRow2(cell.getRowIndex()+1);
XSSFPicture excelImage = drawing.createPicture(imageAnchor, pictureId);
//get image width and height to adjust the cell row.
Double height = excelImage.getImageDimension().getHeight();
short heightInShort = height.shortValue();
short EmuUnitInShort = image.getHeight().shortValue();
cell.getRow().setHeight((short)(heightInShort*EmuUnitInShort));
} catch(Exception e) {
log.error(e.getMessage(), e);
}
Upvotes: 0
Reputation: 792
There is no images cells in Excel,you need to add the images to the worksheet.
HSSFWorkbook workbook;
workbook.addPicture(pngData, HSSFWorkbook.PICTURE_TYPE_PNG);
Upvotes: 1