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


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

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

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

    1.引入依赖坐标

            <dependency>
                <groupId>org.apache.poigroupId>
                <artifactId>poi-ooxmlartifactId>
                <version>4.1.2version>
            dependency>
            <dependency>
                <groupId>org.apache.poigroupId>
                <artifactId>poiartifactId>
                <version>4.1.2version>
            dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    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);
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

    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();
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53

    4.测试结果

        public static void main(String[] args){
            try {
                readTemplate();
                System.out.println("执行成功!!!");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
  • 相关阅读:
    ROS学习(24)plugin插件
    数据结构之队列
    3.canal解析binlog送入kafka
    历史性的时刻!OpenTiny 跨端、跨框架组件库正式升级 TypeScript,10 万行代码重获新生!
    系统分析师备考经验分享:分阶段、分重点
    【Vue-Demo】倒计时3秒后返回首页
    非小细胞肺癌靶向用药整理
    数据服务:B站数据服务的演进之路
    java计算机毕业设计框架的电脑测评系统源码+数据库+系统+lw文档+mybatis+运行部署
    每天都很煎熬,领导派的活太难,真的想跑路了
  • 原文地址:https://blog.csdn.net/ll123151190/article/details/134193821