gpt4 book ai didi

javascript - 导入到excel时删除特定列

转载 作者:行者123 更新时间:2023-12-04 20:22:10 25 4
gpt4 key购买 nike

我有以下 JS 代码可以从 HTML 表导入 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);
}
我不想从表中导入最后一列。我该怎么做?

最佳答案

如果您使用 jquery,那么您可以使用 last child selector 轻松删除最后一列

$('#myTable tr').find('th:last-child, td:last-child').remove()
但是,如果您想使用 JS,请尝试如下(例如,这只是一个虚拟表)

// 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>

因此,在您的代码中获取表格数据后,您可以应用您喜欢的任何方法。
如果您选择第二种方法,那么您的代码将如下所示:
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
}

关于javascript - 导入到excel时删除特定列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70023960/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com