首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > JavaScript >

怎么拆分剪贴板中复制的Excel数据到数组,并赋值到下方的table中

2012-02-20 
如何拆分剪贴板中复制的Excel数据到数组,并赋值到下方的table中?scriptfunctionPaste(){varClipboardTex

如何拆分剪贴板中复制的Excel数据到数组,并赋值到下方的table中?
<script>
        function   Paste(){
                var   ClipboardText=window.clipboardData.getData( 'Text ');                
                var   ClipboardText=ClipboardText.replace(/[\r\n]/g,   ", ");
                var   Rows=ClipboardText.split( ", ");
                for(var   iRow=0;iRow <Rows.length;iRow++)   {
                        var   Cells=Rows[iRow].split( '\t ');
                        for(var   iCol=0;iCol <Cells.length;iCol++)
                                alert(Cells[iCol]);
                }
        }
</script>

<input   id= "Test "> </input> <input   type= "button "   value= "Paste "   onclick= "Paste(); "> </input>
<table>
<tr> <td> <input> </input> <input> </input> </td> </tr>
<tr> <td> <input> </input> <input> </input> </td> </tr>
</table>


[解决办法]
<html>
<head>
<title> </title>
<script language= "JavaScript ">
function Paste()
{
var tab = window.document.getElementById( "TableExcel ");
alert(window.clipboardData.getData( 'Text '));
var exRows = window.clipboardData.getData( 'Text ').split( "\r ");
for(var i = 0; i < exRows.length; i++)
{
if(i == tab.rows.length)
{
break;
}
alert(i)
var row = tab.rows[i];
var exCells = exRows[i].split( "\t ");
for(var j = 0; j < exCells.length; j++)
{
if(j == row.cells.length)
{
continue;
}
row.cells[j].childNodes[0].value = exCells[j];
}
}
}

</script>
</head>
<body>
<input id= "paset " type= "button " value= "Paste " onclick= "Paste(); "> </input>
<table id= "TableExcel ">
<tr> <td> <input> </input> </td> <td> <input> </input> </td> </tr>
<tr> <td> <input> </input> </td> <td> <input> </input> </td> </tr>
</table>
</body>
</html>


IE6.0中测试通过
[解决办法]
<html>
<head>
<title> </title>
<script language= "JavaScript ">
function Paste(){
var tab = window.document.getElementById( "TableExcel ");
while(tab.rows.length > 0){
tab.deleteRow(0);
}
var exRows = window.clipboardData.getData( 'Text ').split( "\r ");


for(var i = 0; i < exRows.length; i++){
var trTmp = tab.insertRow(i);
var exCells = exRows[i].split( "\t ");
for(var j = 0; j < exCells.length; j++){
var tdTmp = trTmp.insertCell(j);
tdTmp.innerHTML = ' <td align= "center "> ' + exCells[j] + ' </td> '
}
}
}

</script>
</head>
<body>
<input id= "paset " type= "button " value= "Paste " onclick= "Paste(); "> </input>
<table id= "TableExcel " border= "0 " bgcolor= '#E4E4E4 ' cellspacing= "3 " cellpadding= "3 " align= "center "> </table>
</body>
</html>

IE6.0中测试通过
方法比0009(夏天以南)的优化一点,表格的单元格是动态的。

热点排行