[英]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? 如何使用Java和Apache POI在xls中写入具有自定义大小的图像? 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. 谢谢。
Try running these before calling drawing.createPicture
: 尝试在调用drawing.createPicture
之前运行它们:
anchor.setAnchorType(ClientAnchor.MOVE_AND_RESIZE);
anchor.setCol2(X);
anchor.setRow2(Y);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.