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

根据Word模板,使用POI生成文档

突然想起来有个小作业:需要根据提供的Word模板填充数据。这里使用POI写了一个小demo验证下。

测试用模板:
在这里插入图片描述

执行结果
在这里插入图片描述

1.引入依赖坐标

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

2.获取相关数据的方法

    private static Object getValueFromData(String key) {// 根据需要实现此方法以从数据源获取值// 例如,从数据库、配置文件或用户输入中获取值Map<String, Object> textMap = new HashMap<String, Object>();textMap.put("title","《模板》");textMap.put("myName","--尉某人--");textMap.put("today","2023-11-02");List<Map> tables = new ArrayList<Map>();Map<String, String> tableMap = new HashMap<String, String>();tableMap.put("name","尉某人");tableMap.put("age","26");tableMap.put("phone","189*******");tables.add(tableMap);tableMap = new HashMap<String, String>();tableMap.put("name","王某人");tableMap.put("age","28");tableMap.put("phone","186*******");tables.add(tableMap);tableMap = new HashMap<String, String>();tableMap.put("name","张某人");tableMap.put("age","24");tableMap.put("phone","130*******");tables.add(tableMap);textMap.put("tables",tables);return textMap.get(key);}

3.解析模板并填充数据

    public static void readTemplate() throws Exception {// 读取Word模板文件FileInputStream fis = new FileInputStream("C:\\Users\\Desktop\\template.docx");XWPFDocument document = new XWPFDocument(fis);// 获取所有段落List<XWPFParagraph> paragraphs = document.getParagraphs();for (XWPFParagraph paragraph : paragraphs) {List<XWPFRun> runs = paragraph.getRuns();for (XWPFRun run : runs) {String text = run.getText(0);if (text != null && text.startsWith("${")) {String key = text.substring(2, text.length() - 1);String value = getValueFromData(key).toString(); run.setText(value, 0);}}}// 获取表格List<XWPFTable> rowTables = document.getTables();for (XWPFTable table : rowTables) {// 遍历表格的每一行for (XWPFTableRow row : table.getRows()) {row.getCell(0).setText("尉某人");row.getCell(1).setText("26");row.getCell(2).setText("おひさしぶりだな");}}// 创建表格并填充数据List<Map> tables = (List<Map>) getValueFromData("tables");int size = tables.size();XWPFTable table = document.createTable(size+1, 3);table.getRow(0).getCell(0).setText("姓名");table.getRow(0).getCell(1).setText("年龄");table.getRow(0).getCell(2).setText("联系方式");for (int i = 0; i < tables.size(); i++) {Map<String,String> map = tables.get(i);XWPFTableRow row = table.getRow(i + 1);row.getCell(0).setText(map.get("name"));row.getCell(1).setText(map.get("age"));row.getCell(2).setText(map.get("phone"));}// 保存文件FileOutputStream fos = new FileOutputStream("C:\\Users\\Desktop\\output.docx");document.write(fos);fos.close();document.close();fis.close();}

4.测试结果

    public static void main(String[] args){try {readTemplate();System.out.println("执行成功!!!");} catch (Exception e) {e.printStackTrace();}}
http://www.lryc.cn/news/218315.html

相关文章:

  • 大语言模型的学习路线和开源模型的学习材料《一》
  • 【案例】3D地球
  • 安全组问题 访问华为云服务器端口
  • 音视频常见问题(七):首开慢
  • [SSD综述1.2] SSD 和 HDD(机械硬盘) 区别?
  • ali sdm docker
  • HCIE-kubernetes(k8s)-Authentication身份验证
  • uniapp开发小程序接入阿里云TTS语音合成(RESTful API)
  • 稳定性测试—fastboot和monkey区别
  • Python库Requests的爬虫程序爬取视频通用模版
  • ngx_http_set_response_header阅读
  • 词典查询工具django-mdict
  • Ubuntu20.04搭建RISC-V和qemu环境
  • 代码生成器
  • AndroidMonitor - 基于AndroidLocalService实现的抓取OKHTTP请求的工具
  • LuatOS-SOC接口文档(air780E)--nbiot - NB-IOT操作库
  • 大数据之LibrA数据库系统告警处理(ALM-12017 磁盘容量不足)
  • Python算法例4 求平方根
  • LVGL_多界面切换
  • C/C++输出字符菱形 2021年3月电子学会青少年软件编程(C/C++)等级考试一级真题答案解析
  • DI93a HESG440355R3 通过其Achilles级认证提供网络安全
  • Go中Panic and Recover
  • webpack 与 grunt、gulp 的不同?
  • 园区网真实详细配置大全案例
  • 小程序video标签在底部出现1px无法去除的黑色线
  • 渗透工具使用及思路总结(持续更新)
  • 速卖通新卖家有必要测评吗?
  • 从lc114. 二叉树展开为链表到lc-LCR 155二叉搜索树转化为排序的双向链表
  • 做读书笔记时的一个高效小技巧
  • Redis7.x 高级篇