从HTML表导出为ex​​cel显示带有标记的所有表内容

时间:2021-01-20 23:12:15

i am currently having problem converting html table to excel but whenever i check the file it is filled with only one line i.e 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).

我目前在将html表转换为excel时遇到问题,但每当我检查文件时,它只填充一行,即表格的HTML标签。我正在开发的chrome扩展程序有table但没有id和class所以我使用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();
});

1 个解决方案

#1


0  

<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>

#1


0  

<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>