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

java生成word-xdocreport

pom  maven 引入

 

<!--  doc start -->
<dependency>
<groupId>fr.opensagres.xdocreport</groupId>
<artifactId>fr.opensagres.xdocreport.core</artifactId>
<version>2.0.6</version>
</dependency>
<dependency>
<groupId>fr.opensagres.xdocreport</groupId>
<artifactId>fr.opensagres.xdocreport.document</artifactId>
<version>2.0.6</version>
</dependency>
<dependency>
<groupId>fr.opensagres.xdocreport</groupId>
<artifactId>fr.opensagres.xdocreport.template</artifactId>
<version>2.0.6</version>
</dependency>
<dependency>
<groupId>fr.opensagres.xdocreport</groupId>
<artifactId>fr.opensagres.xdocreport.document.docx</artifactId>
<version>2.0.6</version>
</dependency>
<dependency>
<groupId>fr.opensagres.xdocreport</groupId>
<artifactId>fr.opensagres.xdocreport.template.freemarker</artifactId>
<version>2.0.6</version>
</dependency>

<!-- doc end -->


java 代码
--------------
 //实现类
public static void createXdocreportRe(HttpServletResponse response) throws IOException {
InputStream inputStream = null;
FileOutputStream outputStream = new FileOutputStream("warning_task.docx");
try {
//读取取resource目录下的模板 
inputStream = WordUtil.class.getClassLoader().getResourceAsStream("summary.docx");
IXDocReport report = XDocReportRegistry.getRegistry().loadReport(inputStream, TemplateEngineKind.Freemarker);
//2.设置填充字段、填充类以及是否为list。
FieldsMetadata fieldsMetadata = report.createFieldsMetadata();
IContext context = report.createContext();

// 创建内容-text为模版中对应都变量名称
String content = "&#x3c;p&#x3e;我在这里放了一段富文本&#x3c;/p&#x3e;" +
"&#x3c;p&#x3e;我准备测试富文本的处理&#x3c;/p&#x3e;";
content = HtmlUtils.htmlUnescape(content);
context.put("name", "年终总结大会");
context.put("time", "2021年3月26日");
context.put("place", "线上");
context.put("sponsor", "张三");
context.put("content", content);
// InputStream p1 = new FileInputStream(new File("C:\\Users\\DELL\\img\\1.jpg"));
// //context.put("img", p1);
// InputStream p2 = new FileInputStream(new File("C:\\Users\\DELL\\img\\2.jpeg"));
// //new ByteArrayImageProvider(p1)
User u1=new User("张三", "组织部", "广州天河");
User u2=new User("李四", "宣传部", "北京海淀");
List<User> users = new ArrayList<>();
users.add(u1);
users.add(u2);
context.put("userList", users);
fieldsMetadata.load("userList",User.class,true);
// 生成文件
report.setFieldsMetadata(fieldsMetadata);
report.process(context, outputStream);

inputStream.close();
outputStream.close();
} catch (Exception e) {
log.info("生成纪要文件发生异常:<{}>", e.getMessage());
}
}


--------------
doc 模版


需要注意事项

在doc文档中,使用 Ctrl+F9 生成 域 然后 编辑 域

image

 

如果需要遍历 list

则需要 定义list域

 

image

 

别忘了 结束标签

image

 




http://www.jsqmd.com/news/49333/

相关文章:

  • SynchronizedList线程安全列表
  • CLaunch设置自动开机启动的方法
  • 2025 年 11 月灌装机厂家权威推荐榜:酱油灌装机/高精度灌装机,洗瓶机/多种异型瓶洗瓶机/酱油洗瓶机,封盖机/酱油封盖机,高效稳定与智能操控的行业优选方案
  • 电池安全与召回危机
  • 求评
  • 10道面试常考的C++面试题 - Sad
  • Oracle 11g 安装程序闪退无法运行问题解决
  • 2025年上海一对一辅导机构推荐:徐汇区、虹口区家教辅导机构深度测评榜单
  • 2025年上海一对一辅导机构推荐:杨浦区、闵行区家教辅导机构深度测评榜单
  • 深入解析:从BIOS到UEFI:电脑启动进化史
  • 深耕四川 服务全国钢栈桥厂家推荐!四川中和志城建筑工程以施工 / 临时 / 装配式等全品类钢栈桥技术,筑牢工程生命线
  • 周总结汇览
  • 质量管理系统(QMS),如何选供应商?
  • 2025 年 11 月二氧化硫检测仪厂家推荐排行榜,便携式二氧化硫检测仪,固定式二氧化硫检测仪,在线二氧化硫检测仪,高精度二氧化硫检测仪公司精选
  • 2025年整流桥GBL生产厂家权威推荐榜单:整流桥GBU/整流桥GBP/整流桥KBJ源头厂家精选
  • Java 中的结构化并发模式
  • ddddocr: 得到滑块的目标位置
  • 小满的五年心得体会(程序人生) - 教程
  • ChatGPT响应背后的47步技术之旅
  • 2025 最新山东青岛进口公司排行榜,全链路进口清关优质企业最新推荐 涵盖多品类专业服务权威榜单机械进口代理/海关进口代理/进口通关/进口报关行/代理进口公司推荐
  • 我在赛时写下了如下代码,你也来试试吧
  • cmake使用教程 - 教程
  • 2025 年 11 月实验室净化工程厂家权威推荐榜:洁净室/手术室/无尘车间建设装修全方案解析,专业技术与高效服务深度评测
  • 催化剂过滤回收哪家好?相关领域企业选择参考
  • NOIP 模拟赛 8 总结
  • 智能交通新引擎:国标GB28181算法算力平台EasyGBS打造城市交通路口智能感知中枢
  • 锁的失效和分布式锁的实现
  • 2025 年 11 月镀膜材料厂家权威推荐榜:真空镀膜材料、光学镀膜材料、纳米镀膜材料,创新工艺与高性能解决方案深度解析
  • 2025 年 11 月臭氧检测仪厂家权威推荐榜:高精度检测与稳定性能,助力环境监测与工业安全
  • 想要抚州PC耐力板加工?查行情享优惠高达30%