We have a requirement wherein we add images to worksheet. For every image we add, we attempt to resize the underlying cell. Images can have borders as well. Unfortunately, manually setting the size of the cell based on the image dimensions and its border width doesn’t work. The column width is slightly more than expected in the excel output. Here’s the code snippet that we use:
public static void main(String args) throws Exception
Workbook workbook = new Workbook();
Worksheet worksheet = workbook.getWorksheets().get(0); double borderWidth = 5; // input is always expected in pixels. int index = worksheet.getPictures().add(1, 1, "D:\\issues\\imageresize\\school.jpg"); Picture picture = worksheet.getPictures().get(index); LineFormat lf = picture.getLine(); lf.setCapType(LineCapType.FLAT); lf.setJoinType(LineJoinType.MITER); lf.setCompoundType(MsoLineStyle.SINGLE); lf.setDashStyle(MsoLineDashStyle.SOLID); borderWidth = borderWidth / CellsHelper.getDPI(); // convert it to inches picture.getLine().setWeight(borderWidth * 72); // converting to points by multiplying with 72 picture.setBorderWeight(borderWidth * 72); Color color = Color.fromArgb(Integer.parseInt("FF8040", 16)); picture.setBorderLineColor(color); double imageHeight = picture.getHeightInch() + (2 * borderWidth); double imageWidth = picture.getWidthInch() + (2 * borderWidth); picture.setTopInch(borderWidth); picture.setLeftInch(borderWidth); worksheet.getCells().setColumnWidthInch(1, imageWidth); worksheet.getCells().setRowHeightInch(1, imageHeight); workbook.save("D:\\issues\\imageresize\\work.xlsx"); }
-What could be wrong with the approach?
-Attaching snapshot of excel output
work.zip (14.1 KB)