代碼如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Ctrl + V</title>
<script type="text/javascript" src="jq.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$(document.body).bind({
paste: function(e) {
if (window.clipboardData) {
return ;
}
var clipboardData = e.originalEvent.clipboardData;
processData(clipboardData);
}
});
document.onkeyup = function(e) {
if (window.clipboardData) {
e = window.event || e;
var keyCode = e.keyCode || e.which || e.charCode;
var ctrlKey = e.ctrlKey || e.metaKey;
if (ctrlKey && keyCode == 86) {
var clipboardData = window.clipboardData;
processData(clipboardData);
}
}
return false;
}
var processData = function(clipboardData) {
$('table#dgBudget>tbody>tr.datarow').remove();
var data = clipboardData.getData('Text').split('\n');
var dataHtml = '';
for (var i = 0; i < data.length; i++) {
if (!data[i]) {
continue ;
}
var bugdetData = data[i].split('\t');
dataHtml += '<tr class="datarow">';
dataHtml += '<td>' + (bugdetData[0] || ' ') + '</td>';
dataHtml += '<td>' + (bugdetData[1] || ' ') + '</td>';
dataHtml += '<td>' + (bugdetData[2] || ' ') + '</td>';
dataHtml += '<td>' + (bugdetData[3] || ' ') + '</td>';
dataHtml += '<td>' + (bugdetData[4] || ' ') + '</td>';
dataHtml += '<td>' + (bugdetData[5] || ' ') + '</td>';
dataHtml += '<td>' + (bugdetData[6] || ' ') + '</td>';
dataHtml += '<td>' + (bugdetData[7] || ' ') + '</td>';
dataHtml += '<td>' + (bugdetData[8] || ' ') + '</td>';
dataHtml += '<td>' + (bugdetData[9] || ' ') + '</td>';
dataHtml += '<td>' + (bugdetData[10] || ' ') + '</td>';
dataHtml += '<td>' + (bugdetData[11] || ' ') + '</td>';
dataHtml += '<td>' + (bugdetData[12] || ' ') + '</td>';
dataHtml += '</tr>';
}
$('table#dgBudget>tbody>tr').after(dataHtml);
};
});
</script>
</head>
<body>
<div>使用 Ctrl + V 可粘貼預(yù)算數(shù)據(jù)到下表中</div>
<table id="dgBudget" border="1">
<tr>
<th>科目名稱(chēng)</th>
<th>1月</th>
<th>2月</th>
<th>3月</th>
<th>4月</th>
<th>5月</th>
<th>6月</th>
<th>7月</th>
<th>8月</th>
<th>9月</th>
<th>10月</th>
<th>11月</th>
<th>12月</th>
</tr>
</table>
</body>
</html>
效果如下:(注意引入jq)
以上就是“如何直接從excel中復(fù)制內(nèi)容到html表格中?”的詳細(xì)內(nèi)容,更多請(qǐng)關(guān)注木子天禾科技其它相關(guān)文章!