简体   繁体   中英

Export as excel from HTML table shows all the table contents with tags

i am currently having problem converting html table to excel but whenever i check the file it is filled with only one line ie with HTML tags of table. The chrome extension i am developing has table but with no id and class so i am targeting it using table:nth-child(2).

Here is my code:

var textbox = document.getElementsByName('regular')[0];
console.log(textbox);
    var para = document.createElement("input");

    var t = document.createTextNode("Show Password");
    para.setAttribute("type", "button"); 
    para.setAttribute("id", "btnExport"); 
    para.setAttribute("value","Export Table data into Excel");
    textbox.parentElement.appendChild(para);

$("#btnExport").click(function (e) {
    window.open('data:application/vnd.ms-excel,' + $('table:nth-child(2)').html());
    e.preventDefault();
});
<html>
<head>
<script src="http://code.jquery.com/jquery-1.10.2.js"></script>


<script>

    $(document).ready(function () {
        $("#btnExport").click(function (e) {
            window.open('data:application/vnd.ms-excel,' + $('#dvData').html());
            e.preventDefault();
        });
 });  
</script>
</head>
<body>
    <input type="button" id="btnExport" value="Export" />
    <div id="dvData">
    <table>
        <tr>
            <th>Column One</th>
            <th>Column Two</th>
            <th>Column Three</th>
        </tr>
        <tr>
            <td>row1 Col1</td>
            <td>row1 Col2</td>
            <td>row1 Col3</td>
        </tr>
        <tr>
            <td>row2 Col1</td>
            <td>row2 Col2</td>
            <td>row2 Col3</td>
        </tr>
        <tr>
            <td>row3 Col1</td>
            <td>row3 Col2</td>
            <td><a href="http://www.jquery2dotnet.com/">http://www.jquery2dotnet.com/</a>
            </td>
        </tr>
    </table>
</div>


</body>
</html>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM