java poi生成excel(个人例子js-jsp-java)
js代码:
function exportRepQudl() { if (confirm("导出输出页面内容?")) { var id = $("input[name='id']").val(); var lx = $("input[name='lx']").val(); var rq = $("input[name='rq']").val(); var url = __ctx + '/zjdlbb/zjdlbb/zjdlbb/exportExcelQudl.ht?id=' + id + "&lx=" + lx + "&rq=" + rq; var userAgent = navigator.userAgent; //取得浏览器的userAgent字符串 if (userAgent.indexOf("compatible") > -1 && userAgent.indexOf("MSIE") > -1) {//判断是否IE浏览器 window.location.href(url); } else { window.open(url, "导出报表"); } } }
jsp代码:
<a class="myLinkA" id="exportRepQudl" onclick="exportRepQudl()"><span></span>导出</a>
java代码:
@RequestMapping({ "exportExcelQudl" }) @Action(description = "导出excel") public void exportExcelQudl(HttpServletRequest request, HttpServletResponse response) throws Exception { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); // 获取请求参数的各种处理 String lx = request.getParameter("lx"); String rq = request.getParameter("rq"); String rqTime = ""; if (StringUtil.isEmpty(rq)) { rq = sdf.format(new Date()); rqTime = sdf.format(new Date()) + " 22:00:00"; } else { rqTime = rq + " 22:00:00"; } // 取得各种需要导出的数据 Map<String, String> mapXzz = new HashMap<String, String>(); mapXzz.put("rq", rq); mapXzz.put("rqTime", rqTime); List<Zjdlbbxzb> listXzz = zjdlbbxzbService.getQqdl(mapXzz); // 第一步、设置文件名,类型+日期+后缀.xls String fileName = lx + rq + ".xls"; ; // 第二步,创建excel HSSFWorkbook wb = new HSSFWorkbook(); // 第三步、创建excel sheet HSSFSheet sheet = wb.createSheet(); // 第四步、设置数据 // 添加行的指标 int flagIndex = 0; // 第一行操作 HSSFRow row0 = sheet.createRow(flagIndex++); row0.setHeightInPoints((short) 20); // 添加合并区域(合并第三到第五列) sheet.addMergedRegion(new CellRangeAddress(0, 0, 2, 4)); HSSFCell titleCell = row0.createCell(2); titleCell.setCellValue("" + rq); // 标题样式 HSSFCellStyle cellStyleTitle = wb.createCellStyle(); cellStyleTitle.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 居中 HSSFFont font = wb.createFont(); font.setFontHeightInPoints((short) 14); cellStyleTitle.setFont(font); titleCell.setCellStyle(cellStyleTitle); // 第二行操作 List<String> listTitle = zjdlbbService.getQutlTitle(); HSSFRow row1 = sheet.createRow(flagIndex++); // 样式 HSSFCellStyle cellStyle = ZjdlbbUtil.getHSSFCellStyle(wb); for (int i = 0; i < listTitle.size(); i++) { HSSFCell title = row1.createCell(i); title.setCellValue(listTitle.get(i)); title.setCellStyle(cellStyle); // 设置每一列的宽度 sheet.setColumnWidth(i, 5000); } // 其他行操作 for (int i = 0; i < listXzz.size(); i++) { HSSFRow row = sheet.createRow(flagIndex++); HSSFCell qy = row.createCell(0); HSSFCell ygdl = row.createCell(1); HSSFCell wgdl = row.createCell(2); HSSFCell zgfh = row.createCell(3); HSSFCell pjfh = row.createCell(4); HSSFCell fhl = row.createCell(5); HSSFCell glys = row.createCell(6); Zjdlbbxzb entity = listXzz.get(i); qy.setCellValue(entity.getLx() == null ? "" : entity.getLx()); ygdl.setCellValue(entity.getYgzdl() == null ? 0d : entity.getYgzdl()); wgdl.setCellValue(entity.getWgzdl() == null ? 0d : entity.getWgzdl()); zgfh.setCellValue(entity.getZgfh() == null ? 0d : entity.getZgfh()); pjfh.setCellValue(entity.getPjfh() == null ? 0d : entity.getPjfh()); fhl.setCellValue(entity.getFhl() == null ? 0d : entity.getFhl()); glys.setCellValue(entity.getGlys() == null ? 0d : entity.getGlys()); } response.setContentType("application/octet-stream;charset=UTF-8"); response.setHeader("Content-Type", "application/vnd.ms-excel"); response.setHeader("Content-Disposition", "attachment;filename=" + new String(fileName.getBytes("GB2312"), "8859_1")); response.addHeader("Pargam", "no-cache"); response.addHeader("Cache-Control", "no-cache"); OutputStream out = response.getOutputStream(); wb.write(out); out.flush(); out.close(); }