简体   繁体   中英

i add row to html table & it added successfully but i want to import that table data to excel sheet how to add on download button?

$(document).ready(function () {

        $("#btn_ImporttoExcelRD").click(function (e) {
            window.open('data:application/vnd.ms-excel,' + $('#div1').html());
            e.preventDefault();
        });
});


function addRow(in_tbl_name) {
        var count = "1";
        var tbody = document.getElementById(in_tbl_name).getElementsByTagName("TBODY")[0];
        // create row
        var row = document.createElement("TR");

        var rowCount = tbody.rows.length;
        //var row = tbody.insertRow(rowCount);

        // create table cell 1
        var td1 = document.createElement("TD")
        var strHtml1 = "<FONT SIZE=\"+5\"></FONT>";
        //td1.innerHTML = strHtml1.replace(/!count!/g, count);
        td1.innerHTML = rowCount + 0;

        // create table cell 2
        var td2 = document.createElement("TD")
        var strHtml2 = "<INPUT TYPE=\"text\" NAME=\"in_name\" SIZE=\"15\" MAXLENGTH=\"30\"  STYLE=\"height:24;border: 1 solid;margin:0;\">";
        td2.innerHTML = strHtml2.replace(/!count!/g, count);

        // create table cell 3
        var td3 = document.createElement("TD")
        var strHtml3 = "<INPUT TYPE=\"text\" NAME=\"in_name\" SIZE=\"15\" MAXLENGTH=\"30\"  STYLE=\"height:24;border: 1 solid;margin:0;\">";
        td3.innerHTML = strHtml3.replace(/!count!/g, count);

        // create table cell 4
        var td4 = document.createElement("TD")
        var strHtml4 = "<INPUT TYPE=\"text\" NAME=\"in_name\" SIZE=\"15\" MAXLENGTH=\"30\"  STYLE=\"height:24;border: 1 solid;margin:0;\">";
        td4.innerHTML = strHtml4.replace(/!count!/g, count);

        // create table cell 5
        var td5 = document.createElement("TD")
        var strHtml5 = "<INPUT TYPE=\"text\" NAME=\"in_name\" SIZE=\"15\" MAXLENGTH=\"30\"  STYLE=\"height:24;border: 1 solid;margin:0;\">";
        td5.innerHTML = strHtml5.replace(/!count!/g, count);

        // create table cell 6
        var td6 = document.createElement("TD")
        var strHtml6 = "<INPUT TYPE=\"text\" NAME=\"in_name\" SIZE=\"15\" MAXLENGTH=\"30\"  STYLE=\"height:24;border: 1 solid;margin:0;\">";
        td6.innerHTML = strHtml5.replace(/!count!/g, count);

        // create table cell 7
        var td7 = document.createElement("TD")
        var strHtml7 = "<INPUT TYPE=\"text\" NAME=\"in_name\" SIZE=\"15\" MAXLENGTH=\"30\"  STYLE=\"height:24;border: 1 solid;margin:0;\">";
        td7.innerHTML = strHtml5.replace(/!count!/g, count);

        // create table cell 8
        var td8 = document.createElement("TD")
        var strHtml8 = "<INPUT TYPE=\"text\" NAME=\"in_name\" SIZE=\"15\" MAXLENGTH=\"30\"  STYLE=\"height:24;border: 1 solid;margin:0;\">";
        td8.innerHTML = strHtml5.replace(/!count!/g, count);

        // create table cell 9
        var td9 = document.createElement("TD")
        var strHtml9 = "<INPUT TYPE=\"text\" NAME=\"in_name\" SIZE=\"15\" MAXLENGTH=\"30\"  STYLE=\"height:24;border: 1 solid;margin:0;\">";
        td9.innerHTML = strHtml5.replace(/!count!/g, count);



        // append data to row
        row.appendChild(td1);
        row.appendChild(td2);
        row.appendChild(td3);
        row.appendChild(td4);
        row.appendChild(td5);
        row.appendChild(td6);
        row.appendChild(td7);
        row.appendChild(td8);
        row.appendChild(td9);
        // add to count variable
        count = parseInt(count) + 1;
        // append row to table
        tbody.appendChild(row);
    }

Call this function in a button under your table

function excel()
{
   var url='data:application/vnd.ms-excel,' + encodeURIComponent($('#searchresult').html());
   location.href=url;
   return false;
}

Example:

<table id='searchresult>'>
..
<tr...>
..
</table>
<Button onClick=excel()>Export</Button>

It will work in all browser except IE

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