zoukankan      html  css  js  c++  java
  • jeecg 笔记之 自定义word 模板导出(一)

    1、前言

    jeecg 中已经自带 word 的导出导出功能,其所使用的也是 easypoi,尽管所导出的 word 能满足大部分需求,

    但总是有需要用到自定义 word导出模板,下文所用到的皆是 easypoi 提供的,为方便下次翻阅,故记之。

    2、代码部分

    2.1、controller

    复制代码
    @RequestMapping("/ftl2word")
    public void velocity2word(JeecgDemoExcelEntity jeecgDemoExcel, HttpServletRequest request,
                HttpServletResponse response) throws IOException {
        try {
            jeecgDemoExcel = this.jeecgDemoExcelService.getEntity(JeecgDemoExcelEntity.class, jeecgDemoExcel.getId());
            List<Map<String, Object>> departs = this.systemService.findForJdbc("select id,departname from t_s_depart");
            String docFileName = "word-模板导出测试.doc";
            Map<String, Object> rootMap = new HashMap<String, Object>();
            rootMap.put("info", jeecgDemoExcel);
            rootMap.put("departs", departs);
            // FreemarkerUtil.createFile("exportMyExcel.xls",
            // docFileName,rootMap, request, response,
            // FreemarkerUtil.EXCEL_FILE);
            FreemarkerUtil.createFile("ftl2doc.ftl", docFileName, rootMap, request, response, FreemarkerUtil.WORD_FILE);
         } catch (Exception e) {
             e.printStackTrace();
        }
    }
    复制代码

    2.2、entity

    实体就不扔出来了,详细说一下这个地方:

    复制代码
    jeecgDemoExcel = this.jeecgDemoExcelService.getEntity(JeecgDemoExcelEntity.class, jeecgDemoExcel.getId());
    List<Map<String, Object>> departs = this.systemService.findForJdbc("select id,departname from t_s_depart");
    String docFileName = "word-模板导出测试.doc";
    Map<String, Object> rootMap = new HashMap<String, Object>();
    rootMap.put("info", jeecgDemoExcel);
    rootMap.put("departs", departs);
    复制代码

    jeecgDemoExcel  为 List<实体>,departs 为 List<Map<String, Object>>,怎么用?ftl 语法了解一下?

    2.3、工具类 FreemarkerUtil 

    复制代码
    public class FreemarkerUtil {
        private static final Object LOCK = new Object();
         /**
          * word文件
          */
         public static final int WORD_FILE = 1;
         /**
          * excel文件
          */
         public static final int EXCEL_FILE = 2;
         
         private static Configuration cfg;
         
         private static FreemarkerUtil ftl ;
         
         private FreemarkerUtil(String templateFolder) throws IOException {
             cfg = new Configuration();
             cfg.setDirectoryForTemplateLoading(new File(templateFolder));
             cfg.setObjectWrapper(new DefaultObjectWrapper());
         }
    
         private static void check(HttpServletRequest request) {
                if (ftl == null) {
                    synchronized (LOCK) {
                        try {
                            ftl = new FreemarkerUtil(request.getServletContext().getRealPath("/")+"export/template");
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
               
            }
         
         
            /**
             * 创建 word 文档
             * 必须先设置response导出配置,然后解析模版,否则会出问题
             * @throws IOException 
             */
            public static void createFile(String templateName,String docFileName, Map<String,Object> rootMap,HttpServletRequest request, HttpServletResponse response,int fileType) throws IOException {
          //      response.resetBuffer();
                //设置导出
                response.addHeader("Cache-Control","no-cache");
                response.setCharacterEncoding("UTF-8");
                if( WORD_FILE == fileType){
                    response.setContentType("application/vnd.ms-word;charset=UTF-8");
                }else if(EXCEL_FILE == fileType){
                    response.setContentType("application/octet-stream;charset=UTF-8");
                }else{
                    response.setContentType("application/octet-stream");
                }
                String ua = request.getHeader("user-agent");
                ua = ua == null ? null : ua.toLowerCase();
                if(ua != null && (ua.indexOf("firefox") > 0 || ua.indexOf("safari")>0)){
                    try {
                        docFileName = new String(docFileName.getBytes(),"ISO8859-1");
                         response.addHeader("Content-Disposition","attachment;filename=" + docFileName);
                    } catch (Exception e) {
                    }
                }else{
                    try {
                        docFileName = URLEncoder.encode(docFileName, "utf-8");
                        response.addHeader("Content-Disposition","attachment;filename=" + docFileName);
                    } catch (Exception e) {
                    }
                }
                check(request);
               //解析模版
                Template temp = cfg.getTemplate(templateName, "UTF-8");
                PrintWriter write = response.getWriter();
                try {
                    temp.process(rootMap, write);
                } catch (TemplateException e) {
                    e.printStackTrace();
                }finally {
                     if(write != null){
                        write.flush();
                        write.close();
                    }
                }
            }
    }
    复制代码

    2.4、ftl 模板

    https://files.cnblogs.com/files/niceyoo/ftl2doc.rar

    至于,ftl 如何生成,以及如何写,可自定查询,后面也会单独文章补充。

     博客地址:http://www.cnblogs.com/niceyoo

  • 相关阅读:
    Shared Memory in Windows NT
    Layered Memory Management in Win32
    软件项目管理的75条建议
    Load pdbs when you need it
    Stray pointer 野指针
    About the Rebase and Bind operation in the production of software
    About "Serious Error: No RTTI Data"
    Realizing 4 GB of Address Space[MSDN]
    [bbk4397] 第1集 第一章 AMS介绍
    [bbk3204] 第67集 Chapter 17Monitoring and Detecting Lock Contention(00)
  • 原文地址:https://www.cnblogs.com/Jeely/p/12613785.html
Copyright © 2011-2022 走看看