java web将数据导出为Excel格式文件代码片段

这篇文章主要为大家详细介绍了java web将数据导出为Excel格式文件代码片段,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了java web将数据导出为Excel格式文件的具体代码,供大家参考,具体内容如下

1、jsp代码

2、js代码

function getVerExcel() { window.location.href = '/pms/jsp/version/getPrdVerListExcel?page=' + $("#getPage").html() + '&key=' + $("#select").val(); }

3、java代码

/** * * Purpose :将产品版本列表导出为Excel文件 * @param req * 请求 * @param resp * 应答 * @param page * 当前页数 * @param key * 查询条件 * @return */ @RequestMapping("getPrdVerListExcel") public void getExcel(HttpServletRequest req, HttpServletResponse resp, Integer page, String key) { // 设置文件的mime类型 resp.setContentType("application/vnd.ms-excel"); // 得到所有的数据 List verList = prdVersionSer.getAllPrdVersion(key); // 若没有数据,则给用户提示 if (verList.size() == 0) { req.setAttribute("getFileMsg", "没有符合条件的信息!"); req.setAttribute("select", key); try { req.getRequestdispatcher("/jsp/version/ver_list.jsp").forward(req, resp); } catch (Exception e) { e.printstacktrace(); } } else { // 存储编码后的文件名 String name = "name"; // 存储文件名称 String n = ""; if (key != "") { n = verList.get(0).getPrdName() + "的版本列表"; } else { n = "产品版本列表"; } try { name = URLEncoder.encode(n, "utf-8"); } catch (UnsupportedEncodingException e1) { e1.printstacktrace(); } resp.setHeader("content-disposition", "attachment;filename=" + name + ".xls;filename*=utf-8''" + name + ".xls"); System.out.println("key:" + key); // 从session中删除saveExcelMsg属性 req.getSession().removeAttribute("saveExcelMsg"); // 定义一个输出流 ServletoutputStream sos = null; // 创建一个工作簿 hssfWorkbook wb = new hssfWorkbook(); // 创建一个工作表 hssfSheet sheet = null; if (key != "") { sheet = wb.createSheet(verList.get(0).getPrdName() + "的版本信息"); } else { sheet = wb.createSheet("产品版本信息"); } // 返回数据格式对象 // 从格式对象中获取对应日期格式的编号,如果格式不存在,该方法会为它生成新的编号 hssfDataFormat format = wb.createDataFormat(); short dfNum = format.getFormat("yyyy-mm-dd"); // 创建样式对象 CellStyle style = wb.createCellStyle(); // 设置数据格式 style.setDataFormat(dfNum); // 创建第一行(表格标题hssfRow row = sheet.createRow(0); hssfCell cell = row.createCell(0, hssfCell.CELL_TYPE_STRING); if (key != "") { cell.setCellValue(verList.get(0).getPrdName() + "的产品版本列表"); } else { cell.setCellValue("产品版本列表"); } // 创建第二行(表头) row = sheet.createRow(1); cell = row.createCell(0, hssfCell.CELL_TYPE_STRING); cell.setCellValue("序号"); cell = row.createCell(1, hssfCell.CELL_TYPE_STRING); cell.setCellValue("产品名称"); cell = row.createCell(2, hssfCell.CELL_TYPE_STRING); cell.setCellValue("版本号"); cell = row.createCell(3, hssfCell.CELL_TYPE_STRING); cell.setCellValue("发布日期"); cell = row.createCell(4, hssfCell.CELL_TYPE_STRING); cell.setCellValue("版本类型"); cell = row.createCell(5, hssfCell.CELL_TYPE_STRING); cell.setCellValue("版本描述"); int num = 1; // 遍历输出verList中的数据,将其存入Excel中 for (int i = 0; i

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程之家。

相关文章

HashMap是Java中最常用的集合类框架,也是Java语言中非常典型...
在EffectiveJava中的第 36条中建议 用 EnumSet 替代位字段,...
介绍 注解是JDK1.5版本开始引入的一个特性,用于对代码进行说...
介绍 LinkedList同时实现了List接口和Deque接口,也就是说它...
介绍 TreeSet和TreeMap在Java里有着相同的实现,前者仅仅是对...
HashMap为什么线程不安全 put的不安全 由于多线程对HashMap进...