在Java中使用apache poi用Fill和Line设置图片格式 [英] Format Picture with Fill and Line using apache poi in Java

查看:386
本文介绍了在Java中使用apache poi用Fill和Line设置图片格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用apache poi 3.15的工作文件在excel中插入图像,但是我想向图像添加边框,这可以通过右键单击图像->设置图片格式->填充和放大来完成. Line-> Line-> MS Office中的Solid Line 在SO和apache文档中进行了大量搜索,但是不知道如何使用poi实现这一点.按照我的代码

I am inserting image in excel using apache poi 3.15 its working file but I want to add border line to image which can be done by right click on image --> Format Picture --> Fill & Line --> Line --> Solid Line in MS Office searched a lot on SO and apache documentation but have no clue how to achieve this using poi. Following my code

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设置时那样随图像移动. 第一张图片显示了我得到的东西,第二张显示了我想要的东西

I am able to draw line around image using XSSFSimpleShape but it is not what I wanted exactly and also tried with setBorderXXX() but these borders or lines do not move with image as when set using MS Office. First image show what I am getting and second shows what I want

推荐答案

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

This can be achieved by using XSSFPicture's setLineXXX() method as follows,

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

这篇关于在Java中使用apache poi用Fill和Line设置图片格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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