January 12, 2022
By: Serena

FreeMarker

FreeMarker

背景

生成任务结果的文书,如下图所示:

什么是 FreeMarker?

FreeMarker 是一款 模板引擎: 即一种基于模板和要改变的数据, 并用来生成输出文本(HTML网页,电子邮件,配置文件,源代码等)的通用工具。 它不是面向最终用户的,而是一个Java类库,是一款程序员可以嵌入他们所开发产品的组件。

模板编写为FreeMarker Template Language (FTL)

操作流程

生成模板ftl

word 文档.doc

在word文档(一定是.doc,不能是.docx),将需要改变的数据用变量填写,如下图 任务名称:${main_task_name}

.doc另存为.xml

格式化存储方便查看 param.png

.xml 后缀改成.ftl

mv report-template-pdsm1.xml report-template-pdsm1.ftl

准备参数

(def pdsm1-param
  {:main_task_name "频扫"
   :main_func_name "频段扫描"
   :sub_func_names "sub频段扫描"
   :count 1
   :xhpl 5000
   :xhdk 600
   :xhcq 70
   :time "2021-11-26 12:12:56"})

产生文书

使用 git@github.com:grinnbearit/freemarker-clj.git 目标文件制定为.doc即可

(defn generate-result
  [template-file param target-file]
  (spit target-file (render cfg template-file param)))
  
(def pdsm1-template-file "serena/report-template-pdsm1.ftl")
(def pdsm1-target-file "serena/report-pdsm1.doc")


(def pdsm1-param
  {:main_task_name "频扫"
   :main_func_name "频段扫描"
   :sub_func_names "sub频段扫描"
   :count 1
   :xhpl 5000
   :xhdk 600
   :xhcq 70
   :time "2021-11-26 12:12:56"})

(generate-result pdsm1-template-file pdsm1-param pdsm1-target-file )

支持数组展示

单层

(def pdsm2-param
  {:main_task_name "频扫"
   :main_func_name "频段扫描"
   :sub_func_names "sub频段扫描"
   :results [{:count 1
              :xhpl 5000
              :xhdk 600
              :xhcq 70
              :time "2021-11-26 12:12:56"}
             {:count 2
              :xhpl 3000
              :xhdk 400
              :xhcq 80
              :time "2021-11-26 10:52:56"}]})

(def pdsm2-target-file "serena/report-pdsm2.doc")
(def pdsm2-template-file-new "serena/report-template-pdsm.ftl")
(generate-result pdsm2-template-file-new pdsm2-param pdsm2-target-file)

模板增加list逻辑

在需要循环的地方增加

<#list results as result>
  <p>${result.count}
</#list>

list.png

多层

用if来判断

                <#list sub_task_results as sub_task>
                    <#if sub_task.type = "dchj">
                    <wx:sub-section>
                      ...
                     </#if>

rh.png

(def rh-param
  {:main_task_name "融合"
   :main_func_name "融合任务"
   :sub_func_names "电磁环境/频段扫描"
   :sub_task_results [
             {:type "dchj"
              :sub_task_name "电磁环境子任务"
              :results [{:index 1
                         :signal_type 5000
                         :signal_frea 600
                         :bandwidth 70
                         :signal_field_strength 11
                         :time "2021-11-26 12:12:56"
                         :carrier_to_noise_ratio 12
                         :model_type 1
                         :code_rate 22
                         :modulation_degree 33
                         :freq_shift 44
                         :directional_degree 55
                         :pitch 88
                         :quality "yyyy"}
                        {:index 2
                         :signal_type 5002
                         :signal_frea 602
                         :bandwidth 72
                         :signal_field_strength 11
                         :time "2021-11-22 12:12:56"
                         :carrier_to_noise_ratio 12
                         :model_type 1
                         :code_rate 22
                         :modulation_degree 33
                         :freq_shift 44
                         :directional_degree 55
                         :pitch 88
                         :quality "yyyy"}]
              }
             {:sub_func_type "pdsm"
              :sub_task_name "频段扫描子任务"
              :results [{:index 1
                                 :type 5000
                                 :freq 600
                                 :bandwidth 70
                                 :signal_field_strength 11
                                 :time "2021-11-26 12:12:56"
                                 :ratio 12
                                 :style 1
                                 :bitrate 22
                                 :module_degree 33
                                 :freq_offset 44
                                 :angle 55
                                 :angle_of_pitch 88
                                 :quality "yyyy"}
                                {:index 2
                                 :type 50002
                                 :freq 6002
                                 :bandwidth 702
                                 :signal_field_strength 112
                                 :time "2021-11-29 12:12:56"
                                 :ratio 122
                                 :style 12
                                 :bitrate 222
                                 :module_degree 332
                                 :freq_offset 442
                                 :angle 552
                                 :angle_of_pitch 882
                                 :quality "xxx"}]}
             ]})
Tags: FreeMarker