JavaWeb开发中采用FreeMarker生成Excel表格
一、方案的对比
二、介绍下FreeMarker方案实现过程
public void exportExcel(){
try {
/** 1.从数据库查出数据 */
List<User> uList = userService.findAll();
/** 2.封装数据 */
Map<String, Object> dataMap = new HashMap<String, Object>();
List<Map<String, String>> userList = new ArrayList<>(uList.size());
for (User u : uList) {
Map<String, String> rowData = new HashMap<>();
rowData.put("name", u.getName());
rowData.put("account", u.getAccount());
rowData.put("dept", u.getDept());
rowData.put("gender", u.isGender() ? "男" : "女");
rowData.put("email", u.getEmail());
userList.add(rowData);
}
dataMap.put("userList", userList);
/** 3.调用FreeMarker的Api生成Excel */
Configuration cfg = new Configuration(Configuration.VERSION_2_3_22);
// 设置加载模板文件的位置
cfg.setServletContextForTemplateLoading(ServletActionContext.getServletContext(), "/ftl");
// 获取模板
Template template = cfg.getTemplate("exportExcel.ftl");
// 设置response的header
HttpServletResponse response = ServletActionContext.getResponse();
// 防止乱码,设置下编码
response.setCharacterEncoding("utf-8");
response.setHeader("Content-Disposition", "attachment; filename=testExcel.xls");
// 生成Excel
template.process(dataMap, response.getWriter());
} catch (Exception e) {
e.printStackTrace();
}
}
public void exportExcel(){
try {
/** 1.从数据库查出数据 */
List<User> uList = userService.findAll();
/** 2.封装数据 */
Map<String, Object> dataMap = new HashMap<String, Object>();
List<Map<String, String>> userList = new ArrayList<>(uList.size());
for (User u : uList) {
Map<String, String> rowData = new HashMap<>();
rowData.put("name", u.getName());
rowData.put("account", u.getAccount());
rowData.put("dept", u.getDept());
rowData.put("gender", u.isGender() ? "男" : "女");
rowData.put("email", u.getEmail());
userList.add(rowData);
}
dataMap.put("userList", userList);
/** 3.调用FreeMarker的Api生成Excel */
Configuration cfg = new Configuration(Configuration.VERSION_2_3_22);
// 设置加载模板文件的位置
cfg.setServletContextForTemplateLoading(ServletActionContext.getServletContext(), "/ftl");
// 获取模板
Template template = cfg.getTemplate("exportExcel.ftl");
// 设置response的header
HttpServletResponse response = ServletActionContext.getResponse();
// 防止乱码,设置下编码
response.setCharacterEncoding("utf-8");
response.setHeader("Content-Disposition", "attachment; filename=testExcel.xls");
// 生成Excel
template.process(dataMap, response.getWriter());
} catch (Exception e) {
e.printStackTrace();
}
}
JavaWeb开发中采用FreeMarker生成Excel表格的更多相关文章
- C#开发中使用Npoi操作excel实例代码
C#开发中使用Npoi操作excel实例代码 出处:西西整理 作者:西西 日期:2012/11/16 9:35:50 [大 中 小] 评论: 0 | 我要发表看法 Npoi 是什么? 1.整个Exce ...
- Java操作Jxl实现导出数据生成Excel表格数据文件
实现:前台用的框架是Easyui+Bootstrap结合使用,需要引入相应的Js.Css文件.页面:Jsp.拦截请求:Servlet.逻辑处理:ClassBean.数据库:SQLserver. 注意: ...
- Javaweb开发中URL路径的使用
看到博客园孤傲苍狼的web系列文章中有关于URL路径的使用文章后,感觉自己对URL的使用清楚了很多,自己再对着动手写一遍以加深记忆. JavaWeb开发中常看到URL以"/"开头, ...
- H5纯前端生成Excel表格
H5纯前端生成Excel表格方法如下: <!DOCTYPE html> <html> <head> <title></title> < ...
- 详解python操作生成excel表格,并且填充数据
最近在研究python操作excel表格的问题,首先读取excel表格觉得平时用的多,不怎么有难度,就是pyhon生成excel表格的时候,平时不怎么用,所以重点研究了一下,现总结如下: 1.首先用到 ...
- 使用Aspose插件将程序中的表格,导出生成excel表格
http://www.cnblogs.com/lanyue52011/p/3372452.html这个是原文地址 /// <summary> /// 点击按钮,将内存表导出excel表格! ...
- 在.NET中使用EPPlus生成Excel报表 .
--摘抄自:http://blog.csdn.net/zhoufoxcn/article/details/14112473 在开发.NET应用中可能会遇到需要生成带图表(Chart)的Excel报表的 ...
- java中使用poi导出excel表格数据并且可以手动修改导出路径
在我们开发项目中,很多时候会提出这样的需求:将前端的某某数据以excel表格导出,今天就给大家写一个简单的模板. 这里我们选择使用poi导出excel: 第一步:导入需要的jar包到 lib 文件夹下
- 数据库数据生成Excel表格(多用在导出数据)
最近在项目开发中遇到这样一个需求,用户聊天模块产品要求记录用户聊天信息,但只保存当天的,每天都要刷新清空数据,但聊天记录要以Excel的形式打印出来,于是就引出了将数据库的数据导出成Excel表格的需 ...
随机推荐
- Windows下更新 npm 和 nodejs
一.更新npm // 将npm更新到最新版本 npm install npm@latest -g 二.更新nodejs 1. 首先通过 where node 命令找到nodejs的安装路径 2. 然后 ...
- 动态导入模块:__import__、importlib、动态导入的使用场景
相关内容: __import__ importlib 动态导入的使用场景 首发时间:2018-02-23 16:06 __import__: 功能: 是一个函数,可以在需要的时候动态导入模块 使用: ...
- [20170628]11g修改用户名.txt
[20170628]11g修改用户名.txt --//昨天看了链接,提到修改用户名:http://www.oratea.com/2017/06/26/oracle-11g%e4%bf%ae%e6%94 ...
- python第六十六天--sqlalchemy
#!usr/bin/env python #-*-coding:utf-8-*- # Author calmyan #python #2017/7/6 21:29 #__author__='Admin ...
- 关于string指针
string str("hello world"); string *pstr = &str; cout << pstr[0] << endl; c ...
- MySQL 复制夯住一例排查以及原理探讨
目录 目录 一 引子 二 故障分析 三 故障解决 四 原理探讨 五 小结 文/温国兵 一 引子 研发反应,有台从库和主库不同步.由于业务读操作是针对从库的,数据不同步必定会带来数据的不一致,业务获取的 ...
- CRM lookup筛选
function Loadcouse() { var type; var id; retrieveRecord(Xrm.Page.getAttribute("ownerid").g ...
- Python3.5中安装Scrapy包时出现问题
在Python3.5中安装Scrapy第三方库 pip install Scrapy 安装到后面出现的这类错误: error: Microsoft Visual C++ 14.0 is require ...
- js根据鼠标和键盘判断页面是否长时间未进行操作
<script> var count = 0; var outTime = 1;//分钟 window.setInterval(go, 1000); function go() { cou ...
- February 8th, 2018 Week 6th Thursday
When you fall in love, friends, let yourself fall. 当你坠入爱河,我的朋友,你就放手去爱吧. To love someone is like movi ...