[Java教程]Springmvc和poi3.9导出excel并弹出下载框

0 2013-09-06 20:00:07

Springmvc 和 poi3.9 用java程序从数据库导出数据到excel(在博客园的第一篇原创博客)@RequestMapping(value = "/importexcel.htm", method = RequestMethod.GET) public ModelAndView _importExcel(HttpServletRequest request, HttpServletResponse response, Integer cId) throws Exception { // 获得要导出的数据集 List> 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; }

本文网址:http://www.shaoqun.com/a/70018.html

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们:admin@shaoqun.com。

Spring

0

java导出excel弹出下载框_[Java教程]Springmvc和poi3.9导出excel并弹出下载框相关推荐

  1. python迅雷远程下载页面_【教程】Chrome浏览器添加迅雷下载支持

    2019年4月28日更新: 目前官方最新版本的迅雷X已经实现自动安装Chrome扩展,无需手动安装. 请仍在使用老版本迅雷的雷友升级到官方最新版本的迅雷X. 以下内容已经过期 最近一段时间,Googl ...

  2. java百分号的用法,Java String.format()方法中使用百分号'%'_栗子教程

    下面这个例子演示了如何在String.format方法中显示一个百分号. 像下面这样直接调用String.format("%d%", 100)来显示百分号,将会抛出java.uti ...

  3. 在reader中勾选pdf复选框_绝对可勾选的在WORD 2003中加入复选框的方法

    绝对可勾选的在 word 2003 中加入复选框的方法 方法一: 要在 word 2003 中加入复选框,最好是使用"控件工具箱"来完成. 具体方法是: 打开 Word ,依次点击 ...

  4. java可以写a手机app吗_‎JAVA程序员自学编程 a l’App Store

    *****Java程序员的给力助手***** -----本应用是一款java程序员自学参考手册,内容涵盖了所有java自学入门教程.高级教程以及数十道java经典面试题及代码答案,十分适合java开发 ...

  5. java 多选列表框_快逸报表:填报中的下拉多选列表框

    在快逸报表应用的填报页面中,下拉框是最常见的了.下拉选择框可以简化用户的输入,可以避免用户的错误录入,还可以轻松实现中文与代码的映射关系,可谓用途多多. 可是随着业务的越来越复杂,各种奇怪的需求均可能 ...

  6. java开发可重用代码包工具包_[Java教程]彻底搞懂Java开发工具包(JDK)安装及环境变量配置...

    [Java教程]彻底搞懂Java开发工具包(JDK)安装及环境变量配置 0 2021-01-04 04:00:04 安装并配置JDK环境变量,不但要知道怎样做,也要知道为什么这样做,知其然知其所以然. ...

  7. excel小写转大写公式_喂!这边居然有演示的Excel快捷键!

    一颗想偷懒的心使得人类的向前迈了一大步,无数快捷便利的东西应运而生.Excel快捷键的使用逐渐的熟练起来就可以使工作更加便利,相信快捷键里最熟悉的恐怕就是Ctrl+C和Ctrl+V,这个小学生都知道的 ...

  8. excel导出多重表头utils_Java中注解学习系列教程-4 使用自定义注解实现excel导出...

    本文是<Java中注解学习系列教程>第四篇文章也是小案例文章. 自定义注解小案例是:使用自定义注解实现excel导出. Excel导出分析: ​ 有表头.数据值.一般第一行是表头,从第二行 ...

  9. java实现html页面转pdf解决方案_[Java教程]纯js实现html转pdf

    [Java教程]纯js实现html转pdf 0 2017-02-16 00:00:30 项目开发中遇到了一个变态需求,需要把一整个页面导出为pdf格式,而且要保留页面上的所有的表格.svg图片和样式. ...

  10. 易语言 图片插入超级列表框_新手教程!如何在PDF文件中插入图像

    每当在工作中收到一份PDF文件时,只要没有图片,阅读起来就会非常的枯燥无味,长时间的阅读还会带来睡意,而图片的使用能够更好的提高文件的阅读性.所以图片与文字的相配才是完整的一份PDF文件.但是PDF文 ...

最新文章

  1. mysql 查询临时表列名_为什么mysql会缓存被删除的临时表的列名?
  2. matlab画坐标系,Matlab绘制图形坐标轴操作汇总 | 望天博客
  3. tensorflow保存内容到相对文件路径下_如何添加自定义文件系统插件
  4. GDI+入门(5、在GDI+中绘制带动画效果的图片)
  5. esp32 arduion 加速下载json
  6. 【数据库课程设计】金融数据库设计与实现
  7. AIML相关内容学习整理
  8. 网上交易安全-使用JAVA调用U盾进行客户认证
  9. 论文阅读——Efficient and Robust Feature Selection via Joint L2,1-Norms Minimization
  10. 用C++编写一个走迷宫游戏(1.0版)
  11. PDF怎么裁剪页面,PDF裁剪页面的操作步骤
  12. 盛世昊通全新升级,引领智慧新经济
  13. 如何在VR全景中嵌入AI数字人功能?打造云端体验感
  14. python-pptx替换ppt模板的文本
  15. 欧姆龙SYSMAC STUDIO如何与基恩士DL-EP1进行EIP通信
  16. 论文阅读《Direct Sparse Odometry》1
  17. java狗具有特别的接飞盘的方法_java第七章 多态 课堂笔记/作业
  18. 电商秒杀系统设计分析
  19. JavaScript的真值Truthy
  20. 如何翻滚截屏_电脑上如何实现滚动截图?用这款工具即可轻松实现!

热门文章

  1. 美联储历次加息周期及结果
  2. 神经网络和有限元方法
  3. php元素周期表,元素周期表 - 理视天下 - 简单学习网论坛_中高考学习交流论坛_中学生学习论坛 - Powered by phpwind...
  4. java 浮点型_java 基础 浮点类型
  5. 软件测试工程师,不只是你眼中的点点点
  6. 王家林大咖清华新书预发布:《企业级AI技术内幕:深度学习框架开发+机器学习案例+Alluxio解密》之盘古人工智能框架多层次神经网络的实现
  7. 疫情期间华为面试总结
  8. Android App 启动时显示正在加载图片(讲解+源码)
  9. 贪心算法题目 推销员
  10. Qt 小例子学习14 - 动态滚动QScrollArea