1. 程式人生 > >freemark生成word,及雙層表格。

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>


大功告成。