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

java生成word

两种方案

一、poi-tl生成word

<dependency><groupId>com.deepoove</groupId><artifactId>poi-tl</artifactId><version>1.12.1</version>
</dependency>
public static void main(String[] args) throws Exception {String path = "C:/siefile/bak11/v9/";String templateUrl = path + "poitl-template.docx";String outUrl = path + "poi-tl测试.docx";Map<String, Object> data = new HashMap<>();data.put("detail", "用这些语言编写的程序和上面我们讲的命令行客户端");data.put("remark", "对mysql-connector-java源码的分析");RowRenderData row0 = Rows.of("姓名", "学历").textColor("FFFFFF").bgColor("4472C4").center().create();RowRenderData row1 = Rows.create("李四", "博士");data.put("testTable", Tables.create(row0, row1));XWPFTemplate render = XWPFTemplate.compile(templateUrl).render(data);render.writeToFile(outUrl);System.out.println("========done========");
}

word模板:注意模板里面的中文最好全部设置成微软雅黑或者宋体,不然会乱码

二、docx4j生成word

<properties><docx4j.version>8.3.10</docx4j.version>
</properties>
<dependency><groupId>org.docx4j</groupId><artifactId>docx4j-JAXB-Internal</artifactId><version>${docx4j.version}</version>
</dependency>
<dependency><groupId>org.docx4j</groupId><artifactId>docx4j-JAXB-ReferenceImpl</artifactId><version>${docx4j.version}</version>
</dependency>
<dependency><groupId>org.docx4j</groupId><artifactId>docx4j-export-fo</artifactId><version>${docx4j.version}</version>
</dependency>
public static void main(String[] args) throws Exception {String path = "C:/siefile/bak11/v9/";String templateFilePath = path + "docx4j-template.docx";String outFilePath = path + "docx4j测试.docx";// 加载word模板WordprocessingMLPackage wordPackage = WordprocessingMLPackage.load(new File(templateFilePath));MainDocumentPart documentPart = wordPackage.getMainDocumentPart();// 找到表格第二行,然后进行变量替换ClassFinder find = new ClassFinder(Tbl.class);new TraversalUtil(documentPart.getContent(), find);Tbl table = (Tbl) find.results.get(0);List<Object> tableContent = table.getContent();Tr templateTr = (Tr) tableContent.get(1);String templateTrXml = XmlUtils.marshaltoString(templateTr);List<Map> list = getTableData();for (Map map : list) {Tr newTr = (Tr) XmlUtils.unmarshallFromTemplate(templateTrXml, map);tableContent.add(newTr);}tableContent.remove(1);// 普通的变量替换Map<String, String> data = new HashMap<>();data.put("detail", "用这些语言编写的程序和上面我们讲的命令行客户端");data.put("remark", "对mysql-connector-java源码的分析");VariablePrepare.prepare(wordPackage);documentPart.variableReplace(data);Docx4J.save(wordPackage, new FileOutputStream(outFilePath));System.out.println("=========done========");
}private static List<Map> getTableData() {List list = new ArrayList();for (int index = 0; index < 5; index++) {HashMap map = new HashMap();map.put("name", "name_" + index);map.put("age", "age_" + index);list.add(map);}return list;
}

word模板:注意模板里面的中文最好全部设置成微软雅黑或者宋体,不然会乱码

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

相关文章:

  • C语言中的结构体:揭秘数据的魔法盒
  • Listener
  • 单细胞RNA测序(scRNA-seq)SRA数据下载及fastq-dumq数据拆分
  • 金蝶Apusic应用服务器 未授权目录遍历漏洞复现
  • 成都百洲文化传媒有限公司电商服务的新领军者
  • 从无到有开始创建动态顺序表——C语言实现
  • Unix 网络编程, Socket 以及bind(), listen(), accept(), connect(), read()write()五大函数简介
  • 【附下载】2024全行业数字化转型企业建设解决方案PPT合集
  • 【QT+QGIS跨平台编译】056:【pdal_lepcc+Qt跨平台编译】(一套代码、一套框架,跨平台编译)
  • 蓝桥集训之斐波那契数列
  • 程序员的工资是多少,和曹操有莫大的关系
  • 使用Element Plus
  • 单例(Singleton)设计模式总结
  • LeetCode每日一题之专题一:双指针 ——快乐数
  • Docker Desktop 不支持 host 网络模式
  • Linux网络编程二(TCP图解三次握手及四次挥手、TCP滑动窗口、MSS、TCP状态转换、多进程/多线程服务器实现)
  • 【云原生篇】K8S之Job 和 CronJob
  • PHP8.3-ZTS版本安装流程以及添加扩展
  • RabbitMQ系统监控、问题排查和性能优化实践
  • 【华为OD机试】根据IP查找城市(贪心算法—JavaPythonC++JS实现)
  • css:阴影效果box-shadow
  • Scala第十九章节(Actor的相关概述、Actor发送和接收消息以及WordCount案例)
  • 蓝桥杯杯赛之深度优先搜索优化《1.分成互质组》 《 2.小猫爬山》【dfs】【深度搜索剪枝优化】【搜索顺序】
  • 软件设计原则:依赖倒置
  • 03-自媒体文章发布
  • Oracle中实现一次插入多条数据
  • 【C++入门】关键字、命名空间以及输入输出
  • 初识MySQL(中篇)
  • 前端订阅后端推送WebSocket定时任务
  • 提高机器人系统稳定性:引入阻尼作为共振后的相位超前