在javascript中导出表时排除列
在将表格导出到excel文件时,是否有任何方法可以排除特定列,我只想将Firstname和Savings排除到excel文件。目前我只是从表中导出所有数据,但我想排除Firstname和Savings列,有专家可以帮助吗? html在javascript中导出表时排除列,javascript,html,jquery,excel,export,Javascript,Html,Jquery,Excel,Export,在将表格导出到excel文件时,是否有任何方法可以排除特定列,我只想将Firstname和Savings排除到excel文件。目前我只是从表中导出所有数据,但我想排除Firstname和Savings列,有专家可以帮助吗? html 导出到Excel 名字 姓氏 储蓄 wowww 彼得 格里芬 $100 $100 露易丝 格里芬 $150 $100 乔 斯旺森 $300 $100 克利夫兰 棕色的 $250 $100 javascript $(文档).ready(函数(){ $(“#bt
导出到Excel
名字
姓氏
储蓄
wowww
彼得
格里芬
$100
$100
露易丝
格里芬
$150
$100
乔
斯旺森
$300
$100
克利夫兰
棕色的
$250
$100
javascript
$(文档).ready(函数(){
$(“#btnExport”)。单击(
函数(){
tableToExcel('testExportId','test','trys');
}
);
})
函数getIEVersion()
//返回Windows Internet Explorer或-1的版本
//(表示使用其他浏览器)。
{
var rv=-1;//返回值假定失败。
如果(navigator.appName==“Microsoft Internet Explorer”){
var ua=navigator.userAgent;
var re=newregexp(“MSIE([0-9]{1,}[\.0-9]{0,})”;
如果(重新执行(ua)!=null)
rv=parseFloat(RegExp.$1);
}
返回rv;
}
函数tableToExcel(表、表名、文件名){
var ua=window.navigator.userAgent;
变量msie=ua.indexOf(“msie”);
如果(msie>0 | |!!navigator.userAgent.match(/Trident.*rv \:11\./)//如果Internet Explorer
{
返回报告(表、文件名);
}
var uri='data:application/vnd.ms excel;base64',
templateData='{table}',
base64Conversion=函数{return window.btoa(unescape(encodeURIComponent))},
formatExcelData=函数(s,c){返回s.replace(/{(\w+)}/g,函数(m,p){返回c[p];})}
$(“tbody>tr[data level='0']);
if(!table.nodeType)
table=document.getElementById(表)
var ctx={工作表:sheetName | |'工作表',表:table.innerHTML}
var-element=document.createElement('a');
element.setAttribute('href','data:application/vnd.ms excel;base64',+
Base64转换(formatExcelData(templateData,ctx));
setAttribute('download',fileName);
element.style.display='none';
document.body.appendChild(元素);
元素。单击();
document.body.removeChild(元素);
$(“tbody>tr[data level='0']);
}
函数FNCelReport(表、文件名){
var tab_text=“”;
变量范围;
if(!table.nodeType)
table=document.getElementById(表)
$(“tbody>tr[data level='0']);
tab_text=tab_text+table.innerHTML;
tab_text=tab_text+“”;
tab_text=tab_text.replace(/]*>|/g,“”;//如果需要表中的链接,请删除
tab_text=tab_text.replace(//*>/gi,“”;//如果希望表中有图像,请删除
tab_text=tab_text.replace(//*>|/gi,“”)//reomves输入参数
打开(“txt/html”,“replace”);
txtArea1.文档。写入(制表符文本);
txtArea1.document.close();
txtArea1.focus();
sa=txtArea1.document.execCommand(“SaveAs”,false,fileName+”.xls);
$(“tbody>tr[data level='0']);
返回(sa);
}
您可以在要排除的列中添加类“excludeExport”
<button id="btnExport">Export To Excel</button>
<table id="testExportId">
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th class="excludeExport">Savings</th>
<th>wowww</th>
</tr>
<tr>
<td>Peter</td>
<td>Griffin</td>
<td class="excludeExport">$100</td>
<td>$100</td>
</tr>
<tr>
<td>Lois</td>
<td>Griffin</td>
<td class="excludeExport">$150</td>
<td>$100</td>
</tr>
<tr>
<td>Joe</td>
<td>Swanson</td>
<td class="excludeExport">$300</td>
<td>$100</td>
</tr>
<tr>
<td>Cleveland</td>
<td>Brown</td>
<td class="excludeExport">$250</td>
<td>$100</td>
</tr>
</table>
<script src = "https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#btnExport").click(
function () {
tableToExcel('testExportId','test','trys');
}
);
})
function getIEVersion()
// Returns the version of Windows Internet Explorer or a -1
// (indicating the use of another browser).
{
var rv = -1; // Return value assumes failure.
if (navigator.appName == 'Microsoft Internet Explorer') {
var ua = navigator.userAgent;
var re = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
if (re.exec(ua) != null)
rv = parseFloat(RegExp.$1);
}
return rv;
}
function tableToExcel(table, sheetName, fileName) {
var ua = window.navigator.userAgent;
var msie = ua.indexOf("MSIE ");
if (msie > 0 || !!navigator.userAgent.match(/Trident.*rv\:11\./)) // If Internet Explorer
{
return fnExcelReport(table, fileName);
}
var uri = 'data:application/vnd.ms-excel;base64,',
templateData = '<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40"><head><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name>{worksheet}</x:Name><x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--><meta http-equiv="content-type" content="text/plain; charset=UTF-8"/></head><body><table>{table}</table></body></html>',
base64Conversion = function (s) { return window.btoa(unescape(encodeURIComponent(s))) },
formatExcelData = function (s, c) { return s.replace(/{(\w+)}/g, function (m, p) { return c[p]; }) }
$("tbody > tr[data-level='0']").show();
if (!table.nodeType)
table = document.getElementById(table)
var ctx = { worksheet: sheetName || 'Worksheet', table: table.innerHTML }
var element = document.createElement('a');
element.setAttribute('href', 'data:application/vnd.ms-excel;base64,' +
base64Conversion(formatExcelData(templateData, ctx)));
element.setAttribute('download', fileName);
element.style.display = 'none';
document.body.appendChild(element);
element.click();
document.body.removeChild(element);
$("tbody > tr[data-level='0']").hide();
}
function fnExcelReport(table, fileName) {
var tab_text = "<table border='2px'>";
var textRange;
if (!table.nodeType)
table = document.getElementById(table)
$("tbody > tr[data-level='0']").show();
tab_text = tab_text + table.innerHTML;
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
txtArea1.document.open("txt/html", "replace");
txtArea1.document.write(tab_text);
txtArea1.document.close();
txtArea1.focus();
sa = txtArea1.document.execCommand("SaveAs", false, fileName + ".xls");
$("tbody > tr[data-level='0']").hide();
return (sa);
}
</script>
<button id="btnExport">Export To Excel</button>
<table id="testExportId">
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th class="excludeExport">Savings</th>
<th>wowww</th>
</tr>
<tr>
<td>Peter</td>
<td>Griffin</td>
<td class="excludeExport">$100</td>
<td>$100</td>
</tr>
<tr>
<td>Lois</td>
<td>Griffin</td>
<td class="excludeExport">$150</td>
<td>$100</td>
</tr>
<tr>
<td>Joe</td>
<td>Swanson</td>
<td class="excludeExport">$300</td>
<td>$100</td>
</tr>
<tr>
<td>Cleveland</td>
<td>Brown</td>
<td class="excludeExport">$250</td>
<td>$100</td>
</tr>
</table>
$(document).ready(function () {
$("#btnExport").click(function () {
var tempTable = $("#testExportId").html();
$("#testExportId .excludeExport").remove();
tableToExcel("testExportId", "test", "trys");
$("#testExportId").html(tempTable);
});
});