将 Excel 数据粘贴到 html 表格中 [英] Paste Excel data into html table

查看:38
本文介绍了将 Excel 数据粘贴到 html 表格中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 Javascript,我如何创建一个 HTML 表格,该表格可以通过在电子表格中复制"然后粘贴"到浏览器中的表格中,从而接受"来自 excel(或谷歌电子表格)的数字矩阵数据.

Using Javascript, how do I create an HTML table that can "accept" numeric matrix data from excel (or google spreadsheet), via "copy" in the spreadsheet and then "paste" into the table in the browser.

推荐答案

这只能在 IE 上可靠地工作,因为 Firefox(以及其他可能的)不允许在没有特别允许的情况下访问剪贴板;之前先粘贴到 textarea 的建议可能比这更好.

This would only work reliably on IE since Firefox (and likely others) don't allow access to the clipboard without specifically allowing it; the earlier suggestion of pasting into a textarea first might work better than this.

当您从电子表格复制时,通常单元格用制表符 (chr9) 分隔,行用 CR (chr13) 分隔.此脚本将剪贴板转换为二维数组,然后从中构建表格.不太优雅,但似乎可以从 Excel 中复制数据.

When you copy from a spreadsheet, generally the cells are separated with a tab (chr9) and the rows with a CR (chr13). This script converts the clipboard into a 2D array and then builds a table from it. Not too elegant but it seems to work copying data out of Excel.

<html>
<head>
<script language="javascript">
function clip() {

    // get the clipboard text

    var clipText = window.clipboardData.getData('Text');

    // split into rows

    clipRows = clipText.split(String.fromCharCode(13));

    // split rows into columns

    for (i=0; i<clipRows.length; i++) {
        clipRows[i] = clipRows[i].split(String.fromCharCode(9));
    }


    // write out in a table

    newTable = document.createElement("table")
    newTable.border = 1;
    for (i=0; i<clipRows.length - 1; i++) {

        newRow = newTable.insertRow();

        for (j=0; j<clipRows[i].length; j++) {
            newCell = newRow.insertCell();
            if (clipRows[i][j].length == 0) {
                newCell.innerText = ' ';
            }
            else {
                newCell.innerText = clipRows[i][j];
            }
        }
    }

    document.body.appendChild(newTable);
}
</script>
</head>
<body>
<input type="button" onclick="clip()">
</body>
</html>

这篇关于将 Excel 数据粘贴到 html 表格中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆