最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Hide column while exporting to excel from html table using javascript - Stack Overflow

programmeradmin2浏览0评论

I have a html table which I have to export to excel , but while doing so, I dont want some of the td elements to be exported. When I apply javascript to hide the td, the changes are being applied only to the view form and not to the the content being exported.

Need help in how to export in this case.

I have included html, css and script all in one page.

<html>
<body>
<div id="myDiv">
<table id="metrics" border="1px" cellspacing="0 px" style="border-style:  solid; border-color: Black;
        border-width: thin;">
        <tr>
            <td style= "background-color: #bfbfbf;  font-size: small; color: black;">
                LOB
            </td>
            <td>
                <span class="hillbillyForm" data-displayname='LOB' style="display:none;"></span>

           </td>
       </tr>
</table>
    <input type="button" id="btnExport"  value="Export" onclick="TableToExcel('metrics');" />
</div>
</body>

Here I want to hide the td which contains span class "hillbillyForm"

The javascript That I am using is

    function TableToExcel(tableid) {

        var id = $('[id$="' + tableid + '"]');
        var $clonedTable = $("id").clone();
        $clonedTable.find('[style = "display: none"]').remove();
        var strCopy = $('<div></div>').html(id.clone()).html();

        window.clipboardData.setData("Text", strCopy);

        var objExcel = new ActiveXObject("Excel.Application");

        objExcel.visible = false; var objWorkbook = objExcel.Workbooks.Add; var objWorksheet = objWorkbook.Worksheets(1); objWorksheet.Paste; objExcel.visible = true;
    }
</script>

I have a html table which I have to export to excel , but while doing so, I dont want some of the td elements to be exported. When I apply javascript to hide the td, the changes are being applied only to the view form and not to the the content being exported.

Need help in how to export in this case.

I have included html, css and script all in one page.

<html>
<body>
<div id="myDiv">
<table id="metrics" border="1px" cellspacing="0 px" style="border-style:  solid; border-color: Black;
        border-width: thin;">
        <tr>
            <td style= "background-color: #bfbfbf;  font-size: small; color: black;">
                LOB
            </td>
            <td>
                <span class="hillbillyForm" data-displayname='LOB' style="display:none;"></span>

           </td>
       </tr>
</table>
    <input type="button" id="btnExport"  value="Export" onclick="TableToExcel('metrics');" />
</div>
</body>

Here I want to hide the td which contains span class "hillbillyForm"

The javascript That I am using is

    function TableToExcel(tableid) {

        var id = $('[id$="' + tableid + '"]');
        var $clonedTable = $("id").clone();
        $clonedTable.find('[style = "display: none"]').remove();
        var strCopy = $('<div></div>').html(id.clone()).html();

        window.clipboardData.setData("Text", strCopy);

        var objExcel = new ActiveXObject("Excel.Application");

        objExcel.visible = false; var objWorkbook = objExcel.Workbooks.Add; var objWorksheet = objWorkbook.Worksheets(1); objWorksheet.Paste; objExcel.visible = true;
    }
</script>
Share Improve this question asked Jan 16, 2015 at 15:36 DeepikaDeepika 3001 gold badge5 silver badges17 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 5

So what I'm understanding is that you want to remove all TD's which have a direct child with the class hillbillyForm.

You can do something like this:

const form = document.getElementById('#metrics');
const exportForm = form.cloneNode(true);

exportForm.querySelectorAll('.hillbillyForm').forEach((el) => {
  const td = el.parentElement;
  if (td) td.parentElement.removeChild(td);
});

// do something with exportForm

jsFiddle

发布评论

评论列表(0)

  1. 暂无评论