Template:ExportCSV
Template page
More actions
<button onclick="(function(btn){
var container = btn.parentElement;
var tbl = container.nextElementSibling;
while(tbl && tbl.tagName !== 'TABLE') tbl = tbl.nextElementSibling;
if(!tbl){ alert('Table not found'); return; }
var rows = tbl.querySelectorAll('tr');
var csv = [];
rows.forEach(function(row){
var cells = row.querySelectorAll('th, td');
var line = [];
cells.forEach(function(cell){
var txt = cell.innerText.replace(/
/g,' ').trim();
if(txt.indexOf(',') !== -1 || txt.indexOf('"') !== -1) txt = '"' + txt.replace(/"/g,'""') + '"';
line.push(txt);
});
if(line.length) csv.push(line.join(','));
});
var blob = new Blob([csv.join('
')], {type:'text/csv'});
var a = document.createElement('a');
a.href = URL.createObjectURL(blob);
a.download = (document.title.replace(/ - .*/,).trim().replace(/ /g,'_') || 'export') + '.csv';
document.body.appendChild(a); a.click(); document.body.removeChild(a);
})(this)" style="cursor:pointer; background:#2a2a2a; color:#e07000; border:1px solid #e07000; border-radius:4px; padding:4px 12px; font-size:0.85em; font-weight:bold;">⬇ Export CSV</button>
Usage
Place {{ExportCSV}} immediately before any wikitable to add a CSV export button above it.
The button reads the next <table> element in the DOM and downloads it as a .csv file named after the page title.