Javascript - Export Html Table Data Into Excel
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
Solution 1:
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 = functiontmpl(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).newFunction("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 userreturn 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/
Solution 2:
create a function and pass the tableID to it
functionpassing_id_to_excel(tableID){
var myTableid =
document.getElementById(tableID) //remaining code }
Solution 3:
- Add a checkbox for each table. Use javascript to process those that are checked.
- In case if you just want to convert every table, you could use
$('table').each(function() { do something })
.
Post a Comment for "Javascript - Export Html Table Data Into Excel"