本介绍两种Excle导出方法: JAVA 导出 Excle, JS 导出 Excle
1, js 根据 html 页面的 table > tr > td 标签导出
js代码:
//导出 var idTmr; function getExplorer() { var explorer = window.navigator.userAgent ; //ie if (explorer.indexOf("MSIE") >= 0) { return 'ie'; } //firefox else if (explorer.indexOf("Firefox") >= 0) { return 'Firefox'; } //Chrome else if(explorer.indexOf("Chrome") >= 0){ return 'Chrome'; } //Opera else if(explorer.indexOf("Opera") >= 0){ return 'Opera'; } //Safari else if(explorer.indexOf("Safari") >= 0){ return 'Safari'; } } function exportData(tableid) { if(getExplorer()=='ie') { var curTbl = document.getElementById(tableid); var oXL = new ActiveXObject("Excel.Application"); var oWB = oXL.Workbooks.Add(); var xlsheet = oWB.Worksheets(1); var sel = document.body.createTextRange(); sel.moveToElementText(curTbl); sel.select(); sel.execCommand("Copy"); xlsheet.Paste(); oXL.Visible = true; try { var fname = oXL.Application.GetSaveAsFilename("Excel.xls", "Excel Spreadsheets (*.xls), *.xls"); } catch (e) { print("Nested catch caught " + e); } finally { oWB.SaveAs(fname); oWB.Close(savechanges = false); oXL.Quit(); oXL = null; idTmr = window.setInterval("Cleanup();", 1); } } else { tableToExcel(tableid); } } function Cleanup() { window.clearInterval(idTmr); CollectGarbage(); } var tableToExcel = (function() { var uri = 'data:application/vnd.ms-excel;base64,', template = '<html><head><meta charset="UTF-8"></head><body><table>{table}</table></body></html>', base64 = function(s) { return window.btoa(unescape(encodeURIComponent(s))); }, format = function(s, c) { return s.replace(/{(w+)}/g, function(m, p) { return c[p]; }); }; return function(table, name) { if (!table.nodeType) table = document.getElementById(table); var ctx = {worksheet: name || 'Worksheet', table: table.innerHTML}; window.location.href = uri + base64(format(template, ctx)); }; })();
html 代码:
<input class='btn_04' type='button' id='exportData' value="导出数据" onclick="exportData('tableExcel')"/>
ps:js 导出可兼容主流浏览器,包括360. 缺陷为:只能导出千级别的数据量,亲测15000条数据不能导出
----------------------------------------------------------------------------------
2, java 导出 Excle文件,根据集合遍历生成excle,需要依赖 poi-3.8-20120326.jar 包(或其他版本,请自行下载)
java 代码:
List<ClUserinfo> regUsers = clSecurityService.findClUserinfos(clSecurityForm); List<TempUser> userList = new ArrayList<TempUser>(); for (ClUserinfo userinfo : regUsers) { TempUser user = new TempUser(); user.setUserName(userinfo.getUserName()); user.setUserType(userinfo.getUserType()); user.setCompany(userinfo.getCompany()); user.setKeshi(userinfo.getKeshi()); user.setProvince(userinfo.getProvince()); user.setCreatedDate(userinfo.getCreatedDate()); user.setCreateDateStr(userinfo.getCreatedDate().toString()); userList.add(user); } HSSFWorkbook wb = new HSSFWorkbook(); HSSFSheet sheet = wb.createSheet("注册用户表"); HSSFRow row = sheet.createRow((int) 0); HSSFCellStyle style = wb.createCellStyle(); style.setAlignment(HSSFCellStyle.ALIGN_CENTER); HSSFCell cell = row.createCell((short) 0); cell.setCellValue("姓名"); cell.setCellStyle(style); cell = row.createCell((short) 1); cell.setCellValue("专委会"); cell.setCellStyle(style); cell = row.createCell((short) 2); cell.setCellValue("单位"); cell.setCellStyle(style); cell = row.createCell((short) 3); cell.setCellValue("科室"); cell.setCellStyle(style); cell = row.createCell((short) 4); cell.setCellValue("注册日期"); cell.setCellStyle(style); for (int i = 0; i < userList.size(); i++) { row = sheet.createRow((int) i + 1); TempUser user = (TempUser) userList.get(i); row.createCell((short) 0).setCellValue(user.getUserName()); row.createCell((short) 1).setCellValue(user.getUserType()); row.createCell((short) 2).setCellValue(user.getCompany()); row.createCell((short) 3).setCellValue(user.getKeshi()); row.createCell((short) 4).setCellValue(user.getCreateDateStr()); } try { FileSystemView fsv = FileSystemView.getFileSystemView(); File com=fsv.getHomeDirectory(); //桌面 FileOutputStream fout = new FileOutputStream(com.getPath() + "/注册用户列表-" + new SimpleDateFormat("yyyyMMddhhmmss").format(new Date()) + ".xls"); wb.write(fout); fout.close(); } catch (Exception e) { e.printStackTrace(); }