使用FreeMark导出数据到Excle表格

一、使用freemarker时需要的jar包:freemarker-2.3.19.jar。

二、根据需求做出导出模板

  1. 做出Excle模板

    这个没什么说的,直接按照需求做出Excle模板,如下:(这里建议用Excle,别用WPS。后面转存为XML文件时容易报错)

    在Excle里面,建议加上如上图的${list.aa},list为需要遍历的变量,aa为遍历后VO里面的字段,方便在转存后的XML里面找到


  2. 修改扩展名为ftl,在ftl里面加上 遍历的标签和变量


三、获取需要导出的数据

可以由前台传回sql筛选条件,在后台拼接后。进行查询数据。

在获得相应的list数据后,放入map

//将资产list放入map
@SuppressWarnings("rawtypes")
Map dataMap = new HashMap<String, List<Lessee>>();
dataMap.put("size", String.valueOf(size));//size是后面提到的表格的初始行数
dataMap.put("list", list);//list 传入模板的list
//编写在Excel里面对应的变量
           
                //模版名称
                        String templateName = "exlessee.ftl";
                        //导出文件的名称
    String exportWord = application.getRealPath("template") + DateUtil.getDateNow("yyyyMMddHHmmss") + ".xls";
                        
                        
                        exportWord(templateName, dataMap, exportWord, "xxx.xls");

四、封装的exportWord方法

/**
      * 导出的工具方法
      */
     
     public void exportWord(String templatePath, Map<String, Object> dataMap,
                String buildFile, String newName)
        {
            try
            {
                Configuration configuration = new Configuration();
                configuration.setDefaultEncoding("utf-8");
                String path = application.getRealPath("template");
                configuration.setDirectoryForTemplateLoading(new File(path));//此处是本类Class.getResource()相对于模版文件的相对路径
                Template template = null;
                File outFile = new File(buildFile);
                Writer writer = null;
                template = configuration.getTemplate(templatePath);
                template.setEncoding("utf-8");
                writer = new BufferedWriter(new OutputStreamWriter(
                        new FileOutputStream(outFile), Charset.forName("utf-8")));//此处为输 出文档编码
                template.process(dataMap, writer);
                writer.flush();
                writer.close();

//                return true;
                
                //设置response的编码方式
                response.setContentType("application/x-msdownload");
                //设置附加文件名
                response.setHeader("Content-Disposition", "attachment;filename="
                        + new String(newName.getBytes("gbk"), "iso-8859-1"));

                //读出文件到i/o流
                FileInputStream fis = new FileInputStream(outFile);
                BufferedInputStream buff = new BufferedInputStream(fis);

                byte[] b = new byte[1024];//相当于我们的缓存

                long k = 0;//该值用于计算当前实际下载了多少字节

                //从response对象中得到输出流,准备下载

                OutputStream myout = response.getOutputStream();

                //开始循环下载

                while (k < outFile.length())
                {

                    int j = buff.read(b, 0, 1024);
                    k += j;

                    //将b中的数据写到客户端的内存
                    myout.write(b, 0, j);

                }

                //将写入到客户端的内存的数据,刷新到磁盘
                myout.flush();
                myout.close();

            } catch (Exception e)
            {
                
                e.printStackTrace();
//                return false;
            }
        }


转载于:https://my.oschina.net/u/1765449/blog/272334

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值