简体   繁体   English

如何使用Java和Apache POI在具有自定义尺寸的xls中写入图像

[英]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.

相关问题 使用Apache POI将数据写入Java中的xls,xlsx,csv文件 - Write data to xls,xlsx,csv file in java using apache POI .xls使用java和POI APACHE转换为xlsx - .xls convert to xlsx using java and POI APACHE 如何使用Apache POI检查单元格是否包含图像? - How do I check if a cell contains an image using Apache POI? 如何使用Apache POI停止Java中的迭代 - How do I stop iteration in Java using Apache POI Java apache POI-如何用poi换句话说,每个页面都有边框? - Java apache POI - How do I do, in word using poi, every page has borders? 无法使用Apache POI再次写入xls文件 - Unable to write again to xls File using Apache POI 如何在android中使用apache POI在现有的xls或xlsx文件上写入数据 - how to write data on existing xls or xlsx file using apache POI in android 在使用 Apache POI 时获取 excel 文件中的图像大小(高度和宽度)而不是原始大小(对于 xls 和 xlsx 文件) - Get the image size (height and width) in excel files instead of the original size when using Apache POI (for both xls and xlsx files) 如何使用带有java的apache poi将XWPFRun中的文本替换为给定大小的jpg图像? - How to replace text in XWPFRun with jpg image with given size using apache poi with java? 使用 java servlet 和 apache poi 库下载 xls 文件 - Downloading xls file using java servlet and apache poi library
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM