Freemarker + xml 實現Java導出word

前言

最近作了一個調查問卷導出的功能,需求是將維護的題目,答案,導出成word,參考了幾種方案以後,選擇功能強大的freemarker+固定格式以後的wordxml實現導出功能。導出word的代碼是能夠直接複用的,因而在此貼出,並進行總結,方便你們拿走。後端

實現過程概覽

先在word上,調整好本身想要的樣子。而後存爲xml文件。保存爲freemarker模板,以ftl後綴結尾。將須要替換的變量使用freemarker的語法進行替換。最終將數據準備好,和模板進行渲染,生成文件並返回給瀏覽器流。瀏覽器

詳細的實現過程

準備好word的樣式

咱們新建一個word,咱們應該使用Microsoft office,若是使用wps可能會形成樣式有些不兼容。在新建的office中,設置好咱們的表格樣式。咱們的調查問卷涉及到四種類型,單選,多選,填空,簡答。咱們作出四種類型的示例。 樣式沒有問題後,咱們選擇另存爲word xml 2003版本。將會生成一個xml文件。 app

格式化xml,並用freemarker語法替換xml

咱們能夠先下載一個工具 firstobject xml editor,這個能夠幫助咱們查看xml,同時方便咱們定位咱們須要改的位置。 複製過去以後,按f8能夠將其進行格式化,左側是標籤,右側是內容,咱們只須要關注w:body便可。 像右側的調查問卷這個就是個標題,咱們實際渲染的時候應該將其進行替換,好比咱們的程序數據map中,有title屬性,咱們想要這裏展現,咱們就使用語法$便可。 freemarker的具體語法,能夠參考freemarker的問題,在這裏我給出幾個簡單的例子。 好比咱們將全部的數據放置在dataList中,因此咱們須要判斷,dataList是否是空,是空,咱們不該該進行下面的邏輯,不是空,咱們應該先循環題目是必須的,答案是須要根據類型進行再次循環的。語法參考文檔,這裏再也不贅述。工具

程序端引入freemarker

<dependency>
    <groupId>org.freemarker</groupId>
    <artifactId>freemarker</artifactId>
</dependency>

將咱們的flt文件放在resources下的templates下。3d

後端代碼實現

此代碼能夠複用,在此貼出code

public class WordUtils {

    private static Configuration configuration = null;
    private static final String templateFolder = WordUtils.class.getClassLoader().getResource("").getPath()+"/templates/word";
    static {
        configuration = new Configuration();
        configuration.setDefaultEncoding("utf-8");
        try {
            configuration.setDirectoryForTemplateLoading(new File(templateFolder));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     *  @Description:導出word,傳入request,response,map就是值,title是導出問卷名,ftl是你要使用的模板名
     */
    public static void exportWord(HttpServletRequest request, HttpServletResponse response, Map map, String title, String ftlFile) throws Exception {
        Template freemarkerTemplate = configuration.getTemplate(ftlFile);
        File file = null;
        InputStream fin = null;
        ServletOutputStream out = null;
        try {
            file = createDocFile(map,freemarkerTemplate);
            fin = new FileInputStream(file);
            String fileName = title + ".doc";
			response.setCharacterEncoding("utf-8");
			response.setContentType("application/msword");
			response.setHeader("Content-Disposition", "attachment;filename="
             +fileName);
			out = response.getOutputStream();
            byte[] buffer = new byte[512];  
            int bytesToRead = -1;
            while((bytesToRead = fin.read(buffer)) != -1) {
                out.write(buffer, 0, bytesToRead);
            }
        }finally {
            if(fin != null) fin.close();
            if(out != null) out.close();
            if(file != null) file.delete(); 
        }
    }

    /**
     *  @Description:建立doc文件
     */
    private static File createDocFile(Map<?, ?> dataMap, Template template) {
        File file = new File("init.doc");
        try {
            Writer writer = new OutputStreamWriter(new FileOutputStream(file), "utf-8");
            template.process(dataMap, writer);
            writer.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return file;
    }

}

有了工具類後,咱們準備好咱們的map數據。map裏面的數據你們能夠自行定義。而後調用utils中的導出方法便可。xml

WordUtils.exportWord(request, response, dataMap, "word", "demo.ftl");

結語

至此已經結束了,十分的好用,有疑問的話,能夠評論交流。blog

相關文章
相關標籤/搜索