首页 > 代码库 > [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‘); }
复制粘贴到Excel样子:
希望有所帮助!
<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
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。