通过poi下载图片到word

通过poi下载图片到word

一、添加相关依赖包

修改pom.xml文件,添加如下依赖(其他方式亦可)

<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>3.16</version>
    <exclusions>
        <exclusion>
            <artifactId>commons-codec</artifactId>
            <groupId>commons-codec</groupId>
        </exclusion>
    </exclusions>
</dependency>

<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-scratchpad</artifactId>
    <version>3.16</version>
</dependency>

二、实现方法

实现demo

XWPFDocument document = new XWPFDocument();

//获取本地图片 C:\Users\admin\Desktop
File file=new File("C:/Users/admin/Desktop/aa.jpg");
InputStream  in = new FileInputStream(file);
BufferedImage image = ImageIO.read(file);

XWPFParagraph wparagraph = document.createParagraph();
XWPFRun wrun = wparagraph.createRun();
wrun.addPicture(in,
    org.apache.poi.xwpf.usermodel.Document.PICTURE_TYPE_PNG, "",
    Units.pixelToEMU(image.getWidth()),
    Units.pixelToEMU(image.getHeight()));

猜你喜欢

转载自www.cnblogs.com/wscy/p/9223508.html