簡體   English   中英

如何使用Java和Apache POI在具有自定義尺寸的xls中寫入圖像

[英]How do I write an image in xls with a custom size using Java and 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();

謝謝。

嘗試在調用drawing.createPicture之前運行它們:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM