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并弹出下载框的更多相关文章

  1. JAVA导出excel 直接弹出下载框

    转自:https://blog.csdn.net/qq_38423105/article/details/80782283 效果展示: 1.首先准备jar包 <dependency>    ...

  2. springmvc导出excel并弹出下载框

    https://my.oschina.net/aptx4869/blog/298507

  3. JavaWeb动态导出Excel可弹出下载

    由于项目需求,需要将数据导出成Excel表格,并且可选择导出项,可下载.项目使用的Spring+Mybatis+SpringMVC框架,利用Apache POI导出Excel.POI具体使用请自行百度 ...

  4. JAVAWeb SSH框架 利用POI 导出EXCEL,弹出保存框

    导入包这一些不多说,直接贴出关键代码,JSP只要点一个Action链接就行. poi包我是用:poi-3.11-20141221.jar 亲测有效: 效果: Action 类代码: private I ...

  5. asp.net导出excel并弹出保存提示框

    asp.net导出excel并弹出保存提示框 2013-07-12 | 阅:1  转:78   |  分享  腾讯空间 人人网 开心网 新浪微博 腾讯微博 搜狐空间 推荐给朋友 举报          ...

  6. 【Java】数据库查询的数据直接以指定文件类型下载到本地(弹出下载框)

    欲实现的功能目标:当点击下图的导出数据文件时弹出文件下载框,默认csv格式,用户自定义下载的本地路径 遇到的问题: 1.项目之前做过一次下载,但是是使用了本地文件模板.用输入流读取文件模板,插入数据, ...

  7. POI导出Excel不弹出保存提示_通过ajax异步请求(post)到后台通过POI导出Excel

    实现导出excel的思路是:前端通过ajax的post请求,到后台处理数据,然后把流文件响应到客户端,供客户端下载 文件下载方法如下: public static boolean downloadLo ...

  8. 【第十七篇】easyui-datagrid 导出Excel (在客户端能弹出下载框)

    //导出Excel function exportExcel(obj) { var SaleOrderNo = $("#SaleOrderNo").val().trim(); va ...

  9. POI文件导出至EXCEL,并弹出下载框

    相关参考帖子 : [1]http://www.tuicool.com/articles/MnqeUr [2]http://www.oschina.net/question/253469_51638?f ...

随机推荐

  1. 07-java学习-方法重载-idea集成开发工具学习-项目-模块-包

    方法重载的概念? 方法重载的好处? 集成开发工具idea的学习 下载 安装 设置 建项目 导入项目 建模块 导入模块 建包 复制粘贴包 建类 复制粘贴类 运行 调试

  2. SpringBoot初识

    作用 SpringBoot是为了简化Spring应用的创建.运行.调试.部署等等而出现的,使用它可以专注业务开发,不需要太多的xml的配置. 核心功能 1.内嵌Servlet容器(tomcat.jet ...

  3. Install odoo 11(10) on centos7

    https://www.odoo.com/documentation/11.0/setup/install.html https://nightly.odoo.com/ https://www.odo ...

  4. [转帖]Nginx安装及配置详解 From https://www.cnblogs.com/zhouxinfei/p/7862285.html

    Nginx安装及配置详解   nginx概述 nginx是一款自由的.开源的.高性能的HTTP服务器和反向代理服务器:同时也是一个IMAP.POP3.SMTP代理服务器:nginx可以作为一个HTTP ...

  5. C++与C的区别

    在最开始C++只是C加上了一些面向对象的特性,C++最初的名字为C with Classes.后来C++又提出了一些不同于Class的特性:Exceptions(异常).templates(模板).S ...

  6. Node 开启

    cmd    //进入命令行 D:     //指定磁盘 cd   文件路径   //指定路径 node 文件名.js       //执行文件 增补: Node执行js文件自动嵌套 (functio ...

  7. ubuntu安装命令

    sudo apt-get update  更新源sudo apt-get install package 安装包sudo apt-get remove package 删除包sudo apt-cach ...

  8. github使用指南(2015年3月23日更新了本地创建仓库再推送到remote仓库的使用方法)

    我是通过这个来学习的.个人愚笨,琢磨了半天,终于搞通了,醉了醉了,以前一直使用svn,用git确实有点水土不服.本文以如何使用git为主来展开,不涉及太多理论. git是分布式的版本管理.什么叫分布式 ...

  9. angular4 组件通讯、生命周期

    主要通讯形式 父组件通过属性绑定到子组件,子组件通过事件传递参数到父组件 父组件通过局部变量获取子组件的引用 父组件使用@ViewChild获取子组件的引用 两个不相关联的组件使用中间人模式交互 终极 ...

  10. Java循环中try...finally...遇到continue

    一段很简单的代码,先自己在大脑中给出结果: for (int i = 0; i < 5; i++) { System.out.println("enter: i=" + i) ...