javascript实现从Excel文件向Web表单的数据复制
- 摘要:本文介绍利用javascript实现从Excel文件向Web表单的数据复制
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> javascript实现从Excel文件向Web表单的某列多行数据复制 </TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="sharpnessdotnet">
<META NAME="Keywords" CONTENT="javascript,Excel,Web表单,多行复制">
<META NAME="Description" CONTENT="javascript实现从Excel文件向Web表单的某列多行数据复制">
<style type="text/css">
<!--
.div1 { border-top:buttonface 1px solid;border-left:buttonface 1px solid;border-bottom:windowframe 1px solid;border-right:windowframe 1px solid;}
.div2 { border-top:window 1px solid;border-left:window 1px solid;border-bottom:buttonshadow 1px solid;border-right:buttonshadow 1px solid;}
.MouseOver {background-color:highlight;color:highlighttext;font-size: 12px;cursor:hand;font-size: 12px;}
.MouseOut {background-color:buttonface;color:buttontext;font-size: 12px;cursor:default;font-size: 12px;}
-->
</style>
<script language="javascript">
//点击对象
var msgSender = null;
//弹出菜单:本函数来源于网络其他作者
function PopupMouseRightButtonUpMenu()
{
//显示菜单
if(MouseMenu.style.visibility=='visible') MouseMenu.style.visibility='hidden';
//如果是链接、文本区或输入框,则显示IE菜单
if (event.srcElement.tagName=='A' ||
event.srcElement.tagName=='TEXTAREA' ||
event.srcElement.tagName=='INPUT' ||
document.selection.type!='None')
return true;
else
{
//防止菜单出格
if (event.clientX+150 > document.body.clientWidth)
MouseMenu.style.left=event.clientX+document.body.scrollLeft-150;
else
MouseMenu.style.left=event.clientX+document.body.scrollLeft;
if (event.clientY+DivH > document.body.clientHeight)
MouseMenu.style.top=event.clientY+document.body.scrollTop-DivH;
else
MouseMenu.style.top=event.clientY+document.body.scrollTop;
MouseMenu.style.visibility='visible';
//设置点击对象
msgSender = event.srcElement;
}
//禁止IE菜单
return false;
}
//画出菜单但不显示:本函数来源于网络其他作者
function DrawMouseRightButtonUpMenu()
{
DivH =2;
var HrStr='<tr><td align=\"center\" valign=\"middle\" height=\"2\"><TABLE border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"128\" height=\"2\"><tr><td height=\"1\" bgcolor=\"buttonshadow\"><\/td><\/tr><tr><td height=\"1\" bgcolor=\"buttonhighlight\"><\/td><\/tr><\/TABLE><\/td><\/tr>';
var MenuItemStr1='<tr><td align=\"center\" valign=\"middle\" height=\"20\"><TABLE border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"132\"><tr><td valign=\"middle\" height=\"16\" class=\"MouseOut\" onMouseOver=\"this.className=\'MouseOver\'\" onMouseOut=\"this.className=\'MouseOut\'\" onclick=\"'
var MenuItemStr2="<\/td><\/tr><\/TABLE><\/td><\/tr>";
var MenuStr='';
MenuStr+=HrStr;
for(i=0;i<arguments.length;i++)
{
MenuStr+=MenuItemStr1+arguments[i]+MenuItemStr2;
DivH+=20;
}
if(arguments.length>0)
{
MenuStr+=HrStr;
DivH+=2;
}
var aboutMenu=['MouseMenu.style.visibility=\'hidden\';AffixSingleColumn(this);">从Excel单列粘贴']
MenuStr+=HrStr;
for(i=0;i<aboutMenu.length;i++)
{
MenuStr+=MenuItemStr1+aboutMenu[i]+MenuItemStr2;
DivH+=20;
}
var MenuTop = '<DIV id=\"MouseMenu\" class=\"div1\" style=\"position:absolute; left:0px; top:0px; width=150;height='+DivH+'; z-index:1; visibility:hidden;\">\n<TABLE border=\"0\" cellpadding=\"0\" cellspacing=\"0\" class=\"div2\">\n<tr>\n<td bgcolor=\"' + MenuBarColor+ '\" width=\"50\" valign=\"bottom\" align=\"center\" bgcolor=\"buttonface\">\n<\/td>\n<td bgcolor=\"buttonface\">\n<TABLE border=\"0\" cellpadding=\"0\" cellspacing=\"0\">';
var MenuBottom = '<\/TABLE><\/td><\/tr><\/TABLE><\/DIV>';
document.write(MenuTop+MenuStr+MenuBottom);
document.body.oncontextmenu=
new Function('return PopupMouseRightButtonUpMenu();');
document.body.onclick=
new Function('if(event.srcElement.tagName !=\'INPUT\') MouseMenu.style.visibility=\'hidden\'');
document.body.onscroll=new Function('MouseMenu.style.visibility=\'hidden\';');
document.body.onselectstart=new Function('MouseMenu.style.visibility=\'hidden\';');
window.onresizestart=new Function('MouseMenu.style.visibility=\'hidden\';');
}
//作者:sharpnessdotnet
//电邮:sharpnessdotnet@163.com
function AffixSingleColumn(sender)
{
var content = null;
try
{
//从剪贴板获取数据
content = clipboardData.getData('text');
}
catch(e)
{
alert('error:' + e);
}
if(content != null)
{
//将获取的数据按行分割
var valArray = content.split("\n");
//获取点击对象
var columnObj = msgSender;
if(columnObj.tagName == "TD" &&
columnObj.parentElement.tagName == "TR" &&
columnObj.parentElement.rowIndex == 0)
{
//获取点击对象列索引
var columnIndex = columnObj.cellIndex;
var tableObj = document.getElementById('target');
var tbodyObj = columnObj.parentElement.parentElement;
//获取表对象总行数
var rowsCount = tbodyObj.childNodes.length;
//索引需要跳过标题行
for(var index = 0 ;index< rowsCount;index++)
{
//获取当前行
var rowObj = tbodyObj.childNodes[index];
//获取当前列
var columnObj = rowObj.childNodes[columnIndex];
//获取当前输入文本框
var inputObj = columnObj.childNodes[0];
if(inputObj.tagName == "INPUT")
{
//检查该输入框是否声明value属性
if(!inputObj.value)
{
//添加value属性
inputObj.setAttribute("value","");
}
//从剪贴板中对应行赋值
if(valArray[index] != undefined)
inputObj.value = valArray[index];
}
}
}
}
}
</script>
</HEAD>
<BODY>
<table border = "1" id = 'target'>
<tr>
<td>姓名</td>
<td>性别</td>
<td>年龄</td>
<td>效益工资</td>
</tr>
<tr>
<td><input type = "input" size = 10 /></td>
<td><input type = "input" size = 10 /></td>
<td><input type = "input" size = 10 /></td>
<td><input type = "input" size = 10 /></td>
</tr>
<tr>
<td><input type = "input" size = 10 /></td>
<td><input type = "input" size = 10 /></td>
<td><input type = "input" size = 10 /></td>
<td><input type = "input" size = 10 /></td>
</tr>
<tr>
<td><input type = "input" size = 10 /></td>
<td><input type = "input" size = 10 /></td>
<td><input type = "input" size = 10 /></td>
<td><input type = "input" size = 10 /></td>
</tr>
<tr>
<td><input type = "input" size = 10 /></td>
<td><input type = "input" size = 10 /></td>
<td><input type = "input" size = 10 /></td>
<td><input type = "input" size = 10 /></td>
</tr>
<tr>
<td><input type = "input" size = 10 /></td>
<td><input type = "input" size = 10 /></td>
<td><input type = "input" size = 10 /></td>
<td><input type = "input" size = 10 /></td>
</tr>
</table>
<SCRIPT LANGUAGE="JavaScript">
<!--
//本函数来源于网络其他作者
var MenuBarColor ='#6600FF';
DrawMouseRightButtonUpMenu();
//-->
</SCRIPT>
</BODY>
</HTML>
打印
收藏文章
关闭
该文章已有条评论
我要发表评论
热门文章
热门文章附带页
推荐文章