1
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
2
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
3
<html xmlns="http://www.w3.org/1999/xhtml">
4
<head>
5
<meta http-equiv="Content-Type" content="text/html; charset=gb2312"/>
6
<title>WEB頁面導出為EXCEL文檔的方法</title>
7
</head>
8
<body>
9
<table id="tableExcel" width="100%" border="1" cellspacing="0" cellpadding="0">
10
<tr>
11
<td colspan="5" align="center">WEB頁面導出為EXCEL文檔的方法</td>
12
</tr>
13
<tr>
14
<td>列標題1</td>
15
<td>列標題2</td>
16
<td>列標題3</td>
17
<td>列標題4</td>
18
<td>列標題5</td>
19
</tr>
20
<tr>
21
<td>aaa</td>
22
<td>bbb</td>
23
<td>ccc</td>
24
<td>ddd</td>
25
<td>eee</td>
26
</tr>
27
<tr>
28
<td>AAA</td>
29
<td>BBB</td>
30
<td>CCC</td>
31
<td>DDD</td>
32
<td>EEE</td>
33
</tr>
34
<tr>
35
<td>FFF</td>
36
<td>GGG</td>
37
<td>HHH</td>
38
<td>III</td>
39
<td>JJJ</td>
40
</tr>
41
</table>
42
<input type="button" onclick="javascript:method1('tableExcel');" value="第一種方法導入到EXCEL">
43
<input type="button" onclick="javascript:method2('tableExcel');" value="第二種方法導入到EXCEL">
44
<input type="button" onclick="javascript:getXlsFromTbl('tableExcel',null);" value="第三種方法導入到EXCEL">
45![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
<SCRIPT LANGUAGE="javascript">
46![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
function method1(tableid)
{//整個表格拷貝到EXCEL中
47
var curTbl = document.getElementById(tableid);
48
var oXL = new ActiveXObject("Excel.Application");
49
//創(chuàng)建AX對象excel
50
var oWB = oXL.Workbooks.Add();
51
//獲取workbook對象
52
var oSheet = oWB.ActiveSheet;
53
//激活當前sheet
54
var sel = document.body.createTextRange();
55
sel.moveToElementText(curTbl);
56
//把表格中的內容移到TextRange中
57
sel.select();
58
//全選TextRange中內容
59
sel.execCommand("Copy");
60
//復制TextRange中內容
61
oSheet.Paste();
62
//粘貼到活動的EXCEL中
63
oXL.Visible = true;
64
//設置excel可見屬性
65
}
66
function method2(tableid) //讀取表格中每個單元到EXCEL中
67![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
{
68
var curTbl = document.getElementById(tableid);
69
var oXL = new ActiveXObject("Excel.Application");
70
//創(chuàng)建AX對象excel
71
var oWB = oXL.Workbooks.Add();
72
//獲取workbook對象
73
var oSheet = oWB.ActiveSheet;
74
//激活當前sheet
75
var Lenr = curTbl.rows.length;
76
//取得表格行數
77
for (i = 0; i < Lenr; i++)
78![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
79
var Lenc = curTbl.rows(i).cells.length;
80
//取得每行的列數
81
for (j = 0; j < Lenc; j++)
82![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
83
oSheet.Cells(i + 1, j + 1).value = curTbl.rows(i).cells(j).innerText;
84
//賦值
85
}
86
}
87
oXL.Visible = true;
88
//設置excel可見屬性
89
}
90![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
function getXlsFromTbl(inTblId, inWindow)
{
91![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
try
{
92
var allStr = "";
93
var curStr = "";
94
//alert("getXlsFromTbl");
95![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if (inTblId != null && inTblId != "" && inTblId != "null")
{
96
curStr = getTblData(inTblId, inWindow);
97
}
98![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if (curStr != null)
{
99
allStr += curStr;
100
}
101![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
else
{
102
alert("你要導出的表不存在!");
103
return;
104
}
105
var fileName = getExcelFileName();
106
doFileExport(fileName, allStr);
107
}
108![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
catch(e)
{
109
alert("導出發(fā)生異常:" + e.name + "->" + e.description + "!");
110
}
111
}
112![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
function getTblData(inTbl, inWindow)
{
113
var rows = 0;
114
//alert("getTblData is " + inWindow);
115
var tblDocument = document;
116![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if (!!inWindow && inWindow != "")
{
117![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if (!document.all(inWindow))
{
118
return null;
119
}
120![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
else
{
121
tblDocument = eval(inWindow).document;
122
}
123
}
124
var curTbl = tblDocument.getElementById(inTbl);
125
var outStr = "";
126![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if (curTbl != null)
{
127![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
for (var j = 0; j < curTbl.rows.length; j++)
{
128
//alert("j is " + j);
129![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
for (var i = 0; i < curTbl.rows[j].cells.length; i++)
{
130
//alert("i is " + i);
131![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if (i == 0 && rows > 0)
{
132
outStr += " \t";
133
rows -= 1;
134
}
135
outStr += curTbl.rows[j].cells[i].innerText + "\t";
136![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if (curTbl.rows[j].cells[i].colSpan > 1)
{
137![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
for (var k = 0; k < curTbl.rows[j].cells[i].colSpan - 1; k++)
{
138
outStr += " \t";
139
}
140
}
141![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if (i == 0)
{
142![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if (rows == 0 && curTbl.rows[j].cells[i].rowSpan > 1)
{
143
rows = curTbl.rows[j].cells[i].rowSpan - 1;
144
}
145
}
146
}
147
outStr += "\r\n";
148
}
149
}
150![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
else
{
151
outStr = null;
152
alert(inTbl + "不存在!");
153
}
154
return outStr;
155
}
156![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
function getExcelFileName()
{
157
var d = new Date();
158
var curYear = d.getYear();
159
var curMonth = "" + (d.getMonth() + 1);
160
var curDate = "" + d.getDate();
161
var curHour = "" + d.getHours();
162
var curMinute = "" + d.getMinutes();
163
var curSecond = "" + d.getSeconds();
164![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if (curMonth.length == 1)
{
165
curMonth = "0" + curMonth;
166
}
167![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if (curDate.length == 1)
{
168
curDate = "0" + curDate;
169
}
170![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if (curHour.length == 1)
{
171
curHour = "0" + curHour;
172
}
173![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if (curMinute.length == 1)
{
174
curMinute = "0" + curMinute;
175
}
176![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if (curSecond.length == 1)
{
177
curSecond = "0" + curSecond;
178
}
179
var fileName = "leo_zhang" + "_" + curYear + curMonth + curDate + "_"
180
+ curHour + curMinute + curSecond + ".csv";
181
//alert(fileName);
182
return fileName;
183
}
184![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
function doFileExport(inName, inStr)
{
185
var xlsWin = null;
186![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if (!!document.all("glbHideFrm"))
{
187
xlsWin = glbHideFrm;
188
}
189![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
else
{
190
var width = 6;
191
var height = 4;
192
var openPara = "left=" + (window.screen.width / 2 - width / 2)
193
+ ",top=" + (window.screen.height / 2 - height / 2)
194
+ ",scrollbars=no,width=" + width + ",height=" + height;
195
xlsWin = window.open("", "_blank", openPara);
196
}
197
xlsWin.document.write(inStr);
198
xlsWin.document.close();
199
xlsWin.document.execCommand('Saveas', true, inName);
200
xlsWin.close();
201
}
202
</SCRIPT>
203
</body>
204
</html>
該文章在 2010/8/18 0:00:09 編輯過