用实例入门freemarker

       官网:https://freemarker.apache.org/docs/pgui_datamodel_basics.html

       参考下面这篇文章较多:https://blog.csdn.net/lun379292733/article/details/18673081,但是这篇文章后面写的util等等很全,我用不上,所以自己写了一个测试方法生成文档就完事儿了。

自己在找资料的时候发现,freemarker根据getting started基本可以完成。根据具体例子来写, 做完感觉好简单呀,以前只是听说过,但是始终没有实践过,所以一直觉得有点摸不到头脑。这件事情说明了,想一万次不如做一次。

       废话不多说,进入正题。首先我的需求是,根据模板,生成word文档,熟悉一下freemarker的使用。其实初识需求是,需要根据列表导出word文档,但是只要接口或者DAO查出数据了,往word模板里塞值就ok了。有了这个demo那些就不是问题了。

模板如下图:

1.准备word的模板,参照上图就是下面图片的模板了

2.文件另存为->xml格式

3.ok,保存成功会有一个xml文件。在修改这个文件的后缀,改为.ftl

4.在要循环的表格里面添加标签,注意这时候你直接打开ftl会发现没有换行,只要粘贴出来,去网站上xml格式化一下就好了。

找到刚才我们写的表格变量,加上<#list newList  as list><#list>就可以了。和c标签很像

<#list newList  as list>
                        <w:tr>
                            <w:tblPrEx>
                              ...省略...
                                    
                                        </w:rPr>
                                        <w:t>${list.title}</w:t>
                                    </w:r>
                                    <w:bookmarkStart w:id="0" w:name="_GoBack"/>
                                    <w:bookmarkEnd w:id="0"/>
                                </w:p>
                            </w:tc>
                           ...省略...                                    
                                        </w:rPr>
                                        <w:t>${list.content}</w:t>
                                    </w:r>
                                </w:p>
                            </w:tc>
                           ...省略...
                                        </w:rPr>
                                        <w:t>${list.author}</w:t>
                                    </w:r>
                                </w:p>
                            </w:tc>
                        </w:tr>
</#list>

5.准备好之后,我放到了resources文件夹下,一开始我随便放的,后来发现读取不到文件路径,所以一定放在resources下,你可以再加一层文件夹,这个代码中指定就ok了

6.编写代码

 WordTest类

import freemarker.template.*;
import java.util.*;
import java.io.*;

public class WordTest {
    public static void main(String[] args) throws Exception {
        /* Create and adjust the configuration singleton */
        Configuration cfg = new Configuration();
        // 这就是ftl文件所在文件夹的路径
        cfg.setDirectoryForTemplateLoading(new File("src/main/resources/template"));
        cfg.setDefaultEncoding("UTF-8");
        cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);

        List<TableDTO> newList = new ArrayList<TableDTO>(); // 表格的内容
        for (int i=0; i<4; i++) {
            TableDTO tableDTO = new TableDTO();
            tableDTO.setAuthor("author" + i);
            tableDTO.setContent("content" + i);
            tableDTO.setTitle("title" + i);
            newList.add(tableDTO);
        }
        
        // 文档中的内容
        WordModel wordModel = new WordModel();
        wordModel.setContent("TestTestTestTestTest");
        wordModel.setCurrDate("20190201");
        wordModel.setUsername("Lynee");
        wordModel.setNewList(newList);
        
        /* Get the template (uses cache internally) */
        Template temp = cfg.getTemplate("test.ftl"); // 指定ftl

        /* Merge data-model with template */
        // 运行完毕程序,会生成到指定位置,生成指定文件
        Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("E:/generator-output/wordtest.doc"), "UTF-8"));
        temp.process(wordModel, out);
        out.close();
    }
}

下面的是dto

WordModel  

import java.util.List;

public class WordModel {
    
    private String username;
    private String currDate;
    private String content;
    private List<TableDTO> newList; // 这个名字要和ftl里面的值对应
    
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getCurrDate() {
        return currDate;
    }
    public void setCurrDate(String currDate) {
        this.currDate = currDate;
    }
    public String getContent() {
        return content;
    }
    public void setContent(String content) {
        this.content = content;
    }
    public List<TableDTO> getNewList() {
        return newList;
    }
    public void setNewList(List<TableDTO> newList) {
        this.newList = newList;
    }
    
}

TableDTO 

public class TableDTO {
    private String title;
    private String content;
    private String author;
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    public String getContent() {
        return content;
    }
    public void setContent(String content) {
        this.content = content;
    }
    public String getAuthor() {
        return author;
    }
    public void setAuthor(String author) {
        this.author = author;
    }
}

ftl文件:https://download.csdn.net/download/xueshuiyy/10947943

好了,执行wordtest就完成任务了,我的doc文件会生成在E:/generator-output/wordtest.doc这个位置。打开看一下就是开篇图片的内容了。

猜你喜欢

转载自blog.csdn.net/xueshuiyy/article/details/86748009