在Java中使用POI将图像包含在Excel标题右侧。

huangapple go评论142阅读模式
英文:

Include an image to the right of a Header in Excel in Java using POI

问题

下面是您的翻译内容:

private static void drawImageOnExcelSheet(XSSFSheet sheet, int row, int col, int height, int width, int pictureIdx)
        throws Exception {

    CreationHelper helper = sheet.getWorkbook().getCreationHelper();

    Drawing drawing = sheet.createDrawingPatriarch();

    ClientAnchor anchor1 = helper.createClientAnchor();
    anchor1.setAnchorType(AnchorType.DONT_MOVE_AND_RESIZE);

    anchor1.setRow2(row); // 第二个锚点确定右下位置
    anchor1.setCol2(col);
    anchor1.setDx2(Units.toEMU(width)); // dx = 左边界 + 所需宽度
    anchor1.setDy2(Units.toEMU(height)); // dy = 顶边界 + 所需高度

    Picture pic = drawing.createPicture(anchor1, pictureIdx);
    pic.resize();

}
英文:

I want to add image to the right side of Header in Excel in java using POI. Below is my sample code

private static void drawImageOnExcelSheet(XSSFSheet sheet, int row, int col, int height, int width, int pictureIdx)
        throws Exception {

    CreationHelper helper = sheet.getWorkbook().getCreationHelper();

    Drawing drawing = sheet.createDrawingPatriarch();

    ClientAnchor anchor1 = helper.createClientAnchor();
    anchor1.setAnchorType(AnchorType.DONT_MOVE_AND_RESIZE);

    anchor1.setRow2(row); // second anchor determines bottom right position
    anchor1.setCol2(col);
    anchor1.setDx2(Units.toEMU(width)); // dx = left + wanted width
    anchor1.setDy2(Units.toEMU(height)); // dy= top + wanted height

    Picture pic = drawing.createPicture(anchor1, pictureIdx);
    pic.resize();

}

答案1

得分: 3

我在答案中贴了一个stackoverflow的参考链接,因为我没有评论的权限。

请参阅https://stackoverflow.com/questions/51077404/apache-poi-adding-watermark-in-excel-workbook/51103756#51103756,了解如何在Excel中设置图像作为页眉内容。

英文:

I'm posting a stackoverflow reference in answer as i don't have privilege to comment.

See https://stackoverflow.com/questions/51077404/apache-poi-adding-watermark-in-excel-workbook/51103756#51103756 for how to set an image as header content in Excel

huangapple
  • 本文由 发表于 2020年3月15日 10:39:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/60689244.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定