简体   繁体   English

在html表的javascript导出中隐藏列

[英]Hide a Column in javascript export of html table

So I have a table of 12 columns and at the end I have a button that calls a function when I onclick and exports the table into .xls excel - I need to hide the 12th column in the export. 因此,我有一个包含12列的表格,最后有一个按钮,当我单击并将表格导出到.xls excel时,该按钮会调用一个函数-我需要在导出中隐藏第12列。 How do I hide the 12th column so it doesnt appear in the .xls ?? 如何隐藏第12列,使其不出现在.xls中?

Here is my code for the button 这是我的按钮代码

<li class="button"><a href="javascript:;" onclick="fnExcelReport();" style="background-color: #4cae4c;">Export EXCEL</a></li>

and here is the script 这是脚本

<script>

  function fnExcelReport()
{
var tab_text="<table border='2px'>"; 
var tab_text=tab_text+"<tr bgcolor='#eeeeee' height='50'><th colspan='12' style='text-align:center; font-size:20px;'>COMPLETED TRIPS</th></tr><tr bgcolor='#bf997e' height='50' color='#FFFFFF'>"; 

var i=0;
tab = document.getElementById('completed_trips_prov'); // id of table


for(i = 0 ; i < tab.rows.length ; i++) 
{     
    tab_text=tab_text+tab.rows[i].innerHTML+"</tr>";
    //tab_text=tab_text+"</tr>";
}

tab_text=tab_text+"</table>";
tab_text= tab_text.replace(/<a[^>]*>|<\/a>/g, "");//remove if u want links in your table
tab_text= tab_text.replace(/<img[^>]*>/gi,""); // remove if u want images in your table
tab_text= tab_text.replace(/<input[^>]*>|<\/input>/gi, ""); // reomves input params

var ua = window.navigator.userAgent;
var msie = ua.indexOf("MSIE "); 

if (msie > 0 || !!navigator.userAgent.match(/Trident.*rv\:11\./))      // If Internet Explorer
{
    txtArea1.document.open("txt/html","replace");
    txtArea1.document.write(tab_text);
    txtArea1.document.close();
    txtArea1.focus(); 
    sa=txtArea1.document.execCommand("SaveAs",true,"Say T.xls");
}  
else                 //other browser not tested on IE 11
    sa = window.open('data:application/vnd.ms-excel,' + encodeURIComponent(tab_text));  


return (sa);
}


</script>

With what you have now, you can remove the last cell in the row my modifying your loop with something like this: 现在,您可以删除行中的最后一个单元格,并使用如下所示的代码修改循环:

//hack // hack

for(i = 0 ; i < tab.rows.length ; i++) 
{    
    var row = tab.rows[i];
    var numberOfCells = row.cells.length;
    row.deleteCell(numberOfCells - 1);
    tab_text=tab_text+tab.rows[i].innerHTML+"</tr>";

}

Depending on what you want to write to the document, it might be better to loop through each cell inside the loop where you go through each row. 根据要写入文档的内容,遍历循环中每行的每个单元格可能会更好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM