首页 > 代码库 > [JavaScript]在IE浏览器下,复制Table,并支持粘贴到EXCEL

[JavaScript]在IE浏览器下,复制Table,并支持粘贴到EXCEL

关键代码:

    JsUtils = {        selectedRange: function (el) {            /// <summary>            /// 选中内容            /// eg: selectedRange(document.getElementById(‘123‘));            /// </summary>            /// <param name="el">需要选中对象</param>            var body = document.body, range, sel;            if (document.createRange && window.getSelection) {                //Range 对象表示文档的连续范围区域,如用户在浏览器窗口中用鼠标拖动选中的区域。                //selection是对当前激活选中区(即高亮文本)进行操作。                range = document.createRange();                sel = window.getSelection();                sel.removeAllRanges();                try {                    range.selectNodeContents(el);                    sel.addRange(range);                } catch (e) {                    range.selectNode(el);                    sel.addRange(range);                }            } else if (body.createTextRange) {                range = body.createTextRange();                range.moveToElementText(el);                range.select();            }        }    };
    IEUitls = {        copyElecToClipboard: function (elecId) {            /// <summary>            /// 仅支持IE浏览器的对HTML元素复制到剪切板            /// </summary>            /// <param name="elecId">需要复制ID</param>            var obj = document.getElementById(elecId);            if (obj) {                JsUtils.selectedRange(obj);                document.execCommand(‘copy‘, true);            }        }    };
<style type="text/css">.csharpcode, .csharpcode pre{ font-size: small; color: black; font-family: consolas, "Courier New", courier, monospace; background-color: #ffffff; /*white-space: pre;*/}.csharpcode pre { margin: 0em; }.csharpcode .rem { color: #008000; }.csharpcode .kwrd { color: #0000ff; }.csharpcode .str { color: #006080; }.csharpcode .op { color: #0000c0; }.csharpcode .preproc { color: #cc6633; }.csharpcode .asp { background-color: #ffff00; }.csharpcode .html { color: #800000; }.csharpcode .attr { color: #ff0000; }.csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em;}.csharpcode .lnum { color: #606060; }</style>

实现代码:

        function Copy() {            /// <summary>            /// 将经纬度表格数据复制到剪切板            /// </summary>            //var rowIds = $("#list4").jqGrid(‘getDataIDs‘);            //if (rowIds) {            //    var copyDate = ‘‘;            //    for (var i = 0, j = rowIds.length; i < j; i++) {            //        var curRowData = http://www.mamicode.com/$("#list4").jqGrid(‘getRowData‘, rowIds[i]);            //        copyDate += curRowData[‘lat‘] + ‘   ‘ + curRowData[‘lng‘] + ‘\n‘;            //    }            //    IEUitls.copyTextToClipboard(copyDate);            //}            IEUitls.copyElecToClipboard(‘list4‘);        }

image

复制粘贴到Excel样子:

image

希望有所帮助!

<style type="text/css">.csharpcode, .csharpcode pre{ font-size: small; color: black; font-family: consolas, "Courier New", courier, monospace; background-color: #ffffff; /*white-space: pre;*/}.csharpcode pre { margin: 0em; }.csharpcode .rem { color: #008000; }.csharpcode .kwrd { color: #0000ff; }.csharpcode .str { color: #006080; }.csharpcode .op { color: #0000c0; }.csharpcode .preproc { color: #cc6633; }.csharpcode .asp { background-color: #ffff00; }.csharpcode .html { color: #800000; }.csharpcode .attr { color: #ff0000; }.csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em;}.csharpcode .lnum { color: #606060; }</style>

[JavaScript]在IE浏览器下,复制Table,并支持粘贴到EXCEL