Apache POI XWPF 爬坑指南之三表格中插入图片

接上话

表格中插入图片


思路:新建表格中每个单元格会有一个默认的空的段落,获取这个段落,然后获取这个段落的XWPFRun,使用XWPFRun插入图片。

    /**
     * 往表格中的一个单元格中插入图片
     *
     * @param xwpfTableCell
     * @param imagePojo
     * @throws IOException
     * @throws InvalidFormatException
     */
    public static void insertPictureIntoTableCell(XWPFTableCell xwpfTableCell, ImagePojo imagePojo,int width,int height) throws IOException, InvalidFormatException {
        XWPFParagraph newPara = xwpfTableCell.getParagraphArray(0);
        XWPFRun run = newPara.createRun();
        run.addPicture(new FileInputStream(imagePojo.getImageUrl()), imagePojo.getFormat(), imagePojo.getImageName(), Units.toEMU(width), Units.toEMU(height));
    }

public class ImagePojo {
    private String imageName;
    private String imageUrl;
    private int format;//图片格式

    public int getFormat() {
        return format;
    }

    public void setFormat(int format) {
        this.format = format;
    }

    public String getImageName() {
        return imageName;
    }

    public void setImageName(String imageName) {
        this.imageName = imageName;
    }

    public String getImageUrl() {
        return imageUrl;
    }

    public void setImageUrl(String imageUrl) {
        this.imageUrl = imageUrl;
    }
}

插入图片时需要设置图片格式,获取图片格式的方法为

     /**
     * 获取图片格式
     *
     * @param imgFile
     * @return
     */
    public static int getPictureFormat(String imgFile) {
        int format;
        if (imgFile.endsWith(".emf")) format = XWPFDocument.PICTURE_TYPE_EMF;
        else if (imgFile.endsWith(".wmf")) format = XWPFDocument.PICTURE_TYPE_WMF;
        else if (imgFile.endsWith(".pict")) format = XWPFDocument.PICTURE_TYPE_PICT;
        else if (imgFile.endsWith(".jpeg") || imgFile.endsWith(".jpg")) format = XWPFDocument.PICTURE_TYPE_JPEG;
        else if (imgFile.endsWith(".png")) format = XWPFDocument.PICTURE_TYPE_PNG;
        else if (imgFile.endsWith(".dib")) format = XWPFDocument.PICTURE_TYPE_DIB;
        else if (imgFile.endsWith(".gif")) format = XWPFDocument.PICTURE_TYPE_GIF;
        else if (imgFile.endsWith(".tiff")) format = XWPFDocument.PICTURE_TYPE_TIFF;
        else if (imgFile.endsWith(".eps")) format = XWPFDocument.PICTURE_TYPE_EPS;
        else if (imgFile.endsWith(".bmp")) format = XWPFDocument.PICTURE_TYPE_BMP;
        else if (imgFile.endsWith(".wpg")) format = XWPFDocument.PICTURE_TYPE_WPG;
        else {
            System.err.println("Unsupported picture: " + imgFile +
                    ". Expected emf|wmf|pict|jpeg|png|dib|gif|tiff|eps|bmp|wpg");
            System.err.println("不支持的图片格式: " + imgFile +
                    ". 仅支持 emf|wmf|pict|jpeg|png|dib|gif|tiff|eps|bmp|wpg 格式的图片");
            format = -1;
        }
        return format;
    }
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,372评论 25 708
  • 接上话 特定位置插入表格、段落、图片 思路在word中做个标记,通常这个标记独自占据一个段落,例如标记示例我们想要...
    Pantheon阅读 16,981评论 3 5
  • 虽说Excel制表功能是超级强大,但是当我们在使用word制作文档时难免会用一些表格功能。其实一些不复杂的表格操作...
    Jeck_merlin阅读 3,543评论 1 28
  • 我要带你去旅行 去美丽的宝岛台湾 不是因为厌倦了家乡 而是为了诗意和远方 我要带你去旅行 飞跃台湾海峡 满满的温暖...
    凯歌儿阅读 316评论 3 5
  • 醒来,在夜的转角 对于哪件事哪些人把我从麻醉里踢开 我奇怪,能记得很清楚 并乐此不疲地将它们再过一遍电影 找出其中...
    改变自己369阅读 258评论 1 3