使用Apache POI在专栏中插入图片以实现excel [英] Insert image in column to excel using Apache POI

查看:253
本文介绍了使用Apache POI在专栏中插入图片以实现excel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将图像插入Excel中的单元格.我添加的图片很好,但是我仍然可以在任何地方运行.我想说我想要此专栏.

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.

推荐答案

您可以先设置行和列,然后再设置图像.

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

这篇关于使用Apache POI在专栏中插入图片以实现excel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆