随笔- 47  文章- 8  评论- 0  阅读- 34987 

通过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   AI.℡  阅读(261)  评论(0编辑  收藏  举报
编辑推荐:
· Java 中堆内存和栈内存上的数据分布和特点
· 开发中对象命名的一点思考
· .NET Core内存结构体系(Windows环境)底层原理浅谈
· C# 深度学习:对抗生成网络(GAN)训练头像生成模型
· .NET 适配 HarmonyOS 进展
阅读排行:
· 用 DeepSeek 给对象做个网站,她一定感动坏了
· DeepSeek+PageAssist实现本地大模型联网
· 手把手教你更优雅的享受 DeepSeek
· Java轻量级代码工程
· 从 14 秒到 1 秒:MySQL DDL 性能优化实战
点击右上角即可分享
微信分享提示