在Java中使用ApachePOI格式化带有填充和线条的图片

在Java中使用ApachePOI格式化带有填充和线条的图片,java,excel,apache-poi,Java,Excel,Apache Poi,我正在使用apache poi 3.15将图像插入excel的工作文件中,但我想在图像中添加边框线,这可以通过在MS Office中右键单击图像-->格式图片-->填充和线条-->线条-->实线来完成。遵循我的代码 private void drawImageOnExcelSheetForGLOS(XSSFSheet sitePhotosSheet, int row1, int row2, int col1, int col2, String fileName) { tr

我正在使用apache poi 3.15将图像插入excel的工作文件中,但我想在图像中添加边框线,这可以通过在MS Office中右键单击图像-->格式图片-->填充和线条-->线条-->实线来完成。遵循我的代码

private void drawImageOnExcelSheetForGLOS(XSSFSheet sitePhotosSheet,
        int row1, int row2, int col1, int col2, String fileName) {
    try {

        InputStream is = new FileInputStream(fileName);
        byte[] bytes = IOUtils.toByteArray(is);
        int pictureIdx = sitePhotosSheet.getWorkbook().addPicture(bytes,Workbook.PICTURE_TYPE_JPEG);
        is.close();
        CreationHelper helper = sitePhotosSheet.getWorkbook().getCreationHelper();

        // Create the drawing patriarch. This is the top level container for
        // all shapes.
        Drawing drawing = sitePhotosSheet.createDrawingPatriarch();

        // add a picture shape
        ClientAnchor anchor = helper.createClientAnchor();
        anchor.setAnchorType( ClientAnchor.MOVE_AND_RESIZE );


        // set top-left corner of the picture,
        // subsequent call of Picture#resize() will operate relative to it
        anchor.setCol1(col1);
        anchor.setCol2(col2);
        anchor.setRow1(row1);
        anchor.setRow2(row2);

        drawing.createPicture(anchor, pictureIdx);

    } catch(Exception e) {
    }
}
我可以使用XSSFSimpleShape在图像周围画线,但这并不是我想要的,我也尝试过使用setBorderXXX(),但这些边框或线不会像使用MS Office设置时那样随图像移动。 第一张图片显示我得到了什么,第二张图片显示了我想要什么

这可以通过使用XSSFPicture的setLineXXX()方法实现,如下所示:

private void drawImageOnExcelSheetForGLOS(XSSFSheet sitePhotosSheet,
        int row1, int row2, int col1, int col2, String fileName) {
    try {

        InputStream is = new FileInputStream(fileName);
        byte[] bytes = IOUtils.toByteArray(is);
        int pictureIdx = sitePhotosSheet.getWorkbook().addPicture(bytes,Workbook.PICTURE_TYPE_JPEG);
        is.close();
        CreationHelper helper = sitePhotosSheet.getWorkbook().getCreationHelper();

        XSSFDrawing drawing = sitePhotosSheet.createDrawingPatriarch();

        ClientAnchor anchor = helper.createClientAnchor();
        anchor.setAnchorType( ClientAnchor.MOVE_AND_RESIZE );

        anchor.setCol1(col1);
        anchor.setCol2(col2);
        anchor.setRow1(row1);
        anchor.setRow2(row2);

        // setLineXXX() methods can be used to set line border to image
        XSSFPicture pic = drawing.createPicture(anchor, pictureIdx);
        // 0 indicates solid line
        pic.setLineStyle(0);
        // rgb color code for black line
        pic.setLineStyleColor(0, 0, 0);
        // double number for line width
        pic.setLineWidth(1.5);
    } catch(Exception e) {
        e.printStackTrace();
    }
}

这可以通过使用XSSFPicture的setLineXXX()方法实现,如下所示:

private void drawImageOnExcelSheetForGLOS(XSSFSheet sitePhotosSheet,
        int row1, int row2, int col1, int col2, String fileName) {
    try {

        InputStream is = new FileInputStream(fileName);
        byte[] bytes = IOUtils.toByteArray(is);
        int pictureIdx = sitePhotosSheet.getWorkbook().addPicture(bytes,Workbook.PICTURE_TYPE_JPEG);
        is.close();
        CreationHelper helper = sitePhotosSheet.getWorkbook().getCreationHelper();

        XSSFDrawing drawing = sitePhotosSheet.createDrawingPatriarch();

        ClientAnchor anchor = helper.createClientAnchor();
        anchor.setAnchorType( ClientAnchor.MOVE_AND_RESIZE );

        anchor.setCol1(col1);
        anchor.setCol2(col2);
        anchor.setRow1(row1);
        anchor.setRow2(row2);

        // setLineXXX() methods can be used to set line border to image
        XSSFPicture pic = drawing.createPicture(anchor, pictureIdx);
        // 0 indicates solid line
        pic.setLineStyle(0);
        // rgb color code for black line
        pic.setLineStyleColor(0, 0, 0);
        // double number for line width
        pic.setLineWidth(1.5);
    } catch(Exception e) {
        e.printStackTrace();
    }
}

drawing.createPicture(锚定,pictureIdx)不是无效方法。它返回一个。这扩展并继承了一些
setLine…
方法。你试过使用这些方法吗?
drawing.createPicture(anchor,pictureIdx)不是无效方法。它返回一个。这扩展并继承了一些
setLine…
方法。你试过使用这些方法吗?