I'm trying to insert an image into a cell in excel. I've added pictures fine, but I still runs anywhere. I want to say that I want this column.
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
You can set the row and column first then set the image.
try {
Workbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet("MYSheet");
InputStream inputStream = new FileInputStream("path_to_image.jpg");
byte[] imageBytes = IOUtils.toByteArray(inputStream);
int pictureureIdx = workbook.addPicture(imageBytes, Workbook.PICTURE_TYPE_PNG);
inputStream.close();
CreationHelper helper = workbook.getCreationHelper();
Drawing drawing = sheet.createDrawingPatriarch();
ClientAnchor anchor = helper.createClientAnchor();
anchor.setCol1(1);
anchor.setRow1(2);
drawing.createPicture(anchor, pictureureIdx);
FileOutputStream fileOut = null;
fileOut = new FileOutputStream("output.xlsx");
workbook.write(fileOut);
fileOut.close();
}catch (Exception e) {
System.out.println(e);
}