springmvc poi实现报表导出
1.pom文件:
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.15</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.15</version>
</dependency>
2.controller 根据需要组装数据:
private List<Map<String, Object>> createList(List<User> users){
if(null == users) {
return null;
}
String[] keys = {"name", "gender", "phoneNo", "email"};
String[] excelHeader = {"姓名","性别", "手机号", "邮箱"};
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
Map<String, Object> model = new HashMap<String, Object>();
model.put("fileName", "用户表" + new Date().getTime());
model.put("sheetName", "表一");
model.put("title", "用户表");
model.put("keys", keys);
model.put("excelHeader", excelHeader);
list.add(model); for(User user : users) {
Map<String, Object> map = new HashMap<String, Object>();
map.put("name", user.getName());
map.put("gender", user.getGender());
map.put("phoneNo", user.getPhoneNo());
map.put("email", user.getEmail());
list.add(map);
}
return list;
}
3.ExcelUtil:
private static final Logger LOG = LoggerFactory.getLogger(ExcelUtil.class); /**创建一个excel文件*/
private static Workbook createWorkBoot(String title,
String[] excelHeader, List<Map<String, Object>> list, String[] keys) {
Workbook workbook = new HSSFWorkbook();
//设置sheet的名字
Sheet sheet = workbook.createSheet(list.get(0).get("sheetName").toString());
/*设置表格宽度*/
for(int i = 0; i < keys.length; i++){
sheet.setColumnWidth(i, 35*150);
} /*font样式设置字体大小,是否加粗*/
Font titleFont = createFont(workbook, (short)20, true);
Font headerFont = createFont(workbook, (short)12, true);
Font bodyFont = createFont(workbook, (short)12, false);
/*cell通用样式*/
CellStyle titleStyle = createStyle(workbook, titleFont);
CellStyle headerStyle = createStyle(workbook, headerFont);
CellStyle bodyStyle = createStyle(workbook, bodyFont); // excel中当前行索引
int index = 0;
/*合并标题的单元格设置标题信息及样式 */
sheet.addMergedRegion(new CellRangeAddress(index, index, index,
excelHeader.length - 1));
Row titleRow = sheet.createRow(index++);
Cell titleCell = titleRow.createCell(0);
titleCell.setCellValue(title);
titleCell.setCellStyle(titleStyle); /*设置表格头信息及样式*/
Row headerRow = sheet.createRow(index++);
for(int i = 0; i < excelHeader.length; i++) {
Cell headerCell = headerRow.createCell(i);
headerCell.setCellValue(excelHeader[i]);
headerCell.setCellStyle(headerStyle);
} /*设置每行每 列的值及样式
*Row为行,cell为方格
*创建i*j个方格并设置对应的属性值*/
for(int i = 1; i < list.size(); i++) {
Row bodyRow = sheet.createRow(index++);
for (int j = 0; j < keys.length; j++) {
Cell bodyCell = bodyRow.createCell(j);
bodyCell.setCellValue(list.get(i).get(keys[j]) == null ?
" " : list.get(i).get(keys[j]).toString());
bodyCell.setCellStyle(bodyStyle);
}
}
return workbook;
} /**设置字体大小,颜色,样式,是否加粗*/
private static Font createFont(Workbook workbook,
short fontHeightInPoints, boolean isBlod) {
Font font = workbook.createFont();
//字体大小
font.setFontHeightInPoints(fontHeightInPoints);
//字体颜色
font.setColor(IndexedColors.BLACK.getIndex());
//字体样式
font.setFontName("宋体");
//是否加粗
font.setBold(isBlod);
return font;
} /**设置字体居中显示,背景色,边框*/
private static CellStyle createStyle(Workbook workbook, Font font) {
CellStyle cellStyle = workbook.createCellStyle();
cellStyle.setFont(font);
//居中
cellStyle.setAlignment(HorizontalAlignment.CENTER);
//背景颜色
cellStyle.setFillForegroundColor(IndexedColors.WHITE.index);
cellStyle.setFillBackgroundColor(IndexedColors.WHITE.index);
cellStyle.setFillPattern(FillPatternType.FINE_DOTS);
//边框
cellStyle.setBorderBottom(BorderStyle.THIN);
cellStyle.setBorderLeft(BorderStyle.THIN);
cellStyle.setBorderRight(BorderStyle.THIN);
cellStyle.setBorderTop(BorderStyle.THIN);
return cellStyle;
} public static boolean exportExcel(HttpServletResponse response,List<Map<String, Object>> list) throws IOException {
String fileName = list.get(0).get("fileName").toString();
String[] excelHeader = (String [])list.get(0).get("excelHeader");
String[] keys = (String [])list.get(0).get("keys");
String title = list.get(0).get("title").toString(); ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
createWorkBoot(title, excelHeader, list, keys).write(baos);
} catch (IOException e) {
LOG.error("将workbook中信息写入输出流时失败");
return false;
}
byte[] content = baos.toByteArray();
InputStream is = new ByteArrayInputStream(content); response.reset();
response.setContentType("application/vnd.ms-excel;charset=utf-8");
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
response.setHeader("Content-Disposition", "attachment;filename="
+ new String((fileName + ".xls").getBytes(), "iso-8859-1"));
ServletOutputStream sos = response.getOutputStream();
bis = new BufferedInputStream(is);
bos = new BufferedOutputStream(sos);
byte[] buff = new byte[2048];
int byteRead = 0;
while (-1 != (byteRead = bis.read(buff, 0, buff.length))) {
bos.write(buff, 0, byteRead);
}
} catch (IOException e) {
LOG.error("创建excel文件时失败");
return false;
} finally {
if (bos != null)
bos.close();
if (bis != null)
bis.close();
if(is != null)
is.close();
if(baos != null)
baos.close();
}
return true;
}
导出效果
springmvc poi实现报表导出的更多相关文章
- Ireport 报表导出 Poi + ireport 导出pdf, word ,excel ,htm
Ireport 报表导出 Poi + ireport 导出pdf, doc ,excel ,html 格式 下面是报表导出工具类reportExportUtils 需要导出以上格式的报表 只需要调用本 ...
- Java使用POI实现数据导出excel报表
Java使用POI实现数据导出excel报表 在上篇文章中,我们简单介绍了java读取word,excel和pdf文档内容 ,但在实际开发中,我们用到最多的是把数据库中数据导出excel报表形式.不仅 ...
- Spring Boot 入门(十二):报表导出,对比poi、jxl和esayExcel的效率
本片博客是紧接着Spring Boot 入门(十一):集成 WebSocket, 实时显示系统日志写的 关于poi.jxl和esayExcel的介绍自行百度. jxl最多支持03版excel,所以单个 ...
- poi报表导出4.1.0版本工具类 导出并下载
这一段时间,由于项目上线基于稳定,所以我这边在基于我们一期迭代的分支上优化一部分我们之前没有做的功能,报表导出.本身之前用的是3.5的版本,但是由于同事要写导入,写的代码只有4.1.0的版本支持,所以 ...
- JAVA将Excel中的报表导出为图片格式(一)问题背景
如题所示,先抛出一个问题,如何使用JAVA将Excel中的报表导出为图片格式? 首先说一下这个问题的背景,也就是为什么博主会碰到这个问题 随着微信,易信之流大行其道,企业内部的办公交流.绩效考评甚至考 ...
- Java中使用poi导入、导出Excel
一.介绍 当前B/S模式已成为应用开发的主流,而在企业办公系统中,常常有客户这样子要求:你要把我们的报表直接用Excel打开(电信系统.银行系统).或者是:我们已经习惯用Excel打印.这样在我们实际 ...
- kettle工具实现报表导出的初步搭建
1.下载kettle 国外网站:http://kettle.pentaho.org/需要FQ,下载慢 2.下载完成启动(windows)-->spoon.bat 3.进入界面,两个主要的tab页 ...
- JasperReport报表导出踩坑实录
写在最前面 翻了翻博客,因为太忙,已经好久没认真总结过了. 正好趁着今天老婆出门团建的机会,记录下最近这段时间遇到的大坑-JasperReport. 六月份的时候写过一篇利用poi文件导入导出的小De ...
- 报表导出之easypoi的应用
报表导出有很多种方法,像之前我有写过的jxl,poi,jasperreport又或者各种商业软件,这次来简单介绍下用了许久的开源轮子easypoi. easypoi的底层原理就不介绍了.因为官方文档的 ...
随机推荐
- Java多线程学习(二)synchronized关键字(2)
转载请备注地址:https://blog.csdn.net/qq_34337272/article/details/79670775 系列文章传送门: Java多线程学习(一)Java多线程入门 Ja ...
- Python标准库笔记(2) — re模块
re模块提供了一系列功能强大的正则表达式(regular expression)工具,它们允许你快速检查给定字符串是否与给定的模式匹配(match函数), 或者包含这个模式(search函数).正则表 ...
- exit() _exit() 函数区别
exit(): --stdlib.h (1) 所有使用atexit()注册的函数,将会被以注册相反的顺序调用: (2) 所有打开的输出流被刷新,并且关闭流: (3) 使用tmpfile()创建的文件将 ...
- Linux 入门记录:六、Linux 硬件相关概念(硬盘、磁盘、磁道、柱面、磁头、扇区、分区、MBR、GPT)
一.硬盘 硬盘的功能相当简单但很重要,它负责记录系统所需要的各种数据.硬盘记录数据有两个方面,一个是硬件方面的存储原理和结构,另外一方面则是软件方面的数据和文件系统.硬盘的主要行为就是数据的存放和取出 ...
- twemproxy 简介、安装配置
twemproxy 简介.安装配置 http://www.xuchanggang.cn/archives/993.html
- 【POJ2420】A star not a tree?
蒟蒻开始学模拟退火…… 起初一直不肯学,因为毕竟玄学算法…… 哎呀玄学怎么就没用呢?对不对? #include<iostream> #include<cstdio> #incl ...
- leetcode 之Reverse Linked List II(15)
这题用需要非常细心,用头插法移动需要考虑先移动哪个,只需三个指针即可. ListNode *reverseList(ListNode *head, int m, int n) { ListNode d ...
- initWithFrame和initWithCoder的区别
如果使用了Interface Builder 方式或nib,就不会调用initWithFrame方法,因为nib文件知道怎么初始化了, 但可以使用initWithCoder这一个更深层的init方法来 ...
- Docker概览
Docker.xmind下载
- GT-----FAQ整理
1.pss0,pss1,这里的序号0和1是什么意思? 说明选的目标调试 App 有至少 2 个进程,先启动的那个进程的 pss 值会被加后缀 0,后启动那个会被加后 缀 1.所有参数前面的“ ...