user3230860
user3230860

Reputation: 11

How do I write an image in xls with a custom size using Java and Apache POI

How do I write an image with a custom size in xls using Java and Apache POI? This is my code. It shows the given image with the default size but I need to customize the size.

Workbook wb = new XSSFWorkbook();
Sheet sheet = wb.createSheet("My Sample Excel");
InputStream inputStream = new FileInputStream("images/myLogo.png");
byte[] bytes = IOUtils.toByteArray(inputStream);
int pictureIdx = wb.addPicture(bytes, Workbook.PICTURE_TYPE_PNG);
inputStream.close();
CreationHelper helper = wb.getCreationHelper();
Drawing drawing = sheet.createDrawingPatriarch();
ClientAnchor anchor = helper.createClientAnchor();
anchor.setCol1(1);
anchor.setRow1(2);
Picture pict = drawing.createPicture(anchor, pictureIdx);
pict.resize();
FileOutputStream fileOut = null;
fileOut = new FileOutputStream("data/myFile.xlsx");
wb.write(fileOut);
fileOut.close();

Thanks.

Upvotes: 1

Views: 842

Answers (1)

Barett
Barett

Reputation: 5948

Try running these before calling drawing.createPicture:

anchor.setAnchorType(ClientAnchor.MOVE_AND_RESIZE);
anchor.setCol2(X);
anchor.setRow2(Y);

Upvotes: 1

Related Questions