通过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()));
 posted on 2018-06-25 11:19  AI.℡  阅读(253)  评论(0编辑  收藏  举报