2013-03-05 67 views
12


我试图将HTML表格转换为Excel,我尝试使用将简单表格转换为Excel的JavaScript函数,它工作正常。如果我有多个表格,我将如何将所有表格数据添加到Excel文件中。这是我的尝试。我创建了2个表格并给出了表格索引testTabletestTable1JavaScript - 将HTML表格数据导出到Excel中

如何通过点击按钮将这两个表ID传递给JavaScript函数?现在点击按钮只有第一个表格被导出到Excel,因为我只通过'testTable'。我将如何能够导出多个表,例如:testTable,testTable1到Excel中?

这里的JavaScript的:

<script> 

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) { 
if (!table.nodeType) table = document.getElementById(table) 
var ctx = {worksheet: name || 'Worksheet', table: table.innerHTML} 
window.location.href = uri + base64(format(template, ctx)) 
} 
})() 

</script> 

这里的HTML部分,

<table id="testTable"> 
    <thead> 
     <tr> 
      <th>Name</th> 
      <th>ACP</th> 
      <th>OEMCP</th> 
      <th>Unix<br> 
       NT 3.1</th> 
      <th>Unix<br> 
       NT 3.51</th> 
      <th>Unix<br> 
       95</th> 
     </tr> 
    </thead> 
</table> 
<table id="testTable1"> 
    <thead> 
     <tr> 
      <th>Name</th> 
      <th>ACP</th> 
      <th>OEMCP</th> 
      <th>Windows<br> 
       NT 3.1</th> 
      <th>Windows<br> 
       NT 3.51</th> 
      <th>Windows<br> 
       95</th> 
     </tr> 
    </thead> 
</table> 

请让我知道,这可怎么办呢?
感谢

+0

不能使用Web服务这个问题。它也将工作,无需回传。 – osmanraifgunes 2013-03-05 12:50:24

回答

9

我推荐另一种格式的方法。 John Resig微模板是一款非常好的简单工具,可以满足您的需求。 (ejohn microtemplating

(function(){ 
    var cache = {}; 

    this.tmpl = function tmpl(str, data){ 
    // Figure out if we're getting a template, or if we need to 
    // load the template - and be sure to cache the result. 
    var fn = !/\W/.test(str) ? 
     cache[str] = cache[str] || 
     tmpl(document.getElementById(str).innerHTML) : 

     // Generate a reusable function that will serve as a template 
     // generator (and which will be cached). 
     new Function("obj", 
     "var p=[],print=function(){p.push.apply(p,arguments);};" + 

     // Introduce the data as local variables using with(){} 
     "with(obj){p.push('" + 

     // Convert the template into pure JavaScript 
     str.replace(/[\r\t\n]/g, " ") 
       .split("{{").join("\t") 
       .replace(/((^|}})[^\t]*)'/g, "$1\r") 
       .replace(/\t=(.*?)}}/g, "',$1,'") 
       .split("\t").join("');") 
       .split("}}").join("p.push('") 
       .split("\r").join("\\'") 
       + "');}return p.join('');"); 

    // Provide some basic currying to the user 
    return data ? fn(data) : fn; 
    }; 
})(); 

它的使用非常简单。这不仅允许在HTML之间显示变量,还可以执行JavaScript代码

您的模板字符串需要修改才能使用此微模板。

{{for(var i=0; i<tables.length;i++){ }} 
    <table> 
     {{=tables[i]}} 
    </table> 
{{ } }} 

最后只需要选择出现在您的例子中,所有表

document.getElementsByTagName("table"); 

你可以看到它是如何工作http://jsfiddle.net/Scipion/P8rpn/1/

+0

感谢吨Scipion!我只是想知道如果我可以通过点击一个html按钮来实现这个功能...我试过这个 但这是行不通的....它如何在jsFiddle上工作?如果我在html中实现相同的功能,则无法单击该按钮来获取文件。请让我知道这是否可能。 – shockwave 2013-03-09 18:57:08

+1

尝试并为其添加单击事件。函数下载(){ tableToExcel(document.getElementsByTagName(“table”),“one”); } var btn = document.getElementById(“btn”); btn.addEventListener(“click”,download); http://jsfiddle.net/Scipion/P8rpn/3/ – Scipion 2013-03-11 11:09:15

+0

非常感谢!这工作就像一个魅力! :) – shockwave 2013-03-12 18:04:29

0

创建函数和TABLEID传递给它

function passing_id_to_excel(tableID){ 
    var myTableid = 
document.getElementById(tableID) //remaining code } 
+0

我将如何传递多个tableID? – shockwave 2013-03-05 13:50:29

0
  1. 添加一个复选框为每个表。使用JavaScript来处理那些被检查的。
  2. 如果你只是想转换每个表,你可以使用$('table').each(function() { do something })
相关问题