java freemark產生word文檔

來源:互聯網
上載者:User

標籤:

1.下載freemarker-2.3.19.jar

2.把要填充的內容用  ${title},${no}代替

3.用word 開啟,儲存為2003xml

4.開啟產生xml檔案,看下有沒有把運算式  ${title},${no}   分開,如果分開了,那麼要合并,最後儲存為ftl檔案

5.代碼

public class DocUitlsDemo{    private Configuration configuration=null;    public DocUitlsDemo(){        configuration=new Configuration();//        configuration.setClassicCompatible(false);//空值處理             configuration.setDefaultEncoding("UTF-8");    }    /**     * 產生word     * @param tmplatePath 模板的路徑 /com/document/temlpate     * @param templateName  "text.ftl"     * @param data       map集合的資料     * @param outputFile 檔案輸出路徑     * @throws Exception     */    public void createDoc(File tmplatePath,String templateName,Map<?,?> data,File outputFile) throws Exception {        //設定模本裝置方法和路徑,FreeMarker支援多種模板裝載方法。可以重servlet,classpath,資料庫裝載,        //這裡我們的模板是放在com.havenliu.document.template包下面        configuration.setDirectoryForTemplateLoading(tmplatePath);        Template t=null;        t = configuration.getTemplate(templateName);        //輸出文檔路徑及名稱        Writer out = null;      //解決出現亂碼        out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outputFile),"UTF-8"));        t.process(data, out);        out.close();    }    public static void main(String[] args)    {        Calendar calendar=Calendar.getInstance();        int year=calendar.get(Calendar.YEAR);        int month=calendar.get(Calendar.MONTH)+1;        int day=calendar.get(Calendar.DAY_OF_MONTH);        DocUitlsDemo demo=new DocUitlsDemo();                Map<String , Object>data=new HashMap<String, Object>();        data.put("no", "gk001");        data.put("year", year+"");//這裡的年份要用字串來表示,不然的話,就當成數字了,會有 逗號的        data.put("month", month);        data.put("day", day);        data.put("title", "2015-1-1");        File tmplatePath=new File("d://");        String tempalteName="template.ftl";        File outputFile=new File(tmplatePath,"xx合約.doc");        try        {            demo.createDoc(tmplatePath, tempalteName, data, outputFile);        }        catch (Exception e)        {            e.printStackTrace();        }        System.out.println("產生成功");    }}

 

java freemark產生word文檔

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.