Generate excel from an html table without a column

0

good day, I need help, I want to generate an xls file but I do not need one of the columns. Any way to do it?

<script>
    function ExportToExcel(htmlExport) {
        var ua = window.navigator.userAgent;
        var msie = ua.indexOf("MSIE ");

        //other browser not tested on IE 11
        // If Internet Explorer
        if (msie > 0 || !!navigator.userAgent.match(/Trident.*rv\:11\./)) {
            jQuery('body').append(" <iframe id=\"iframeExport\" style=\"display:none\"></iframe>");
            iframeExport.document.open("txt/html", "replace");
            iframeExport.document.write(htmlExport);
            iframeExport.document.close();
            iframeExport.focus();
            sa = iframeExport.document.execCommand("SaveAs", true, "List.xls");
        }
        else {
            var link = document.createElement('a');

            document.body.appendChild(link); // Firefox requires the link to be in the body
            link.download = "List.xls";
            link.href = 'data:application/vnd.ms-excel,' + escape(htmlExport);
            link.click();
            document.body.removeChild(link);
        }
    }

</script>
<HTML>

<head>
    <script src="../ace_template/components/jquery/dist/jquery.js"></script>
</head>

<body>
    <table id="tabla">
        <tr>
            <td>Campo 1.1</td>
            <td>Campo 1.2</td>
            <td>Campo 1.3</td>
            <td>Campo 1.4</td>
            <td>Campo 1.5</td>
        </tr>
        <tr>
            <td>Campo 2.1</td>
            <td>Campo 2.2</td>
            <td>Campo 2.3</td>
            <td>Campo 2.4</td>
            <td>Campo 2.5</td>
        </tr>
    </table>
    <input type="button" name="btnExportar" id="btnExportar" value="Exportar a Excel" onclick="ExportToExcel(jQuery('#tabla').prop('outerHTML'));"
    />
</body>

</HTML>

I have this code but it does not work as I need it

    
asked by user94276 18.07.2018 в 01:26
source

1 answer

0

Instead of passing the HTML to the function, pass the jQuery object, then clone it and manipulate it at your whim. Something like this:

    function ExportToExcel(tabla) {
        var clone = tabla.clone();
        var trs = clone.find('tr');
        $.each(trs, function(i, tr) {
          $(tr).find('td:eq(1)').remove(); 
          $(tr).find('td:eq(3)').remove(); 
        });
        var htmlExport = clone.prop('outerHTML');
        var ua = window.navigator.userAgent;
        var msie = ua.indexOf("MSIE ");

        //other browser not tested on IE 11
        // If Internet Explorer
        if (msie > 0 || !!navigator.userAgent.match(/Trident.*rv\:11\./)) {
            jQuery('body').append("<iframe id=\"iframeExport\" style=\"display:none\"></iframe>");
            iframeExport.document.open("txt/html", "replace");
            iframeExport.document.write(htmlExport);
            iframeExport.document.close();
            iframeExport.focus();
            sa = iframeExport.document.execCommand("SaveAs", true, "List.xls");
        }
        else {
            var link = document.createElement('a');

            document.body.appendChild(link); // Firefox requires the link to be in the body
            link.download = "List.xls";
            link.href = 'data:application/vnd.ms-excel,' + escape(htmlExport);
            link.click();
            document.body.removeChild(link);
        }
    }
<HTML>

<head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</head>

<body>
    <table id="tabla">
        <tr>
            <td>Campo 1.1</td>
            <td>Campo 1.2</td>
            <td>Campo 1.3</td>
            <td>Campo 1.4</td>
            <td>Campo 1.5</td>
        </tr>
        <tr>
            <td>Campo 2.1</td>
            <td>Campo 2.2</td>
            <td>Campo 2.3</td>
            <td>Campo 2.4</td>
            <td>Campo 2.5</td>
        </tr>
    </table>
    <input type="button" name="btnExportar" id="btnExportar" value="Exportar a Excel" onclick="ExportToExcel(jQuery('#tabla'));" />
</body>

</HTML>
    
answered by 18.07.2018 в 01:43