js复制内容到剪贴板格式化粘贴到excel中

时间:2021-01-08 19:21:23

<input id="Button1" type="button" value="导出EXCEL" onclick="copyToClipboard('ta')">

<div style="width: 760px;float:left;" id="ta">
    <table border="0" style="" cellpadding="3" cellspacing="1" width="760">

    </table>

</div>

<script src="https://cdn.bootcss.com/jquery/1.12.3/jquery.min.js"></script>
<script>

function copyToClipboard(elementId) {
var aux = document.createElement("input");
var $a = $('#'+elementId).clone();
$a.find('input').each(function(){
var x = $(this).val();
$(this).parent().html(x);
});
aux.setAttribute("value", $a.html());
document.body.appendChild(aux);
aux.select();
document.execCommand("copy");
document.body.removeChild(aux);
alert("复制成功");
}
</script>