答:使用xdocreport 生成word

问题原始地址:使用xdocreport 生成word_编程语言-CSDN问答

这个问题解决关键是合并单元格,我看大部分回答都答非所问,下面基于XDOC做了一个示例。

模板地址:https://xdts.xdocin.com/demo/gzwxx.docx

通过${@名称/}标记,实现了循环,通过${#}标记实现了合并单元格。

在线调试地址:https://xdts.xdocin.com/report.html?tmpl=https://xdts.xdocin.com/demo/gzwxx.docx

可以点击【生成数据】生成模拟数据,点击【生成代码】生成多种语言调用代码,如下图:

/**
 * 程序库:https://gitee.com/xdoc/xdoc-client/raw/master/repo/com/xdoc/xdoc-client/1.1.0/xdoc-client-1.1.0.jar
 * 源代码:https://gitee.com/xdoc/xdoc-client
 */
import java.io.File;

import com.xdoc.client.XDtsClient;

public class XDtsDemo {
    public static void main(String[] args) {
        XDtsClient xc = new XDtsClient("https://xdts.xdocin.com/");
        try {
            xc.run("https://xdts.xdocin.com/demo/gzwxx.docx",
                    "{" +
                    "    \"学习年\": \"2023\"," +
                    "    \"学习次数\": \"546\"," +
                    "    \"学习日期\": \"2024-08-01\"," +
                    "    \"学习时长\": \"学习时长-48\"," +
                    "    \"学习地点\": \"梁州市东城区婵雁北街12号\"," +
                    "    \"成员出勤率\": \"346\"," +
                    "    \"未参加人员及原因\": \"未参加人员及原因-1\"," +
                    "    \"学习专题\": [" +
                    "        {" +
                    "            \"专题内容\": \"专题内容-50\"" +
                    "        }," +
                    "        {" +
                    "            \"专题内容\": \"专题内容-73\"" +
                    "        }" +
                    "    ]," +
                    "    \"学习议程\": [" +
                    "        {" +
                    "            \"议程内容\": \"议程内容-82\"" +
                    "        }," +
                    "        {" +
                    "            \"议程内容\": \"议程内容-72\"" +
                    "        }" +
                    "    ]," +
                    "    \"重点发言成员\": [" +
                    "        {" +
                    "            \"姓名\": \"陈树炎\"," +
                    "            \"发言题目\": \"发言题目-96\"" +
                    "        }," +
                    "        {" +
                    "            \"姓名\": \"蒋娥玲\"," +
                    "            \"发言题目\": \"发言题目-24\"" +
                    "        }," +
                    "        {" +
                    "            \"姓名\": \"冯梦岚\"," +
                    "            \"发言题目\": \"发言题目-74\"" +
                    "        }" +
                    "    ]," +
                    "    \"现场照片\": [" +
                    "        {" +
                    "            \"照片URL\": \"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAIAAAD8GO2jAAAASElEQVR42mOYqFXP2bMFSCIz0LiUiDPQwlBkLgOyKFwOTTUl4gy0MBRZhGE0DkbjYDQORuNgNA5G42A0DkbjYDQORuNgkMQBAMqm9z1Onc/iAAAAAElFTkSuQmCC\"" +
                    "        }," +
                    "        {" +
                    "            \"照片URL\": \"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAIAAAD8GO2jAAAAMUlEQVR42mOYydBGU8Swb3oQTRHDqA9GfTDqg1EfjPpg1AejPhj1wagPRn0w6gMqIQA3sIxM5Hs8BQAAAABJRU5ErkJggg==\"" +
                    "        }" +
                    "    ]," +
                    "    \"填报人\": \"杜俊峰\"," +
                    "    \"联系电话\": \"0228-56200369\"" +
                    "}",
                    new File("d:/XDtsResult.docx"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

点击【运行】按钮,生成结果如下:

这样就通过简单的标记,满足了需求。

还有一个小惊喜,这个模板可以作为表单录入数据:

https://xdts.xdocin.com/form.html?tmpl=https://xdts.xdocin.com/demo/gzwxx.docx

更多功能见:https://xdts.xdocin.com

  • 10
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您可以使用XDocReport和FreeMarker模板来生成带页数的Word文档。以下是一个示例代码: 1. 添加以下依赖项到您的项目中: ```xml <dependency> <groupId>fr.opensagres.xdocreport</groupId> <artifactId>xdocreport</artifactId> <version>1.0.6</version> </dependency> <dependency> <groupId>fr.opensagres.xdocreport</groupId> <artifactId>xdocreport-template-freemarker</artifactId> <version>1.0.6</version> </dependency> ``` 2. 创建一个FreeMarker模板,包含一个名为“page”的变量,用于显示当前页数。例如,以下是一个简单的模板: ``` <html> <head> <title>My Document</title> </head> <body> <p>Page ${page} of ${nbPages}</p> <p>This is the content of my document.</p> </body> </html> ``` 3. 在Java代码中使用XDocReport和FreeMarker模板来填充Word文档。以下是一个示例代码: ```java import java.io.FileOutputStream; import java.io.InputStream; import java.io.OutputStream; import java.util.HashMap; import java.util.Map; import org.apache.poi.xwpf.usermodel.XWPFDocument; import fr.opensagres.xdocreport.core.XDocReportException; import fr.opensagres.xdocreport.core.io.XOutputStream; import fr.opensagres.xdocreport.document.registry.XDocReportRegistry; import fr.opensagres.xdocreport.template.TemplateEngineKind; import fr.opensagres.xdocreport.template.formatter.FieldsMetadata; public class GenerateWordDoc { public static void main(String[] args) { try { // Load the template InputStream in = GenerateWordDoc.class.getResourceAsStream("template.docx"); XWPFDocument document = new XWPFDocument(in); FieldsMetadata metadata = new FieldsMetadata(); metadata.addFieldAsTextStyling("page"); XDocReportRegistry.getRegistry().register(document, metadata, TemplateEngineKind.Freemarker); // Populate the template Map<String, Object> context = new HashMap<>(); context.put("page", "${page}"); context.put("nbPages", "${nbPages}"); OutputStream out = new FileOutputStream("output.docx"); XDocReportRegistry.getRegistry().getReport(document, null).process(context, new XOutputStream(out)); // Close the streams in.close(); out.close(); } catch (Exception e) { e.printStackTrace(); } } } ``` 此代码将使用名为“template.docx”的模板创建一个新的Word文档,并将“page”变量替换为当前页数。请注意,您需要使用FieldsMetadata对象来标识文档中的字段,并将其注册到XDocReportRegistry中。最后,代码将输出生成Word文档到名为“output.docx”的文件中。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值