freemark生成word,及双层表格。

1.生成word模板。

    编辑word使用占位符。

    

双层列表循环表格, 群组里面在嵌套一个表格,无边框。其余正常占位即可。

    

     以xml 格式保存,注意  ${xxxxx} 必须何在一起,不可以分开。

    我这里用的是wps 保存的,(如果用的 word2003,以 2003xml格式保存,保存以 数据格式,保存xml形式。)

        

  


保存之后,后缀名更改为 .ftl 格式。

  

开始代码,获取模板,保存模板。

 String filePath = null;
        Integer len;
        Map<String, Object> map = DocUtil.getContractMap(tabList,contract);
        map.put("access",null == access ? "" : access);
        Writer out = null;
        File file = null;
        try {
            //创建
            Configuration configuration = new Configuration();
            //设置编码
            configuration.setDefaultEncoding("UTF-8");
            // 获得当前类路径
            file = new File(new PrintContract().getClass().getResource("").getPath());
            // ftl模板文件统一放至 test.ftl包下面
            configuration.setDirectoryForTemplateLoading(new File(file + "\\wordTemplate"));
            // 获取模板
            Template template = configuration.getTemplate("contract.ftl", "UTF-8");
//            filePath = file + File.separator + "商标申请合同.doc";
            filePath = file + "\\wordTemplate"+ File.separator + "商标申请合同.doc";
            // 打印word到制定目录
            File  outFile = new File( filePath);
            if (!outFile.getParentFile().exists()) {
                outFile.getParentFile().mkdirs();
            }
            // 将模板和数据模型合并生成文件
            out = new BufferedWriter(new OutputStreamWriter(
                    new FileOutputStream(outFile), "UTF-8"));
            // 生成文件
            template.process(map, out);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (out != null){
                out.flush();
                out.close();
            }

作用使 word字段居中。
/**
 * 获取字符串中文个数
 * @param str
 * @return
 */
public static int getChineseNum(String str){
    return (str.getBytes().length - str.length()) / 2;
}
/**
 * 补全空格
 * @param str
 * @return
 */
public static String getDocString(String str,int in){
    if (str.length() < in){
        in = in - getChineseNum(str) - str.length();
        for (int i=1; i < in; i++){
            str += " ";
        }
    }
    return str;
}
/**
 *  补全空格,居中word格式,
 */
public static Map<String,Object> getContractMap(List<ContractFrom> tabList, Contract contract){

    // 合同编号
    map.put("contractNumber", null == contract.getContractNumber() ? "" : contract.getContractNumber());
    // 甲方 联 系 人
    map.put("firstPartyLinkman", null == contract.getFirstPartyLinkman() ? "" : contract.getFirstPartyLinkman());
    // 甲方联系电话
    map.put("firstPartyPhone", null == contract.getFirstPartyPhone() ? "" : contract.getFirstPartyPhone());
    // 乙方经办人
    map.put("partyBAgent", null == contract.getPartyBAgent() ? "" : contract.getPartyBAgent());
    // 乙方联系电话
    map.put("partyBPhone", null == contract.getPartyBPhone() ? "" : contract.getPartyBPhone());

    // 甲方公司名称 (14位,小于补空格满足)
    map.put("firstParty", null == contract.getFirstParty() ? "\t\t\t\t\t\t\t\t\t " : DocUtil.getDocString(contract.getFirstParty(),36));
    // 甲方 联 系 人 (三位小于补空格)
    map.put("firstPartyContactName", null == contract.getFirstPartyContactName() ? "\t\t\t\t\t\t\t\t " : DocUtil.getDocString(contract.getFirstPartyContactName(),34));
    // 甲方 地址
    map.put("firstPartyAddress", null == contract.getFirstPartyAddress() ? "\t\t\t\t\t\t\t\t\t " : DocUtil.getDocString(contract.getFirstPartyAddress(),36));
    // 甲方 签约日期
    map.put("firstPartyDate", null == contract.getFirstPartyDate() ? "\t\t\t\t\t\t\t\t " : DocUtil.getDocString(sf.format(contract.getFirstPartyDate()),32));
    // 甲方 邮箱账号
    map.put("firstPartyEmail", null == contract.getFirstPartyEmail() ? "\t\t\t\t\t\t\t\t " : DocUtil.getDocString(contract.getFirstPartyEmail(),32));

    //乙方 商标顾问
    map.put("partyBBrandConsultant", null == contract.getPartyBBrandConsultant() ? "" : contract.getPartyBBrandConsultant());
    // 乙方 地址
    map.put("partyBAddress", null == contract.getPartyBAddress() ? "" : contract.getPartyBAddress());
    // 乙方 签约日期
    map.put("partyBDate", null == contract.getPartyBDate() ? "" : sf.format(contract.getPartyBDate()));
    // 乙方 邮箱账号
    map.put("partyBEmail", null == contract.getPartyBEmail() ? "" : contract.getPartyBEmail());

    // 支付金额
    map.put("money", 0.0d == contract.getMoney() ? 0.0 : contract.getMoney());
    if ( null != tabList){
        map.put("tabList",tabList);
    }
    return map;
}



freemarker pom


<!-- freemarker jar -->
<!--<dependency>-->
<!--<groupId>org.freemarker</groupId>-->
<!--<artifactId>freemarker</artifactId>-->
<!--<version>2.3.20</version>-->
<!--</dependency>-->
<dependency>
    <groupId>org.freemarker</groupId>
    <artifactId>freemarker-gae</artifactId>
    <version>2.3.25-incubating</version>
</dependency>


大功告成。

猜你喜欢

转载自blog.csdn.net/qq_41497111/article/details/80318545