I’ve recently had to change from HSSF on a project to XSSF (for
password support), and have noticed a couple of inconsistencies (maybe
bugs) in the XSSF implementation for handling addPicture() and
autoSizeColumn():
addPicture
HSSF is 1-based (fist image number is 1), while XSSF is 0-based (even
though the documentation states otherwise).
private static int addLogoImageToWorkbook(Workbook wb)
throws AppException {
int pictureIdx = -1;
try(InputStream is =
Thread.currentThread().getContextClassLoader().getResourceAsStream(
File.separator + SystemConstants.BUSINESS_LOGO_IMAGE_FILENAME)) {
byte[] bytes = IOUtils.toByteArray(is);
pictureIdx = wb.addPicture(bytes, Workbook.PICTURE_TYPE_PNG);
} catch (IOException e) {
throw new AppException("Failed to add picture", e);
}
return pictureIdx;
}
autoSizeColumn
Simply doesn’t work in XSSF, while sheet.autoSizeColumn(columnNumber)
works fine in HSSF.
// Adjust columns widths to fit using ClientColumn enum
for (ClientColumn column : ClientColumn.values()) {
sheet.autoSizeColumn(column.columnNumber);
}
Cheers!
Larry