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冲刺第9天
今天是第三阶段冲刺的最后第二天了,我们该实现的功能基本已经全部实现了,有填空的,选择题的,还有计时的,目前就是在查BUG,看看有哪些地方有BUG需要修改,以下截图是我们团队所做的功能截图: 首页: 填 ...
- shell脚本--分支、条件判断
在看选择判断结构之前,请务必先看一下数值比较与文件测试 if....else... #!/bin/bash #文件名:test.sh score=66 # //格式一 if [ $score -lt ...
- Jenkins and Python
https://jenkins.io/solutions/python/ In the Python ecosystem there are tools which can be integrated ...
- error eslint@5.12.0: The engine "node" is incompatible with this module.
初始化 react项目时报错: error eslint@5.12.0: The engine "node" is incompatible with this module. E ...
- RabbitMQ基础知识详解
什么是MQ? MQ全称为Message Queue, 消息队列(MQ)是一种应用程序对应用程序的通信方法.MQ是消费-生产者模型的一个典型的代表,一端往消息队列中不断写入消息,而另一端则可以读取队列中 ...
- html5 视频和音频
视频:html5支持视屏文件或者视屏流. html5使用video元素来播放视屏,支持的类型有OGG,MEPG 4,webM,但是不同的浏览器支持类型不同. src可以放置视屏文件的路径,可以使用元素 ...
- 文件IO流完成文件的复制(复杂版本主要用来演示各种流的用途,不是最佳复制方案哦)
package io; import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.File;import j ...
- JavaScript——事件机制
事件是将JavaScript脚本与网页联系在一起的主要方式,是JavaScript中最重要的主题之一,深入理解事件的工作机制以及它们对性能的影响至关重要.本文将详细介绍JavaScript的事件机制, ...
- LAMP和LNMP去除index.php访问
使用TP或者Laravel开发的时候,后时候会遇到需要加index.php才能正常访问 LAMP解决方法 1.修改配置 打开配置文件(如:httpd.conf),找到你网站根目录的配置,将AllowO ...
- hud 1312
There is a rectangular room, covered with square tiles. Each tile is colored either red or black. A ...