<HTML>
<TITLE>Form Object example</TITLE>
<HEAD>
<script language="javascript">
function getrowcolvalue(str,innText,rownum)
{
var rstr=str;
var p=rstr.indexOf("name=");
var controlName="";
if (p>0)
{
rstr=rstr.substr(p+5);
//alert(rstr+"------");
p=rstr.indexOf(">");
rstr=rstr.substr(0,p);
//alert(rstr+"=========");
var strarr=document.all[rstr];
if (strarr.length>0)//多个相同的名字 by yl
{
rstr=strarr[rownum].value;
}
else
{
rstr=strarr.value;
}
//alert(rstr+"+++++t");
return rstr;
}
return innText;
}
function AutomateExcel(tableid) //by yanleigis email:landgis@126.com
{
var table;
table = document.getElementById(tableid);
var hang= table.rows.length;
var lie = table.rows(0).cells.length;
var oXL = new ActiveXObject("Excel.Application");
oXL.Visible = true;
var oWB = oXL.Workbooks.Add();
var oSheet = oWB.ActiveSheet;
// Add table headers going cell by cell.
for (i=1;i <hang;i++) //第一个不考虑
{
for(j=0;j <lie;j++)
{
var str=table.rows(i).cells(j).innerHTML;
//alert(str);
var innText=table.rows(i).cells(j).innerText;
//str=str.repacle( ,"");
str=getrowcolvalue(str,innText,i);
oSheet.Cells(i,j+1).value = str;
}
}
//oWB.SaveAs("c:/report.xls");
oSheet.printout;
oXL.UserControl = true;
}
function gettbodyValue()
{
alert("-------------------------");
var t= document.all.yltable.children;
for(var i=0; i<t.length; i++)
{
//alert("my:"+t[i].tagName);
if(t[i].tagName=="TBODY")
{
var trs = t[i].children;
for(var j=0;j<trs.length; j++)
{
//alert(trs[j].outerHTML);
}
}
}
}
function delrow1()
{
var oElement=event.srcElement;
while(oElement.tagName!="TR")
{
oElement=oElement.parentElement;
}
var oTBody=oElement.parentElement;
oTBody.style.display="none";
alert(oTBody.id);
//oTBody.removeChild(oElement)
}
//检查必填字段是否填写
function checkFormobj(obj) //form检查
{
alert(obj.name);
try
{
with(obj)
{
for(var i=0;i<length;i++)
{
var eleobj=elements[i];
var ptype=eleobj.type;
var ename=eleobj.name;
var objvalue=eleobj.value;
//alert("ptype:"+ptype+",ename:"+ename+":"+objvalue);
}
}
}
catch (e)
{
alert("错误信息:"+e);
}
//alert("ok_____________");
return true;
}
function insertrow1(tdodyid,yltableid) //增加的一行方法1,采用
{
//alert(yltablename);
var newnode = document.all[tdodyid].cloneNode(true);
//alert("原始:"+newnode.id);
newnode.id="cc";
//gettbodyValue();
//checkFormobj(document.all['ylform']);
//alert(newnode.id);
newnode.style.display="inline";
document.getElementById(yltableid).appendChild(newnode);
}
function deleteallrow(yltableid)
{
//document.getElementById('yltable').removeNode(true); //将所有的含隐含的都删除
var Table=document.getElementById(yltableid);
for(var i=Table.rows.length-1;i>=1;i--) 将第一个保留
{
Table.deleteRow(i);
}
}
function inserttable()
{
var newnode = document.getElementById('yltable').cloneNode(true);
document.getElementById('ylform').appendChild(newnode);
}
</script>
</HEAD>
<BODY>
<form name="ylform" id="ylform">
<center>
<input name="button2" type="button" onClick="insertrow1('yl1','yltable');" value="增加记录">
<input name="button22" type="button" onClick="deleteallrow('yltable');" value="删除所有">
<input name="button22" type="button" onClick="AutomateExcel('yltable');" value="导出excel">
</center>
<table border="1" align="center" width="50%" id="yltable">
<TBODY id=yl1 style="display=none">
<tr>
<td width="30%" height="22"><div align="center">1</div></td>
<td width="40%">
<div align="center">
<input name="textfield" type="text" value="yl">
</div></td>
<td width="30%"><div align="center">
<input name="button3" type="button" onClick="delrow1('yltable')" value="删除1">
</div></td>
</tr>
</TBODY>
</table>
<center>
</center>
</form>
</BODY>
</HTML>