Apache POI 插入图片至 Excel 的两种方法

方法 1 插入图片至坐标

 

    ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
    BufferedImage bufferImg = ImageIO.read(new File(strAppRootPath + "images/" + "bxlogo.png"));
    ImageIO.write(bufferImg, "png", byteArrayOut);

    HSSFPatriarch patriarch = (HSSFPatriarch) sheet.createDrawingPatriarch();
    HSSFClientAnchor anchor = new HSSFClientAnchor(0, 150, 1000, 210, (short) 0, 0, (short) 1, 1);
    patriarch.createPicture(anchor, workbook.addPicture(byteArrayOut.toByteArray(), HSSFWorkbook.PICTURE_TYPE_PNG));


 

方法 2 插入图片至单元格,以及另一种图片载入方法,其实本质也一样

 

    // 插入 PNG 图片至 Excel
    String fileName = strAppRootPath + "images/" + "bxlogo.png";

    InputStream is = new FileInputStream(fileName);
    byte[] bytes = IOUtils.toByteArray(is);

    int pictureIdx = workbook.addPicture(bytes, Workbook.PICTURE_TYPE_PNG);

    CreationHelper helper = workbook.getCreationHelper();
    Drawing drawing = sheet.createDrawingPatriarch();
    ClientAnchor anchor = helper.createClientAnchor();

    // 图片插入坐标
    anchor.setCol1(0);
    anchor.setRow1(1);
    // 插入图片
    Picture pict = drawing.createPicture(anchor, pictureIdx);
    pict.resize();


 blog.csdn.net/joyous/article/details/9664739

Q群讨论:236201801

你可能感兴趣的:(Apache POI 插入图片至 Excel 的两种方法)