Springmvc和poi3.9导出excel并弹出下载框
Springmvc 和 poi3.9 用java程序从数据库导出数据到excel(在博客园的第一篇原创博客)
@RequestMapping(value = "/importexcel.htm", method = RequestMethod.GET)
public ModelAndView _importExcel(HttpServletRequest request, HttpServletResponse response, Integer cId) throws Exception { // 获得要导出的数据集
List<Map<String, Object>> list = d_ExchangeCodeService.selectExcelRecord(cId); // 创建excel工作簿
Workbook wb = new HSSFWorkbook();
// 创建第一个sheet(页),并命名
Sheet sheet = wb.createSheet(list.get(0).get("NAME").toString()); // 手动设置列宽。第一个参数表示要为第几列设;,第二个参数表示列的宽度,n为列高的像素数。
sheet.setColumnWidth((short) 0, (short) (35.7 * 150));
sheet.setColumnWidth((short) 1, (short) (35.7 * 150));
sheet.setColumnWidth((short) 2, (short) (35.7 * 150));
sheet.setColumnWidth((short) 3, (short) (35.7 * 100));
sheet.setColumnWidth((short) 4, (short) (35.7 * 250));
sheet.setColumnWidth((short) 5, (short) (35.7 * 150));
sheet.setColumnWidth((short) 6, (short) (35.7 * 150)); // 创建第一行
Row row = sheet.createRow((short) 0); // 创建两种单元格格式
CellStyle cs = wb.createCellStyle();
CellStyle cs2 = wb.createCellStyle();
// DataFormat df = wb.createDataFormat(); // 创建两种字体
Font f = wb.createFont();
Font f2 = wb.createFont(); // 创建第一种字体样式
f.setFontHeightInPoints((short) 10);
f.setColor(IndexedColors.RED.getIndex());
f.setBoldweight(Font.BOLDWEIGHT_BOLD); // 创建第二种字体样式
f2.setFontHeightInPoints((short) 10);
f2.setColor(IndexedColors.BLACK.getIndex());
f2.setBoldweight(Font.BOLDWEIGHT_BOLD); // 设置第一种单元格的样式
cs.setFont(f);
cs.setBorderLeft(CellStyle.BORDER_THIN);
cs.setBorderRight(CellStyle.BORDER_THIN);
cs.setBorderTop(CellStyle.BORDER_THIN);
cs.setBorderBottom(CellStyle.BORDER_THIN);
// cs.setDataFormat(df.getFormat("#,##0.0")); // 设置第二种单元格的样式
cs2.setFont(f2);
cs2.setBorderLeft(CellStyle.BORDER_THIN);
cs2.setBorderRight(CellStyle.BORDER_THIN);
cs2.setBorderTop(CellStyle.BORDER_THIN);
cs2.setBorderBottom(CellStyle.BORDER_THIN);
// cs2.setDataFormat(df.getFormat("text")); // 创建列(每行里的单元格)
Cell cell = row.createCell(0);
cell.setCellValue("用户名");
cell.setCellStyle(cs); cell = row.createCell(1);
cell.setCellValue("订单号");
cell.setCellStyle(cs); cell = row.createCell(2);
cell.setCellValue("兑换券序列号");
cell.setCellStyle(cs); cell = row.createCell(3);
cell.setCellValue("兑换券金额");
cell.setCellStyle(cs); cell = row.createCell(4);
cell.setCellValue("兑换券类型名称");
cell.setCellStyle(cs); cell = row.createCell(5);
cell.setCellValue("使用时间");
cell.setCellStyle(cs); cell = row.createCell(6);
cell.setCellValue("使用结束日期");
cell.setCellStyle(cs); DateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); for (short i = 0; i < list.size(); i++) { // Row 行,Cell 方格 , Row 和 Cell 都是从0开始计数的
// 创建一行,在页sheet上
row = sheet.createRow((short) i + 1);
// 在row行上创建一个方格
cell = row.createCell(0);
cell.setCellValue(list.get(i).get("usr_UserID") == null ? "未使用" : list.get(i).get("usr_UserID").toString());
cell.setCellStyle(cs2); cell = row.createCell(1);
cell.setCellValue(list.get(i).get("ord_OrderID") == null ? "未使用" : list.get(i).get("ord_OrderID").toString());
cell.setCellStyle(cs2); cell = row.createCell(2);
cell.setCellValue(list.get(i).get("Account").toString());
cell.setCellStyle(cs2); cell = row.createCell(3);
cell.setCellValue(Double.parseDouble(list.get(i).get("Amount").toString()));
cell.setCellStyle(cs2); cell = row.createCell(4);
cell.setCellValue(list.get(i).get("NAME").toString());
cell.setCellStyle(cs2); cell = row.createCell(5);
cell.setCellValue(list.get(i).get("UsedTime") == null ? "未使用" : df.format(list.get(i).get("UsedTime")).toString());
cell.setCellStyle(cs2); cell = row.createCell(6);
cell.setCellValue(df.format(list.get(i).get("BlankOutTime")).toString());
cell.setCellStyle(cs2);
} ByteArrayOutputStream os = new ByteArrayOutputStream(); try {
wb.write(os);
} catch (IOException e) {
e.printStackTrace();
} byte[] content = os.toByteArray();
InputStream is = new ByteArrayInputStream(content); // 设置response参数,可以打开下载页面
response.reset();
response.setContentType("application/vnd.ms-excel;charset=utf-8");
response.setHeader("Content-Disposition", "attachment;filename=" + new String((list.get(0).get("NAME").toString() + ".xls").getBytes(), "iso-8859-1")); ServletOutputStream out = response.getOutputStream(); BufferedInputStream bis = null;
BufferedOutputStream bos = null; try { bis = new BufferedInputStream(is);
bos = new BufferedOutputStream(out); byte[] buff = new byte[2048];
int bytesRead; // Simple read/write loop.
while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
bos.write(buff, 0, bytesRead);
} } catch (final IOException e) {
throw e;
} finally {
if (bis != null)
bis.close();
if (bos != null)
bos.close();
}
return null; }
Springmvc和poi3.9导出excel并弹出下载框的更多相关文章
- JAVA导出excel 直接弹出下载框
转自:https://blog.csdn.net/qq_38423105/article/details/80782283 效果展示: 1.首先准备jar包 <dependency> ...
- springmvc导出excel并弹出下载框
https://my.oschina.net/aptx4869/blog/298507
- JavaWeb动态导出Excel可弹出下载
由于项目需求,需要将数据导出成Excel表格,并且可选择导出项,可下载.项目使用的Spring+Mybatis+SpringMVC框架,利用Apache POI导出Excel.POI具体使用请自行百度 ...
- JAVAWeb SSH框架 利用POI 导出EXCEL,弹出保存框
导入包这一些不多说,直接贴出关键代码,JSP只要点一个Action链接就行. poi包我是用:poi-3.11-20141221.jar 亲测有效: 效果: Action 类代码: private I ...
- asp.net导出excel并弹出保存提示框
asp.net导出excel并弹出保存提示框 2013-07-12 | 阅:1 转:78 | 分享 腾讯空间 人人网 开心网 新浪微博 腾讯微博 搜狐空间 推荐给朋友 举报 ...
- 【Java】数据库查询的数据直接以指定文件类型下载到本地(弹出下载框)
欲实现的功能目标:当点击下图的导出数据文件时弹出文件下载框,默认csv格式,用户自定义下载的本地路径 遇到的问题: 1.项目之前做过一次下载,但是是使用了本地文件模板.用输入流读取文件模板,插入数据, ...
- POI导出Excel不弹出保存提示_通过ajax异步请求(post)到后台通过POI导出Excel
实现导出excel的思路是:前端通过ajax的post请求,到后台处理数据,然后把流文件响应到客户端,供客户端下载 文件下载方法如下: public static boolean downloadLo ...
- 【第十七篇】easyui-datagrid 导出Excel (在客户端能弹出下载框)
//导出Excel function exportExcel(obj) { var SaleOrderNo = $("#SaleOrderNo").val().trim(); va ...
- POI文件导出至EXCEL,并弹出下载框
相关参考帖子 : [1]http://www.tuicool.com/articles/MnqeUr [2]http://www.oschina.net/question/253469_51638?f ...
随机推荐
- Java 类的加载
package com.cwcec.p2; class C { public static final int SIZE; static { SIZE = 100; System.out.printl ...
- 10-Python3从入门到实战—基础之函数
Python从入门到实战系列--目录 函数的定义 函数是组织好的,可重复使用的,用来实现单一,或相关联功能的代码段. 函数的语法 def 函数名(参数列表): 函数体 函数代码块以 def 关键词开头 ...
- 无符号整型 unsigned int、unsigned long、usigned long long、size_t 比较和格式控制
位数比较 由于数据的长度和平台相关,所以基于 64 位系统比较. Windows Linux unsigned int 32 bits/4294967295 32 bits unsigned long ...
- Linux中gcc与g++编译器的区别
一.对于C文件 对于C文件gcc和g++所做的事情确实是一样的,g++在编译C文件时调用了gcc. 二.对于C++文件 1.预处理 预处理gcc与g++做了相同的事. 2.编译 gcc无法自动和c++ ...
- Spring 入门知识点笔记整理
一.Spring 概述 1. 什么是spring? Spring 是个java企业级应用的开源开发框架.Spring主要用来开发Java应用,但是有些扩展是针对构建J2EE平台的web应用.Sprin ...
- matplotlib绘图
fig = plt.figure() ax=plt.gca() timeList = np.array(timeList) timeList=timeList*100 timeList1 = np.a ...
- loadrunner基础学习笔记二
virtual user generator(vugen) 在测试环境中,loadrunner在物理计算机上使用vuser代替实际用户.vuser以一种可重复.可预测的方式模拟典型用户的操作,对系统施 ...
- Lodop导出excel及提示成功【回调和直接返回值】
高版本的火狐和谷歌不再支持np插件之后,Lodop公司推出了C-Lodop,解决了这些浏览器不能用Lodop插件方式打印的问题,相比较Lodop插件,C-Lodop由于是以服务的形式出现,返回值不能直 ...
- BZOJ2795&2890&3647[Poi2012]A Horrible Poem——hash
题目描述 给出一个由小写英文字母组成的字符串S,再给出q个询问,要求回答S某个子串的最短循环节.如果字符串B是字符串A的循环节,那么A可以由B重复若干次得到. 输入 第一行一个正整数n (n<= ...
- Uva101-STL模拟
一道有点复杂的STL模拟题,对STL迭代器不太熟悉改了好久,最后总算A了出来. 感觉用数组更方便...但是为了练习STL嘛 对比白书上的代码,我写的还是傻了点.一开始没有理解四个操作的意思,单纯的模拟 ...