如何使用Java和Apache POI以自定义大小在xls中编写图像

时间:2014-04-25 12:32:44

标签: java apache-poi

如何使用Java和Apache POI在xls中编写具有自定义大小的图像? 这是我的代码。它以默认大小显示给定图像,但我需要自定义大小。

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();

感谢。

1 个答案:

答案 0 :(得分:1)

在调用drawing.createPicture之前尝试运行这些:

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