以下是幾種除了直接POST back回後端,匯出Excel的範例
1.
實作一頁面負責從後端Export
Excel
可在其他前端利用iframe內嵌此頁面的方式產出Excel。
function ExportExcel() {
var uri = "Excel.aspx?XX=123&YY=456";
var $iframe = $('<iframe></iframe>');
//指定iframe的src並將其設為隱藏
$iframe.attr("id","FrameExcel");
$iframe.attr("src", uri);
$iframe.css('display', 'none');
$("#<%=SummaryDiv.ClientID%>").append($iframe);
$iframe.remove();
}
可在其他前端利用iframe內嵌此頁面的方式產出Excel。
function ExportExcel() {
var uri = "Excel.aspx?XX=123&YY=456";
var $iframe = $('<iframe></iframe>');
//指定iframe的src並將其設為隱藏
$iframe.attr("id","FrameExcel");
$iframe.attr("src", uri);
$iframe.css('display', 'none');
$("#<%=SummaryDiv.ClientID%>").append($iframe);
$iframe.remove();
}
2.
Javascript 前端匯出Html table 到Excel
è 無法在IE上使用
var tableToExcel = (function () {
var uri = 'data:application/vnd.ms-excel;base64,',
template = '<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40"><head><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name>{worksheet}</x:Name><x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--></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))
}
})()
///BExcel Click事件
$("#<%=BtExcel.ClientID%>").bind("click",
function (e) {
//產出Summary Excel
tableToExcel("<%=tbSummary.ClientID%>", "test");
});
è 無法在IE上使用
var tableToExcel = (function () {
var uri = 'data:application/vnd.ms-excel;base64,',
template = '<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40"><head><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name>{worksheet}</x:Name><x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--></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))
}
})()
///BExcel Click事件
$("#<%=BtExcel.ClientID%>").bind("click",
function (e) {
//產出Summary Excel
tableToExcel("<%=tbSummary.ClientID%>", "test");
});
3.
Javascript 前端匯出Html table 到Excel
è 無法在IE上使用
è 無法在IE上使用
u 用一個div包一個html table
<div id="myDiv" runat="server" visible="true" >
<table id="myTb" runat="server" width="100%" >
<div id="myDiv" runat="server" visible="true" >
<table id="myTb" runat="server" width="100%" >
u Js:
window.open('data:application/vnd.ms-excel,' + $('#<%=Div.ClientID%>').html());
e.preventDefault();
window.open('data:application/vnd.ms-excel,' + $('#<%=Div.ClientID%>').html());
e.preventDefault();
4.
Javascript 前端使用ActiveX的方式
try {
var xls = new ActiveXObject("Excel.Application");
xls.Visible = true;
}
catch (e) {
alert("開啟失敗,請確定你的電腦已經安裝excel,且瀏覽器必須允許ActiveX控件執行");
return;
}
var objTable = document.getElementById($("#<%=tbSummary.ClientID%>").attr("id"));
var xlBook = xls.Workbooks.Add;
var xlsheet = xlBook.Worksheets(1);
for (var i = 0; i < objTable.rows.length; i++)
for (var j = 0; j < objTable.rows[i].cells.length; j++)
xlsheet.Cells(i + 1, j + 1).value = objTable.rows[i].cells[j].innerHTML;
try {
var xls = new ActiveXObject("Excel.Application");
xls.Visible = true;
}
catch (e) {
alert("開啟失敗,請確定你的電腦已經安裝excel,且瀏覽器必須允許ActiveX控件執行");
return;
}
var objTable = document.getElementById($("#<%=tbSummary.ClientID%>").attr("id"));
var xlBook = xls.Workbooks.Add;
var xlsheet = xlBook.Worksheets(1);
for (var i = 0; i < objTable.rows.length; i++)
for (var j = 0; j < objTable.rows[i].cells.length; j++)
xlsheet.Cells(i + 1, j + 1).value = objTable.rows[i].cells[j].innerHTML;
5.
待補…
沒有留言:
張貼留言