Javascript 我需要脱光这门课;触摸“;将数据传递到excel工作表时

Javascript 我需要脱光这门课;触摸“;将数据传递到excel工作表时,javascript,jquery,html,css,excel,Javascript,Jquery,Html,Css,Excel,这是我的问题,touch(一个选择框)中的所有数据都在excel中呈现并创建空白 } 下面是我用来将数据传递到excel的方法: <td><input type="button" id="dlink" onclick="tableToExcel('project_table', 'W3C Example Table')" value="Export to Excel"></td> <a id="dlink" style="di

这是我的问题,touch(一个选择框)中的所有数据都在excel中呈现并创建空白

}

下面是我用来将数据传递到excel的方法:

<td><input type="button" id="dlink" onclick="tableToExcel('project_table', 'W3C Example Table')" value="Export to Excel"></td>
            <a id="dlink"  style="display:none;"></a>

var tableToExcel = (function () {
        var uri = 'data:application/vnd.ms-excel;base64,'
        , template = '<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]--></head><body><table>{table}</table></body></html>'
        , base64 = function (s) { return window.btoa(unescape(encodeURIComponent(s))) }
        , format = function (s, c) { return s.replace(/{(\w+)}/g, function (m, p) { return c[p]; }) }
        return function (table, name, filename) {
            if (!table.nodeType) table = document.getElementById(table)
            var ctx = { worksheet: name || 'Worksheet', table: table.innerHTML }
            document.getElementById("dlink").href = uri + base64(format(template, ctx));
            document.getElementById("dlink").download = filename;
            document.getElementById("dlink").click();

        }
    })()

var tableToExcel=(函数(){
var uri='data:application/vnd.ms excel;base64,'
,模板=“{table}”
,base64=函数{return window.btoa(unescape(encodeURIComponent))}
,format=函数(s,c){返回s.replace(/{(\w+)}/g,函数(m,p){返回c[p];})}
返回函数(表、名称、文件名){
如果(!table.nodeType)table=document.getElementById(table)
var ctx={工作表:名称| |'工作表',表:table.innerHTML}
document.getElementById(“dlink”).href=uri+base64(格式(模板,ctx));
document.getElementById(“dlink”).download=filename;
document.getElementById(“dlink”)。单击();
}
})()

var tableToExcel=(函数(){
$('#clonedtable.touch')。每个(函数(){
$(this.parent().prepend($(this.val());//如果要保留这些值。
});
$('#clonedtable.touch').remove();
var uri='data:application/vnd.ms excel;base64,'
,模板=“{table}”
,base64=函数{return window.btoa(unescape(encodeURIComponent))}
,format=函数(s,c){返回s.replace(/{(\w+)}/g,函数(m,p){返回c[p];})}
返回函数(表、名称、文件名){
如果(!table.nodeType)table=document.getElementById(table)
var ctx={工作表:名称| |'工作表',表:table.innerHTML}
document.getElementById(“dlink”).href=uri+base64(格式(模板,ctx));
document.getElementById(“dlink”).download=filename;
document.getElementById(“dlink”)。单击();
$(“#clonedtable”).remove();
}
})()

在将表格转换为excel之前,您可以获取这些值并将它们添加到行中,或者完全删除它们

如果您想保持表的完整性,我建议您克隆表,然后从克隆表中生成excel文件,然后将其删除

它看起来像这样

return function (table, name, filename) {
   $('#clonedtable .touch').each(function(){
       $(this).parent().prepend($(this).val()); //if you want to keep the values.
   });
   $('#clonedtable .touch').remove();
   .... //create the excel of the cloned table
   $('#clonedtable').remove(); 
    }
})()
更新 试试这个

var tableToExcel = (function () {
....
    return function (table, name, filename) {
        $('#'+table+' .touch').each(function(){
            $(this).parent().prepend($(this).val()); //if you want to keep the values.
        });
        $('#'+table+' .touch').remove();
        .... //create the excel of the cloned table
        $('#'+table).remove(); 
     }
})()

我现在就去试试。我将把jquery包装在它自己的JS函数中,并将其插入var ctx上方。我仍然有点困惑,我尝试了这个解决方案,但可能有语法错误。我将编辑我的问题,向您展示我所做的。。不过没用,我明天再看。。但是快速看一眼,我看不到你在哪里克隆桌子。。。不过,我明天会看看是否能帮上忙。@cook检查我的更新,尝试将所有代码移到
返回函数中
我还更改了代码,以便它引用传递给它的表。
  var tableToExcel = (function () {
   $('#clonedtable .touch').each(function(){
       $(this).parent().prepend($(this).val()); //if you want to keep the values.
   });
   $('#clonedtable .touch').remove();
   var uri = 'data:application/vnd.ms-excel;base64,'
        , template = '<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]--></head><body><table>{table}</table></body></html>'
        , base64 = function (s) { return window.btoa(unescape(encodeURIComponent(s))) }
        , format = function (s, c) { return s.replace(/{(\w+)}/g, function (m, p) { return c[p]; }) }
        return function (table, name, filename) {
            if (!table.nodeType) table = document.getElementById(table)

            var ctx = { worksheet: name || 'Worksheet', table: table.innerHTML }
            document.getElementById("dlink").href = uri + base64(format(template, ctx));
            document.getElementById("dlink").download = filename;
            document.getElementById("dlink").click();
   $('#clonedtable').remove(); 
    }
})()
return function (table, name, filename) {
   $('#clonedtable .touch').each(function(){
       $(this).parent().prepend($(this).val()); //if you want to keep the values.
   });
   $('#clonedtable .touch').remove();
   .... //create the excel of the cloned table
   $('#clonedtable').remove(); 
    }
})()
var tableToExcel = (function () {
....
    return function (table, name, filename) {
        $('#'+table+' .touch').each(function(){
            $(this).parent().prepend($(this).val()); //if you want to keep the values.
        });
        $('#'+table+' .touch').remove();
        .... //create the excel of the cloned table
        $('#'+table).remove(); 
     }
})()