zoukankan      html  css  js  c++  java
  • freemarker导出定制excel

    之前我们导excel大部分用的是jxl和poi,JXL只能对Excel进行操作,属于比较老的框架,它只支持到Excel 95-2000的版本。现在已经停止更新和维护

    POI是apache的项目,可对微软的Word,Excel,ppt等进行操作,包括office2003和2007,Excl2003和2007。poi现在一直有更新。所以现在主流使用POI

    如果只是简单的excel,用上述工具导出没有任何问题,但如果导出定制化复杂的excel或word,就会显得很繁琐,代码也有一定难度,所以我尝试用freemarker

    来导出

    先制作一个定制的excel

    新建一个excel,在里面写上点数据并将后缀改为.xml

    将下图的 1和张三改一下以接收数据,将excel复制到项目的resource目录中将后缀名改为.ftl

    到这一步excel已经好了,接下来就是代码

     需要的maven包

    <!--word;excel导出包-->
     <dependency>
       <groupId>org.freemarker</groupId>
       <artifactId>freemarker</artifactId>
       <version>2.3.20</version>
     </dependency>

    导出的方法

    package com.pskj.GSLZ.utils.word;
    
    
    import freemarker.template.Configuration;
    import freemarker.template.Template;
    import freemarker.template.TemplateException;
    import java.io.*;
    import java.util.HashMap;
    import java.util.Map;
    
    /**
     * word,excel导出
     */
    public class FreemarkerWord {
    
        private Configuration configuration = null;
    
    
    
        public FreemarkerWord() {
    
            configuration = new Configuration();
    
            configuration.setDefaultEncoding("utf-8");
    
        }
    
        /**
         * dataMap为要装载的数据
         * @param dataMap
         */
        public void createDoc(Map dataMap) {
    
            // 设置模本装置方法和路径,FreeMarker支持多种模板装载方法。可以重servlet,classpath,数据库装载,
    
            // 这里我的模板是放在resources/ftl包下(放在其它位置总会报文件找不到)
    
            configuration.setClassForTemplateLoading(this.getClass(),
    
                    "/ftl");
    
            Template t = null;
    
            try {
    
                // test2.ftl为要装载的模板
    
                t = configuration.getTemplate("test2.ftl");
    
                t.setEncoding("utf-8");
    
            } catch (IOException e) {
    
                e.printStackTrace();
    
            }
    
            // 输出文档路径及名称
    
            File outFile = new File("E:/test2.xls");
    
            Writer out = null;
    
            try {
    
                out = new BufferedWriter(new OutputStreamWriter(
    
                        new FileOutputStream(outFile), "utf-8"));
    
    
    
            } catch (Exception e1) {
    
                e1.printStackTrace();
    
            }
    
            try {
    
                t.process(dataMap, out);
    
                out.close();
    
            } catch (TemplateException e) {
    
                e.printStackTrace();
    
            } catch (IOException e) {
    
                e.printStackTrace();
    
            }
    
        }
    
        public static void main(String[] args) {
            Map map=new HashMap();
            map.put("id", "1");//添加数据
            map.put("name", "光头权");
            FreemarkerWord fw=new FreemarkerWord();
            fw.createDoc(map);//调用导出方法
        }
    
    
    
    }

    运行之后

    再就是导出多条数据,修改之后的可循环遍历数据

    查询数据库后调用导出方法,数据也能正常导出

     /**
         * 根据excel模板导出数据
         */
        @RequestMapping("listAll")
        @ResponseBody
        public void listAll() {
            PageData pd=this.getPageData();//用于接受参数的封装类
            FreemarkerWord fw=new FreemarkerWord();//实例化该导出类
            Map dataMap = new HashMap();
            try{
                List<Map> list=fhlogService.listAll(pd);//获取多组数据
                dataMap.put("list",list);//将数据装进Map
                fw.createDoc(dataMap);//调用导出方法
            }catch (Exception e){
                e.printStackTrace();
    
            }
    
        }

     参考链接: https://blog.csdn.net/guangcigeyun/article/details/78769704

     参考链接: https://www.jianshu.com/p/66645b71942f

  • 相关阅读:
    武功秘籍 蓝桥杯
    切面条 蓝桥杯
    啤酒和饮料 蓝桥杯
    蚂蚁感冒 蓝桥杯
    hdu N!
    hdu 神、上帝以及老天爷
    ListView滑动删除 ,仿腾讯QQ
    C++ 习题 输出日期时间--友元函数
    C++习题 商品销售
    渠道运营一点事
  • 原文地址:https://www.cnblogs.com/magepi/p/10240131.html
Copyright © 2011-2022 走看看