使用 POI 將影像插入 Excel

如何使用Java中的POI套件在輸出Excel時插入圖片。

try {
    FileInputStream images = new FileInputStream("/path/to/file.png");
    
    final Drawing drawing = sheet.createDrawingPatriarch();
    final ClientAnchor anchor = helper.createClientAnchor();
    anchor.setAnchorType(ClientAnchor.MOVE_AND_RESIZE );
    final int pictureIndex = workbook.addPicture(
        IOUtils.toByteArray(images),
        Workbook.PICTURE_TYPE_PNG
    );
    anchor.setCol1(1);
    anchor.setRow1(1);
    final Picture picture = drawing.createPicture(anchor, pictureIndex);
    picture.resize(1.0, 1.0);
} catch (Exception ex) {
    ex.printStackTrace();
}

參考文獻

poi java