日期:2014-05-16  浏览次数:20413 次

纯js不经后台导出excel实例

---------------------纯js不经后台导出excel实例 --------------------


一:代码如下

<HTML > 
<HEAD > 
<TITLE >js导出excel </TITLE > 
<SCRIPT LANGUAGE="javascript" > 
function AutomateExcel() { 
	var oXL = new ActiveXObject("Excel.Application"); //创建应该对象 
	var oWB = oXL.Workbooks.Add();//新建一个Excel工作簿 
	var oSheet = oWB.ActiveSheet;//指定要写入内容的工作表为活动工作表 
	var table = document.all.data;//指定要写入的数据源的id 
	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++){//在Excel中写行 
		for (j=0;j <lie;j++){//在Excel中写列 
			//定义格式 
			oSheet.Cells(i+1,j+1).Font.Bold = true;//加粗 
			oSheet.Cells(i+1,j+1).Font.Size = 10;//字体大小 
			if(table.rows(i).cells(j).innerHTML.toLowerCase().indexOf( " <img ")!=-1){//如果其HTML代码包括 <img 
				oSheet.Cells(i+1,j+1).Select();//选中Excel中的单元格 
				oSheet.Pictures.Insert(table.rows(i).cells(j).getElementsByTagName( "img ")[0].src);//插入图片 
			}else{ 
				oSheet.Cells(i+1,j+1).value = table.rows(i).cells(j).innerText;//向单元格写入值 
			} 
		} 
	} 
	oXL.Visible = true; 
	oXL.UserControl = true; 
} 
</script>
</HEAD > 
<BODY > 
<table border="0" width="96%" cellspacing="0" cellpadding="0" align="center" class="Noprint">
<tr>
<td width="50%" height="13">·营运商信息管理</td>
<form method="post" action="?mydo=search" name="Form1">
<td width="50%" height="13" align="right">信息主题:<input type="text" name="KeyWord" size="20" value="" class="face">
<select size="1" name="types">
<option value="1" selected>编号</option>
<option value="2">单位名称</option>
<option value="3">单位地址</option>
<option value="4">法定代表人</option>
<option value="5">管理人员</option> 
<option value="6">联系电话</option> 
</select> <input type="submit" name="Submit" value="搜" class="button"></td>
</form>
</tr>
</table>
<div align="center">

<table width="98%" border=1 cellpadding=0 bordercolor="#799AE1" class=tableBorder id="data" >
<tbody>
<tr> 
<th height=21 width="87">编号</th>
<th width="87">单位名称</th>
<th width="301">地址</th>
<th height=21 width="121">法定代表人</th>
<th height=21 width="92">管理人员</th>
<th height=21 width="119">联系电话</th>
</tr> 
<tr> 
<td height=21 align="left" class=forumRow sytle="line-height:150%">50011500006</td>
<td height=21 align="left" class=forumRow sytle="line-height:150%">移动</td>
<td height=21 align="left" class=forumRow sytle="line-height:150%">http://www.52hai.com/2005</td>
<td class=forumRow height=21 sytle="line-height:150%" align="left">某某1</td>
<td class=forumRow height=21 sytle="line-height:150%" align="left">某某</td>
<td class=forumRow height=21 sytle="line-height:150%" align="left">25254255</td>
</tr>

<tr> 
<td height=21 align="left" class=forumRow sytle="line-height:150%">50011500005</td>
<td height=21 align="left" class=forumRow sytle="line-height:150%">铁通</td>
<td h