freemaker生成数据文件

var androidMenuVersion = 0;
androidhome=[
<#if androidNodeList ??>
<#list androidNodeList as nodeList>
{"text":"${nodeList.nodeName}","img":"","Url":"${nodeList.nodeUrl}",
    <#if nodeList.bigPosition ??>
        "bigposter":[
         {"text":"","img":"${nodeList.bigPosition.image1}","url":"${nodeList.bigPosition.url1}"},
         {"text":"","img":"${nodeList.bigPosition.image2}","url":"${nodeList.bigPosition.url2}"},
         {"text":"","img":"${nodeList.bigPosition.image3}","url":"${nodeList.bigPosition.url3}"},
         {"text":"","img":"${nodeList.bigPosition.image4}","url":"${nodeList.bigPosition.url4}"},
         {"text":"","img":"${nodeList.bigPosition.image5}","url":"${nodeList.bigPosition.url5}"},
         {"text":"","img":"${nodeList.bigPosition.image6}","url":"${nodeList.bigPosition.url6}"}]</#if>
    <#if nodeList.bycPosition ??>
         "byc":[
         {"text":"","img":"${nodeList.bycPosition.image1}","url":"${nodeList.bycPosition.url1}"},
         {"text":"","img":"${nodeList.bycPosition.image2}","url":"${nodeList.bycPosition.url2}"},
         {"text":"","img":"${nodeList.bycPosition.image3}","url":"${nodeList.bycPosition.url3}"},
         {"text":"","img":"${nodeList.bycPosition.image4}","url":"${nodeList.bycPosition.url4}"},
         {"text":"","img":"${nodeList.bycPosition.image5}","url":"${nodeList.bycPosition.url5}"},
         {"text":"","img":"${nodeList.bycPosition.image6}","url":"${nodeList.bycPosition.url6}"}]</#if>
    <#if nodeList.pbPosition ??>
         "pb":[
         {"text":"","img":"${nodeList.pbPosition.image1}","url":"${nodeList.pbPosition.url1}"},
         {"text":"","img":"${nodeList.pbPosition.image2}","url":"${nodeList.pbPosition.url2}"},
         {"text":"","img":"${nodeList.pbPosition.image3}","url":"${nodeList.pbPosition.url3}"},
         {"text":"","img":"${nodeList.pbPosition.image4}","url":"${nodeList.pbPosition.url4}"},
         {"text":"","img":"${nodeList.pbPosition.image5}","url":"${nodeList.pbPosition.url5}"},
         {"text":"","img":"${nodeList.pbPosition.image6}","url":"${nodeList.pbPosition.url6}"}]</#if>
    <#if nodeList.applicationList ??>
    <#list nodeList.applicationList as appList >
    <#if appList ??>
    <#list appList as application>
    <#if "${application.appName}" == "weather" >
        weather:{"text":"${application.appName}",img:"","url":"${application.appUrl}"}</#if>
    <#if "${application.appName}" == "calendar" >
        calendar:{"text":"${application.appName}",img:"","url":"${application.appUrl}"}</#if>
    <#if "${application.appName}" == "message" >
        message:{"text":"${application.appName}",img:"","url":"${application.appUrl}"}</#if>
    </#list></#if><#if appList_has_next></#if></#list></#if>         
}
</#list>
</#if>]

Freemarker 是一个模板引擎,可以将模板文件与数据合并生成新的文件,因此可以利用 Freemarker 生成 docx 文件。 以下是一个简单的示例: 1. 引入依赖 ```xml <dependency> <groupId>org.freemarker</groupId> <artifactId>freemarker</artifactId> <version>2.3.30</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>4.1.2</version> </dependency> ``` 2. 创建模板 创建一个 docx 模板文件,使用 Word 编辑器打开,添加需要动态生成的内容,例如表格、文本等。 3. 编写 Freemarker 模板 在模板中使用 Freemarker 语法,例如: ```xml <#list users as user> <p>${user.name}</p> <p>${user.age}</p> </#list> ``` 其中 `users` 是数据源,`user` 是遍历的变量。 4. 生成 docx 文件 读取模板文件,合并数据源,生成 docx 文件: ```java public void generateDocx() throws Exception { Configuration cfg = new Configuration(Configuration.VERSION_2_3_30); cfg.setClassForTemplateLoading(this.getClass(), "/templates/"); cfg.setDefaultEncoding("UTF-8"); Template template = cfg.getTemplate("template.docx"); Map<String, Object> data = new HashMap<>(); List<User> users = new ArrayList<>(); users.add(new User("Tom", 20)); users.add(new User("Jerry", 18)); data.put("users", users); XWPFDocument doc = new XWPFDocument(); OutputStream out = new FileOutputStream("output.docx"); Writer writer = new OutputStreamWriter(out, "UTF-8"); template.process(data, writer); doc.write(out); out.close(); } ``` 其中 `User` 是数据模型,`template.docx` 是模板文件名,`output.docx` 是生成文件名。 运行代码即可生成 docx 文件
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值