蘭庭論壇
標題:
ASP匯出EXCEL語法
[打印本頁]
作者:
李亮寬
時間:
2013-9-18 12:32
標題:
ASP匯出EXCEL語法
以下是在client端將網頁表格中的資料匯出至excel的示範
<html>
< body>
< table border="1" cellpadding="0" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" width="100%" id="table1">
<tr>
<td width="25%">學號</td>
<td width="25%">姓名</td>
<td width="25%">科目</td>
<td width="25%">成績</td>
</tr>
<tr>
<td width="25%">0001</td>
<td width="25%">王小明</td>
<td width="25%">國語</td>
<td width="25%">90</td>
</tr>
<tr>
<td width="25%">0002</td>
<td width="25%">李大名</td>
<td width="25%">國語</td>
<td width="25%">80</td>
</tr>
<tr>
<td width="25%">0003</td>
<td width="25%">趙中明</td>
<td width="25%">國語</td>
<td width="25%">70</td>
</tr>
< /table>
< form name="f1">
<input type="button" value="匯出至excel" name="B1">
< /form>
< /body>
< /html>
< script language="JavaScript">
function saveToExcel(str) {
try {
var xls = new ActiveXObject("Excel.Application");
xls.Visible = true;
}
catch(e) {
alert("開啟失敗,請確定你的電腦已經安裝excel,且瀏覽器必須允許ActiveX控件執行");
return;
}
var objTable = document.getElementById(str);
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
.cells.length;j++)
xlsheet.Cells(i+1,j+1).value = objTable.rows
.cells[j].innerHTML;
}
< /script>
或把button改成如下
<input type="button" name="SaveAs" value="轉存excel檔">
然後在header中加上以下的javascript
< SCRIPT LANGUAGE="JavaScript">
< !--
function executeCommand(strFname)
{
var testCommand = document.execCommand("SaveAs", true, strFname);
}
//-->
< /SCRIPT>
歡迎光臨 蘭庭論壇 (http://120.101.203.2/)
Powered by Discuz! X3.2