freemark简单导出word

freemark导出word,对日期图片还未做处理

1.例如:一个word文档,把基本格式写好,然后另存为:.xml文件
在这里插入图片描述
在这里插入图片描述
2.将.word-template.xml文件放到template|(你存放的文件夹,自定义)文件夹下
在这里插入图片描述
3.将word-template.xml文件进行格式化,然后写一个list标签,按照word文档中的定义写,如图:
<#list clothingList as clothing></#list>

在这里插入图片描述
4.导入word的工具类,同样适用于Excel,此处的只是简单针对于字符串的,日期图片都没有做处理

/**
     *
     * 导出word,Excel公用  对于日期图片不可以
     * @param response
     * @param map 查询的list集合,其他一些数据都放到map中
     * @param fileName 导入的模板的文件名
     * @param suffixName  生成的文件的后缀名 例如:.xlsx .docx
     */
    public static void downloadExcel(HttpServletResponse response, Map map,String fileName,String suffixName)  {
        Configuration configuration = new Configuration();
        //configuration.setDefaultEncoding("utf-8");
        //有两种方式获取你的模板,模板在项目中时用第一个,模板在本地时用第二个。
        //注意:两种方式的路径都只需要写到模板的上一级目录
        //configuration.setClassForTemplateLoading(this.getClass(), "/template");
        //this.getClass()在static中不可使用,可以使用类名.class获取class
        //template为模板的文件夹名,需要写成常量,容易更改
        configuration.setClassForTemplateLoading(ImportExcelUtil.class, Const.IMPORT_TEMPLATE_PATH);
        //  configuration.setDirectoryForTemplateLoading(new File("C:/"));
        //File outFile = new File("D:/outFile/"+Math.random()*10000+".xls");//输出路径
        response.setContentType("application/octet-stream");
        //response.setHeader("Content-disposition", "attachment;filename=" + UUID.randomUUID().toString() + ".xlsx");
        //后缀名作为动态的
        response.setHeader("Content-disposition", "attachment;filename=" + UUID.randomUUID().toString() + suffixName);
        Template t = null;
        OutputStream out = null;
        OutputStreamWriter writer = null;
        try {
           // t = configuration.getTemplate("excel-template.xml", "utf-8"); //文件名,获取模板
            //fileName,传过来的作为常量
            t = configuration.getTemplate(fileName, "utf-8"); //文件名,获取模板
            out = response.getOutputStream();
            writer = new OutputStreamWriter(out, "utf-8");
            t.process(map, writer);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (out != null) {
                    out.close();
                }
                if (writer != null) {
                    writer.close();
                }

            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

常量类:将模板都进行常量存储:
在这里插入图片描述
5.contoller层

 /**
     * 使用工具类将数据导出word,对于图片,日期不可以
     *
     * @param response
     */
    @RequestMapping("downloadWord")
    public void downloadWord(HttpServletResponse response) {
    //查询出来的数据存放到map中
        List list = clothingService.queryAllListNoPage();
        Map map = new HashMap();
        map.put("clothingList", list);
        map.put("time", "2020-11-11");
        map.put("phoneClothing", "12235467");
        ImportExcelUtil.downloadExcel(response, map, Const.IMPORT_WORD_PATH, ".docx");

    }

然后导出就完成了
在这里插入图片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章