<!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> javascript
<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>html
<input type="checkbox" name="" id="" value="" />
<div class="ww"></div>java
<button type="submit" onclick="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"> node
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');
}jquery
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);
curTbl.style.border="1px";
curTbl.style.backgroundClip="padding-box";
curTbl.style.position="relative";
var oXL;
try{
oXL = new ActiveXObject("Excel.Application"); //建立AX對象excel
}catch(e){
alert("沒法啓動Excel!\n\n若是您確信您的電腦中已經安裝了Excel,"+"那麼請調整IE的安全級別。\n\n具體操做:\n\n"+"工具 → Internet選項 → 安全 → 自定義級別 → 對沒有標記爲安全的ActiveX進行初始化和腳本運行 → 啓用");
return false;
}
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> app