Home > OS >  Delete specific column when importing to excel
Delete specific column when importing to excel

Time:11-20

I have the following JS code to import from a HTML table to Excel.

function fnExcelReport(){

var tab_text="<table border='2px'><tr bgcolor='#87AFC6'>";
var textRange; var j=0;
tab = document.getElementById('myTable'); // id of table

for(j = 0 ; j < tab.rows.length ; j  )
{
    tab_text=tab_text tab.rows[j].innerHTML "</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 Thanks to Sumit.xls");
}
else                 //other browser not tested on IE 11
    sa = window.open('data:application/vnd.ms-excel,'   encodeURIComponent(tab_text));

return (sa);

}

I don't want to import the last column from the table. How can i do it?

CodePudding user response:

If you use jquery then you can remove the last column easily by using last child selector

$('#myTable tr').find('th:last-child, td:last-child').remove()

But if you want to use JS, then try like below (it's just a dummy table for example purpose)

Show code snippet

// GET ALL THE ROW OF THE TABLE USING TABLE ID
var tRow = document.getElementById('myTable').rows;
 // LOOPING OVER EACH ROW
 for (var i=0; i< tRow.length; i  ) {
   tRow[i].deleteCell(-1);  //DELETE THE LAST COLUMN
 }

for(j = 0 ; j < tRow.length ; j  )
{
    console.log(tRow[j].innerHTML);
}
table {
  font-family: arial, sans-serif;
  border-collapse: collapse;
  width: 100%;
}

td, th {
  border: 1px solid #dddddd;
  text-align: left;
  padding: 8px;
}

tr:nth-child(even) {
  background-color: #dddddd;
}
<table id="myTable">
  <tr>
    <th>Company</th>
    <th>Contact</th>
    <th>Country</th>
    <th>ISO Code</th>
  </tr>
  <tr>
    <td>Alfreds Futterkiste</td>
    <td>Maria Anders</td>
    <td>Germany</td>
    <td>AL</td>
  </tr>
  <tr>
    <td>Centro comercial Moctezuma</td>
    <td>Francisco Chang</td>
    <td>Mexico</td>
    <td>MEX</td>
  </tr>
  <tr>
    <td>Ernst Handel</td>
    <td>Roland Mendel</td>
    <td>Austria</td>
    <td>AUS</td>
  </tr>
  <tr>
    <td>Island Trading</td>
    <td>Helen Bennett</td>
    <td>UK</td>
    <td>UK</td>
  </tr>
  <tr>
    <td>Laughing Bacchus Winecellars</td>
    <td>Yoshi Tannamuri</td>
    <td>Canada</td>
    <td>CAD</td>
  </tr>
  <tr>
    <td>Magazzini Alimentari Riuniti</td>
    <td>Giovanni Rovelli</td>
    <td>Italy</td>
    <td>ITL</td>
  </tr>
</table>
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

So after getting the table data in your code, you can apply any of the methods you prefer. If you choose second method then your code will be like below:

tab = document.getElementById('myTable'); // id of table

tabRows = tab.rows; //get the rows

for (var i=0; i< tabRows.length; i  ) {
   tabRows[i].deleteCell(-1);  //DELETE THE LAST ONE
 }

for(j = 0 ; j < tabRows.length ; j  ) //DO YOUR ACTUAL WORK
{
    //Your conditions 
}
  • Related