当前位置: 首页 > news >正文

poi word写入图片

直接使用的百度结果,经过测试可行

1.pom增加jar

        <dependency><groupId>org.apache.poi</groupId><artifactId>poi</artifactId><version>5.2.3</version></dependency><dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml</artifactId><version>5.2.3</version></dependency>

2.代码

package pers.wwz.study.poiwordimage;import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.util.Units;
import org.apache.poi.xwpf.usermodel.*;import java.io.*;public class WordExportImage {public static void main(String[] args) throws Exception {// 模板文件路径String templatePath = "template.docx";// 输出文件路径String outputPath = "output.docx";// 图片文件路径String imagePath = "image.png";// 读取模板InputStream is = new FileInputStream(templatePath);XWPFDocument doc = new XWPFDocument(is);// 替换文档中的图片占位符for (XWPFParagraph para : doc.getParagraphs()) {for (XWPFRun run : para.getRuns()) {String text = run.getText(0);if (text != null && text.contains("IMAGE_PLACEHOLDER")) {run.setText("", 0); // 清除占位符文本run.addPicture(new FileInputStream(imagePath),XWPFDocument.PICTURE_TYPE_PNG, imagePath,Units.toEMU(200), Units.toEMU(200)); // 插入图片}}}// 输出文件FileOutputStream out = new FileOutputStream(outputPath);doc.write(out);out.close();doc.close();is.close();}
}

http://www.lryc.cn/news/385283.html

相关文章:

  • 【监控】2.Grafana的安装
  • Java入门教程(上)
  • 【Linux】Linux下使用套接字进行网络编程
  • 强化学习-Q-learning、SARSA和PPO等算法
  • HarmonyOS SDK助力鸿蒙原生应用“易感知、易理解、易操作”
  • Java基础入门day72
  • 文本编辑命令和正则表达式
  • 云手机群控功能讲解
  • gdb用法
  • 聊一聊UDF/UDTF/UDAF是什么,开发要点及如何使用?
  • 配置Nginx二级域名
  • LeetCode——判断回文数
  • shell:使用结构化语句(for、while循环)
  • 数据结构_绪论
  • AI自动生成角色和情节连续的漫画,中山大学联想提出AutoStudio,可以多轮交互式连续生成并保持主题一致性。
  • 【经典面试题】RabbitMQ如何防止重复消费?
  • 如何自己录制教学视频?零基础也能上手
  • 【android】用 ExpandableListView 来实现 TreeView树形菜单视图
  • 策略模式与函数式编程应用
  • docker原理记录C-N-A
  • 【LeetCode】每日一题:二叉树的层次遍历
  • 单体架构改造为微服务架构之痛点解析
  • 马面裙的故事:汉服如何通过直播电商实现产业跃迁
  • SaaS产品运营:维护四个不同类型的合作伙伴的实战指南
  • 【监控】3.配置 Grafana 以使用 Prometheus 数据源
  • 【LinuxC语言】网络编程中粘包问题
  • Docker之jekins的安装
  • # bash: chkconfig: command not found 解决方法
  • Linux线程互斥锁
  • 展开说说:Android列表之RecyclerView