使用javascript将动态表提交到excel

时间:2017-03-01 06:23:55

标签: javascript html excel

我创建了动态表,用于使用HTML和JavaScript添加和删除行。 如何使用javaScript将动态数据提交或导出到excel表格中,请给我帮助。

这是我的代码..

    <HTML>
<HEAD>
    <TITLE> Add/Remove dynamic rows in HTML table </TITLE>
    <SCRIPT language="javascript">
        function addRow(tableID) {

            var table = document.getElementById(tableID);

            var rowCount = table.rows.length;
            var row = table.insertRow(rowCount);

            var colCount = table.rows[0].cells.length;

            for(var i=0; i<colCount; i++) {

                var newcell = row.insertCell(i);

                newcell.innerHTML = table.rows[0].cells[i].innerHTML;
                //alert(newcell.childNodes);
                switch(newcell.childNodes[0].type) {
                    case "text":
                            newcell.childNodes[0].value = "";
                            break;
                    case "checkbox":
                            newcell.childNodes[0].checked = false;
                            break;
                    case "select-one":
                            newcell.childNodes[0].selectedIndex = 0;
                            break;
                }
            }
        }

        function deleteRow(tableID) {
            try {
            var table = document.getElementById(tableID);
            var rowCount = table.rows.length;

            for(var i=0; i<rowCount; i++) {
                var row = table.rows[i];
                var chkbox = row.cells[0].childNodes[0];
                if(null != chkbox && true == chkbox.checked) {
                    if(rowCount <= 1) {
                        alert("Cannot delete all the rows.");
                        break;
                    }
                    table.deleteRow(i);
                    rowCount--;
                    i--;
                }


            }
            }catch(e) {
                alert(e);
            }
        }

    </SCRIPT>
</HEAD>
<BODY>

    <INPUT type="button" value="Add Row" onclick="addRow('dataTable')" />

    <INPUT type="button" value="Delete Row" onclick="deleteRow('dataTable')" />
        <INPUT type="button" value="Submit" />


    <TABLE id="dataTable" width="350px" border="1">
        <TR>
        <TD><INPUT type="checkbox" name="chk"/></TD>
            <TD>Employee Name</TD>
            <TD><input type="text"name="ename"/></TD>
            <TD>Employee Id</TD>
        <TD><input type="text"name="eid"/></TD>
            <TD>Employee Date of Joining</TD>
        <TD><input type="date" name="edoj"/></TD>
            <TD>Employee Designation</TD>
            <TD>
                <SELECT name="edesignation">
                    <OPTION value="aset">Assistant Software Engineer Trainee</OPTION>
                    <OPTION value="ase">Assistant Software Engineer</OPTION>
                    <OPTION value="asse">Assistant Systems Engineer</OPTION>
                    <OPTION value="asa">Assistant Systems Analyst</OPTION>
                    <OPTION value="ita">IT Analyst</OPTION>
            <OPTION value="eng">Engineer</OPTION>
            <OPTION value="se">Software Engineer</OPTION>
            <OPTION value="sse">Systems Engineer</OPTION>
                </SELECT>
            </TD>
            <TD>Employee Salary</TD>
            <TD><input type="text"name="esalary"/></TD>
        </TR>
    </TABLE>

</BODY>
</HTML>

0 个答案:

没有答案