码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • java word转pdf,word模板


    maven

    1. <dependency>
    2. <groupId>org.apache.poi</groupId>
    3. <artifactId>poi-ooxml</artifactId>
    4. <version>5.2.2</version>
    5. </dependency>
    6. <!-- https://mvnrepository.com/artifact/fr.opensagres.xdocreport/fr.opensagres.xdocreport.converter.docx.xwpf -->
    7. <dependency>
    8. <groupId>fr.opensagres.xdocreport</groupId>
    9. <artifactId>fr.opensagres.xdocreport.converter.docx.xwpf</artifactId>
    10. <version>2.0.4</version>
    11. </dependency>
    12. <dependency>
    13. <groupId>com.deepoove</groupId>
    14. <artifactId>poi-tl</artifactId>
    15. <version>1.12.1</version>
    16. </dependency>

    poi-tl语法参考文档

    http://deepoove.com/poi-tl/

    测试poi-tl

    1. import com.deepoove.poi.XWPFTemplate;
    2. import java.io.FileOutputStream;
    3. import java.util.ArrayList;
    4. import java.util.HashMap;
    5. import java.util.List;
    6. import java.util.Map;
    7. public class TestPoiTl {
    8. public static void main(String[] args) throws Exception {
    9. List<Map<String,Object>> users = new ArrayList<>();
    10. users.add(user("张三",10,"zs"));
    11. users.add(user("李四",20,"ls"));
    12. XWPFTemplate template = XWPFTemplate.compile("D:\\wordpdf\\template.docx").render(
    13. new HashMap<String, Object>(){{
    14. put("users", users);
    15. }});
    16. template.writeAndClose(new FileOutputStream("D:\\wordpdf\\output.docx"));
    17. }
    18. static Map<String,Object> user(String name,int age,String account){
    19. Map<String,Object> user = new HashMap<>();
    20. user.put("name",name);
    21. user.put("age",age);
    22. user.put("account",account);
    23. return user;
    24. }
    25. }

    测试word转pdf

    1. import fr.opensagres.poi.xwpf.converter.pdf.PdfConverter;
    2. import fr.opensagres.poi.xwpf.converter.pdf.PdfOptions;
    3. import org.apache.poi.xwpf.usermodel.XWPFDocument;
    4. import java.io.*;
    5. public class word2pdf {
    6. /**
    7. * @param args the command line arguments
    8. * @throws java.io.IOException
    9. */
    10. public static void main(String[] args) throws IOException {
    11. String docPath = "D:\\wordpdf\\test.docx";
    12. String pdfPath = "D:\\wordpdf\\test.pdf";
    13. XWPFDocument document;
    14. InputStream doc = new FileInputStream(docPath);
    15. document = new XWPFDocument(doc);
    16. PdfOptions options = PdfOptions.create();
    17. OutputStream out = new FileOutputStream(pdfPath);
    18. PdfConverter.getInstance().convert(document, out, options);
    19. doc.close();
    20. out.close();
    21. }
    22. }

    test.docx 截图

    template.docx截图

    效果截图

  • 相关阅读:
    C语言学习1==gcc 创建动态库
    (持续更新中!)详解【计算机类&面试真题】军队文职考试 ——第二期(真题+解析)| 网络协议的三个核心要素;交互式系统中的非剥夺策略;中断和陷入的区别;可变分区管理中的硬件机制;数据库系统的优点
    HTTP - HTTP 1.0、HTTP 1.1、HTTP 2.0的区别
    golang之跨语言ipc通信
    A - Penalty Kick
    Ubuntu 18.04.6 LTS安装docker和docker-compose、镜像导入导出
    人工智能作业第二三章
    SAP UI5 navpopover Factory 的使用方法介绍
    Ant Design Vue 注册全局消息通知组件
    c期末复习
  • 原文地址:https://blog.csdn.net/hyz792901324/article/details/134156553
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号