0

我正在使用此脚本进行 excel 到 html 表的转换,但这仅适用于我最初为您提供的列和行的有限数量我希望它在我的 excel 文件中给出的基础或行和列上工作

<script>
    function _ge(id) {
        return document.getElementById(id);
    }

    function _nc(val) {
        return (val != null && val.length > 0);
    }

    function convert2HTML() {
        var fo = _ge('file1');
        var so = _ge('txtSheetName');
        var ho = _ge('txtHeaderRowStart');
        var co = _ge('txtHeaderColStart');
        var hco = _ge('txtHeaderCols');
        var ro = _ge('txtDataRows');

        if (!(_nc(fo.value) && _nc(so.value) && _nc(ho.value) && _nc(co.value) && _nc(hco.value) && _nc(ro.value))) {
            alert('All the fields are mandatory.');
            return false;
        }

        var ex;
        try {
            ex = new ActiveXObject("Excel.Application");
        } catch (e) {
            alert('Your browser does not support the Activex object.\nPlease switch to Internet Explorer.');
            return false;
        }
        var ef = ex.Workbooks.Open(fo.value, true);
        var es = ex.Worksheets(so.value);
        var rs = parseInt(ho.value, 10);
        var cs = parseInt(co.value, 10);
        var ce = cs + parseInt(hco.value, 10) - 1;
        var re = rs + parseInt(ro.value, 10);

        var oc = _ge('chartContainer');
        oc.innerHTML = '';
        var tbl = document.createElement('TABLE');
        tbl.id = 'tblExcel2Html';
        tbl.border = '0';
        tbl.cellPadding = '4';
        tbl.cellSpacing = '0';
        oc.appendChild(tbl);
        var i, j, row, col, r, c;

        for (i = rs, r = 0; i <= re; i++, r++) {
            row = tbl.insertRow(r);
            row.className = (i == rs) ? 'tblHeader' : (i % 2 == 0) ? 'evenRow' : 'oddRow';
            for (j = cs, c = 0; j <= ce; j++, c++) {
                col = row.insertCell(c);
                col.className = (j == ce) ? 'lastCol' : '';
                col.innerHTML = es.Cells(i, j).value || ' ';

            }
        }
        _ge('btnGetSrc').style.display = '';
    }

    function toggleSrc() {
        if (_ge('chartContainer').style.display == '') {
            getHTMLSrc();
        } else {
            back2Table();
        }
    }

    function getHTMLSrc() {
        var oc = _ge('chartContainer');
        var tx = _ge('txtOutput');
        var so = document.getElementsByTagName('style');
        var str = '<html><body>' + oc.outerHTML + so[0].outerHTML + '</body></html>';
        tx.value = str;
        oc.style.display = 'none';
        _ge('divOutput').style.display = '';
    }

    function copy2Clipboard() {
        var tx = _ge('txtOutput');
        window.clipboardData.setData("Text", tx.value);
    }

    function resetFields() {
        window.location.reload();
    }

    function back2Table() {
        _ge('divOutput').style.display = 'none';
        _ge('btnGetSrc').style.display = '';
        _ge('chartContainer').style.display = '';
    }

    function numberOnly(obj) {
        obj.value = obj.value.replace(/[^0-9]*/, '');
    }
</script>

我正在使用此脚本进行 excel 到 html 表的转换,但这仅适用于我最初为您提供的列和行的有限数量我希望它在我的 excel 文件中给出的基础或行和列上工作

4

0 回答 0