Friday, May 17, 2024
 Popular · Latest · Hot · Upcoming
157
rated 0 times [  161] [ 4]  / answers: 1 / hits: 35285  / 11 Years ago, tue, march 5, 2013, 12:00:00


I'm trying to convert HTML tables to Excel, i have tried with a JavaScript function which converts a simple table to Excel, it is working fine. If I have multiple tables how will I be able to add all the table data into the Excel file. here's what I tried. I've created 2 tables and given table index testTable and testTable1.



How will i pass these 2 table ids to the JavaScript function on click of the button? right now on click of the button only the first table is exported to Excel as I'm passing only 'testTable'. how will i be able to export multiple tables eg: testTable, testTable1 into Excel?



Here's the 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>


Here's the HTML part,



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


Please let me know, how this can be done?

Thanks


More From » html

 Answers
37

I recommend another Format method. the John Resig micro-template is a very good and simple tool for do what you need. (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(/[rtn]/g, )
.split({{).join(t)
.replace(/((^|}})[^t]*)'/g, $1r)
.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;
};
})();


It is very simple to use. This allows not only show variables between HTML but also execute JavaScript code



Your template string need some modification to work with this microtemplate.



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


finally only need to select all the tables that appear in your example



document.getElementsByTagName(table);


you can see how it works http://jsfiddle.net/Scipion/P8rpn/1/


[#79818] Monday, March 4, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
victorr

Total Points: 193
Total Questions: 86
Total Answers: 105

Location: Pitcairn Islands
Member since Thu, Jun 24, 2021
3 Years ago
;