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 ...
随机推荐
- 第二个spring,第三天
陈志棚:成绩的统筹 李天麟:界面音乐 徐侃:代码算法 给位组员继续的完成分配任务.
- 影响MapReduce性能的几个因素
Hadoop MapReduce性能优化影响MapReduce输入数据处理时间的因素很多.其中之一是实现map和reduce函数时使用的算法.其他外部因素也可能影响MapReduce性能.根据我们的经 ...
- python之导入模块
导入模块的方法: 导入整个模块:import module_name 导入特定函数:from module_name import function_name 给函数指定别名:from module_ ...
- cordic
cordic里向量旋转得到新向量,利用的是旋转矩阵 摘自百度百科维基百科 旋转矩阵(Rotation matrix)是在乘以一个向量的时候改变向量的方向但不改变大小的效果的矩阵.旋转矩阵不包括反演,它 ...
- SQL语言分类DQL,DML,DDL,DCL,DTL
SQL语言共分为五大类: 数据查询语言DQL 数据操纵语言DML 数据定义语言DDL 数据控制语言DCL 数据事物语言DTL DQL 数据查询语言DQL基本结构是由SELECT子句,FROM子句,WH ...
- Json Self referencing loop detected
Self referencing loop detected......的错误 解决方案: 1 增加 [JsonIgnore] 过滤关联,使其不参与序列化. 这个方法简单粗暴.但是你就没办法获取关 ...
- 【 HDU - 4456 】Crowd (二维树状数组、cdq分治)
BUPT2017 wintertraining(15) #5A HDU 4456 题意 给你一个n行n列的格子,一开始每个格子值都是0.有M个操作,p=1为第一种操作,给格子(x,y)增加z.p=2为 ...
- UOJ#192. 【UR #14】最强跳蚤
题目链接 http://uoj.ac/problem/192 暑期课第二天 树上问题进阶 具体内容看笔记博客吧 题意 n个节点的树T 边有边权w 求满足(u, v)上所有边权乘积为完全平方数的路径有多 ...
- 精选Spring Boot三十五道必知必会知识点
Spring Boot 是微服务中最好的 Java 框架. 我们建议你能够成为一名 Spring Boot 的专家.本文精选了三十五个常见的Spring Boot知识点,祝你一臂之力! 问题一 Spr ...
- 自学Linux Shell10.1-使用编辑器vim
点击返回 自学Linux命令行与Shell脚本之路 10.1-使用编辑器vim 所有的 Unix系统都会内建 vi 文书编辑器,其他的文书编辑器则不一定会存在.但是目前我们使用比较多的是 vim 编辑 ...