天天看點

工作總結之----使用freemarker導出word文檔(導出的隻是文本)

controller控制層代碼:

/**
     * 導出方法
     * @return
     */
    @RequestMapping(value = "/export")
    public void export( HttpServletRequest request, HttpServletResponse response) throws Exception {
        //擷取導出資訊
        String ceccAdministrativeWeeklyInfoId = request.getParameter("ceccAdministrativeWeeklyInfoId");
        CeccAdministrativeWeeklyInfo ceccAdministrativeWeeklyInfo = ceccAdministrativeWeeklyInfoService.mySelectById(ceccAdministrativeWeeklyInfoId);
        List<Map<String, String>> list0 = ceccAdministrativeWeeklyInfoService.
                myEditPage0(ceccAdministrativeWeeklyInfoId);
        List<Map<String, String>> list1 = ceccAdministrativeWeeklyInfoService.
                myEditPage1(ceccAdministrativeWeeklyInfoId);
        List<Map<String, String>> list2 = ceccAdministrativeWeeklyInfoService.
                myEditPage2(ceccAdministrativeWeeklyInfoId);
        //将導出資訊存入dataMap中,其中key為模闆中的變量,value為對應要導出的值
        Map<String,Object> dataMap = new HashMap<String,Object>();
        dataMap.put("weeklyPeriods",ceccAdministrativeWeeklyInfo.getWeeklyperiods());
        dataMap.put("startDate",DateUtil.getDateFormat(ceccAdministrativeWeeklyInfo.getStartdate(),"yyyy年MM月dd日"));
        dataMap.put("endDate",DateUtil.getDateFormat(ceccAdministrativeWeeklyInfo.getEnddate(),"yyyy年MM月dd日"));
        if(list0!=null && list0.size()>0){
            List contentInfoCurrent = new ArrayList();
            for(int i=0; i<list0.size(); i++){
                String contentinfo = list0.get(i).get("contentinfo");
                if(contentinfo!=null && !"".equals(contentinfo)){
                    contentInfoCurrent.add(contentinfo);
                }
            }
            dataMap.put("contentInfoCurrent",contentInfoCurrent);
        }
        if(list1!=null && list1.size()>0){
            List contentInfoNext = new ArrayList();
            for(int i=0; i<list1.size(); i++){
                String contentinfo = list1.get(i).get("contentinfo");
                if(contentinfo!=null && !"".equals(contentinfo)){
                    contentInfoNext.add(contentinfo);
                }
            }
            dataMap.put("contentInfoNext",contentInfoNext);
        }if(list2!=null && list2.size()>0){
            List contentInfoPoint = new ArrayList();
            for(int i=0; i<list2.size(); i++){
                String contentinfo = list2.get(i).get("contentinfo");
                if(contentinfo!=null && !"".equals(contentinfo)){
                    contentInfoPoint.add(contentinfo);
                }
            }
            dataMap.put("contentInfoPoint",contentInfoPoint);
        }
        //導出模闆所在檔案夾路徑
        String templatePath = request.getRealPath("/static/template");
        response.reset();
        response.setHeader("Content-disposition", "attachment; filename=" +
                java.net.URLEncoder.encode("導出word檔案名稱.doc", "UTF-8"));
        response.setContentType("application/msword");
        ServletOutputStream out = response.getOutputStream();
        Writer writer = new OutputStreamWriter(out, "UTF-8");
        //zxgsgzzb.ftl---模闆名稱,将資訊導出為word檔案
        ExportTemplate.write(templatePath, "zxgsgzzb.ftl", dataMap, writer);
    }
}      

 導出工具類方法:

import freemarker.template.Configuration;
import freemarker.template.Template;

import java.io.File;
import java.io.IOException;
import java.io.Writer;
import java.util.Map;

public class ExportTemplate {

    private static Configuration configuration;
    static{
        configuration = new Configuration();
        configuration.setDefaultEncoding("UTF-8");
    }
    /**
     * @param templatePath
     * @param templateName
     * @return
     * @throws IOException
     * 擷取模版
     */
    private static Template getTemplate(String templatePath, String templateName) throws IOException{
        File file = new File(templatePath);
        configuration.setDirectoryForTemplateLoading(file);
        Template t = configuration.getTemplate(templateName);
        t.setEncoding("UTF-8");
        return t;
    }

    /**
     * @param templatePath
     * @param templateName
     * @param dataMap
     * @param out
     * 寫入模版
     */
    public static void write(String templatePath, String templateName, Map<String, Object> dataMap, Writer out){
        try {
            Template t = getTemplate(templatePath, templateName);
            t.process(dataMap, out);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
      

 生成模闆參考https://blog.csdn.net/lun379292733/article/details/18673081