您的位置:首页 > Web前端 > JavaScript

js导出Excel

2009-11-08 20:56 316 查看
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>将Web页面中的数据导出到Excel</title>
<link href="CSS/style.css" mce_href="CSS/style.css" rel="stylesheet">
</head>

<body>

<form name="form1" method="post" action="">
<table width="540" height="343" border="0" align="center" cellspacing="0" background="main.gif" >
<tr align="center">
<td width="42" height="56" align="right" valign="bottom"><img src="add.gif" mce_src="add.gif" width="20" height="18"></td>
<td width="243" height="56" align="left" valign="bottom" id="doctitle"> 2007年1月份工资报表</td>
<td width="249" align="left" valign="bottom"> </td>
</tr>
<tr align="center">
<td height="24"  colspan="3"> </td>
</tr>
<tr align="center">
<td colspan="3" valign="top">
<table width="90%" border="0" cellspacing="1" bgcolor="#000000" id="wage">
<tr>
<td width="79" height="27" align="center" bgcolor="#efefef">员工名称</td>
<td width="69" align="center" bgcolor="#efefef">奖金</td>
<td width="104" align="center" bgcolor="#efefef">基本工资</td>
<td width="63" align="center" bgcolor="#efefef">津贴</td>
<td width="57" align="center" bgcolor="#efefef">扣除</td>
<td width="93" align="center" bgcolor="#efefef">实发工资</td>
</tr>
<tr>
<td width="79" height="27" align="center" bgcolor="#FFFFFF">wgh</td>
<td width="69" align="center" bgcolor="#FFFFFF">200</td>
<td width="104" align="center" bgcolor="#FFFFFF">1800</td>
<td width="63" align="center" bgcolor="#FFFFFF">300</td>
<td width="57" align="center" bgcolor="#FFFFFF">0</td>
<td width="93" align="center" bgcolor="#FFFFFF">2300</td>
</tr>
<tr>
<td width="79" height="27" align="center" bgcolor="#FFFFFF">sk</td>
<td width="69" align="center" bgcolor="#FFFFFF">100</td>
<td width="104" align="center" bgcolor="#FFFFFF">2100</td>
<td width="63" align="center" bgcolor="#FFFFFF">210</td>
<td width="57" align="center" bgcolor="#FFFFFF">5</td>
<td width="93" align="center" bgcolor="#FFFFFF">2405</td>
</tr>
<tr>
<td width="79" height="27" align="center" bgcolor="#FFFFFF">春</td>
<td width="69" align="center" bgcolor="#FFFFFF">70</td>
<td width="104" align="center" bgcolor="#FFFFFF">1700</td>
<td width="63" align="center" bgcolor="#FFFFFF">100</td>
<td width="57" align="center" bgcolor="#FFFFFF">10</td>
<td width="93" align="center" bgcolor="#FFFFFF">1860</td>
</tr>
<tr>
<td width="79" height="27" align="center" bgcolor="#FFFFFF">夏</td>
<td width="69" align="center" bgcolor="#FFFFFF">100</td>
<td width="104" align="center" bgcolor="#FFFFFF">1900</td>
<td width="63" align="center" bgcolor="#FFFFFF">100</td>
<td width="57" align="center" bgcolor="#FFFFFF">20</td>
<td width="93" align="center" bgcolor="#FFFFFF">2080</td>
</tr>
<tr>
<td width="79" height="27" align="center" bgcolor="#FFFFFF">秋</td>
<td width="69" align="center" bgcolor="#FFFFFF">150</td>
<td width="104" align="center" bgcolor="#FFFFFF">1600</td>
<td width="63" align="center" bgcolor="#FFFFFF">100</td>
<td width="57" align="center" bgcolor="#FFFFFF">0</td>
<td width="93" align="center" bgcolor="#FFFFFF">1850</td>
</tr>
<tr>
<td width="79" height="27" align="center" bgcolor="#FFFFFF">冬</td>
<td width="69" align="center" bgcolor="#FFFFFF">200</td>
<td width="104" align="center" bgcolor="#FFFFFF">1500</td>
<td width="63" align="center" bgcolor="#FFFFFF">100</td>
<td width="57" align="center" bgcolor="#FFFFFF">0</td>
<td width="93" align="center" bgcolor="#FFFFFF">1800</td>
</tr>
</table>
<p align="center"><a href="#" mce_href="#" onClick="outExcel();">导出到Excel</a></p>
</td>
</tr>
</table>
</form>
</body>
</html>
<mce:script language="javascript"><!--
function outExcel(){
var table=document.all.wage;
row=table.rows.length;
column=table.rows(1).cells.length;
var excelapp=new ActiveXObject("Excel.Application");
excelapp.visible=true;
objBook=excelapp.Workbooks.Add(); //添加新的工作簿
var objSheet = objBook.ActiveSheet;

title=objSheet.Range("D1").MergeArea;  //合并单元格
title.Cells(1,0).Value =doctitle.innerHTML.replace(" ","");  //输出标题
title.Cells(1,1).Font.Size =16;
for(i=1;i<row+1;i++){
for(j=0;j<column;j++){
objSheet.Cells(i+1,j+1).value=table.rows(i-1).cells(j).innerHTML.replace(" ","");
}
}
excelapp.UserControl = true;
}
// --></mce:script>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: