用javascript将数据导入Excel示例代码

时间:2021-11-30 12:04:34

网上收集的代码

?
1
<input type="button" name="out_excel" onclick="AutomateExcel();" value="导出到excel" class="notPrint">

导出到Excel代码

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<SCRIPT LANGUAGE="javascript">
<!--
function AutomateExcel()
{
// Start Excel and get Application object.
var oXL = new ActiveXObject("Excel.Application");
// Get a new workbook.
var oWB = oXL.Workbooks.Add();
var oSheet = oWB.ActiveSheet;
var table = document.all.data;
var hang = table.rows.length;
 
var lie = table.rows(0).cells.length;
 
// Add table headers going cell by cell.
for (i=0;i<hang;i++)
{
for (j=0;j<lie;j++)
{
//oSheet.Cells(i+1,j+1).Font.Bold = True
//oSheet.Cells(i+1,j+1).Font.Size = 50
//oSheet.Cells(i+1,j+1).Alignment = 2
oSheet.Cells(i+1,j+1).value = table.rows(i).cells(j).innerText;
}
 
}
oXL.Visible = true;
oXL.UserControl = true;
}
//-->
</SCRIPT>
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<table border="1" width="100%" id="data">
<tr>
<td>&nbsp;姓名</td>
<td colspan="2">年龄</td>
<td>出生日期</td>
</tr>
<tr>
<td>6</td>
<td>25</td>
<td colspan="2">8</td>
</tr>
<tr>
<td height="22">1</td>
<td height="22">2</td>
<td colspan="2" height="22">4</td>
</tr>
</table>