<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta charset="UTF-8"> <meta http-equiv="Content-Type" content="text/html;"/> <meta http-equiv="X-UA-Compatible" content="IE=EmulateIE9"/> <title>WEB页面导出为EXCEL文档的方法</title> </head> <body> <table id="tableExcel" width="100%" border="1" cellspacing="0" cellpadding="0"> <tr> <td colspan="5" align="center">WEB页面导出为EXCEL文档的方法</td> </tr> <tr> <td>列标题1</td> <td>列标题2</td> <td>列标题3</td> <td>列标题4</td> <td>列标题5</td> </tr> <tr> <td>aaa</td> <td>bbb</td> <td>ccc</td> <td>ddd</td> <td>eee</td> </tr> <tr> <td>AAA</td> <td>BBB</td> <td>CCC</td> <td>DDD</td> <td>EEE</td> </tr> <tr> <td>FFF</td> <td>GGG</td> <td>HHH</td> <td>III</td> <td>JJJ</td> </tr> </table><table id="tableExcel1" width="100%" border="1" cellspacing="0" cellpadding="0">
<tr> <td colspan="4" align="center">妞妞</td> </tr> <tr> <td>列标题1</td> <td>列标题2</td> <td>列标题3</td> <td>列标题5</td> </tr> <tr> <td>AAA</td> <td>BBB</td> <td>CCC</td> <td>EEE</td> </tr> <tr> <td>FFF</td> <td>GGG</td> <td>HHH</td> <td>JJJ</td> </tr> </table><input type="checkbox" name="" id="" value="" />
<div class="ww"></div> <button type="submit" οnclick="exec()" class="btn btn-primary" style="position:relative;width: 100px;height: 26px;"> <a id="dlink" href="" style="display: none;"></a><span style="position: absolute; left: 40px;top:5px;">导出</span></button> <script src="jquery-1.8.3.min.js"></script> <SCRIPT LANGUAGE="javascript">function exec(){
var tb=$("#tableExcel").html(); var tb1=$("#tableExcel1").html(); var tabhtml='<table id="tableExcel2" width="100%" border="1" cellspacing="0" cellpadding="0" style="display:block">'+tb+'<tr></tr>'+tb1+'</table>'; var cells1=$($("#tableExcel").find("tr")[1]).find("td").length; var cells2=$($("#tableExcel1").find("tr")[1]).find("td").length; $(".ww").html(tabhtml); exportExcel('tableExcel2','name','WEB页面导出为EXCEL文档的方法.xls'); } var idTmr; function getExplorer() { var explorer = window.navigator.userAgent ; //ie if (explorer.indexOf("MSIE") >= 0|| (explorer.indexOf("Windows NT 6.1;") >= 0 && explorer.indexOf("Trident/7.0;") >= 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 exportExcel(tableid,name,filename) { // alert(getExplorer()); if(getExplorer()=='ie') {var curTbl = document.getElementById(tableid);
var oXL = new ActiveXObject("Excel.Application"); var oWB = oXL.Workbooks.Add(); var oSheet = oWB.ActiveSheet; var Lenr = curTbl.rows.length; for (i = 0; i < Lenr; i++){ var Lenc = curTbl.rows(i).cells.length; for (j = 0; j < Lenc; j++){ oSheet.Cells(i + 1, j + 1).value = curTbl.rows(i).cells(j).innerText; } } oXL.Visible = true; }else{ tableToExcel(tableid,name,filename) } } 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 border="1">{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,filename) { if (!table.nodeType) table = document.getElementById(table) var ctx = {worksheet: name || 'Worksheet', table: table.innerHTML} document.getElementById("dlink").href = uri + base64(format(template, ctx)); document.getElementById("dlink").download = filename; document.getElementById("dlink").click(); } })()</SCRIPT>
</body> </html>