admin管理员组文章数量:1356429
I have two functions
- Exports HTML Table
- Download CSV file
Unfortunately my file is downloading as "Undefined" with no .csv file type.
The format seems to be fine, since the file can be opened with any text editor and can be seen to have all table data with a ma as a delimitter. Which makes it easy for Excel to take in the file and seperate each column and row.
But I need the file to download with the .csv file extension.
Here are my two functions:
function exportTableToCSV(html, filename) {
var csv = [];
var rows = document.querySelectorAll("table tr");
for(var i = 0; i < rows.length; i++){
var row = [], cols = rows[i].querySelectorAll("td, th");
for(var j = 0; j < cols.length; j++){
row.push(cols[j].innerText);
}
csv.push(row.join(","));
}
// download csv file
downloadCSV(csv.join("\n"), filename);
}
function downloadCSV(csv, filename) {
var csvFile;
var downloadLink;
csvFile = new Blob([csv], {type:"text/csv"});
downloadLink = document.createElement("a");
downloadLink.download = filename;
downloadLink.href = window.URL.createObjectURL(csvFile);
downloadLink.style.display = "none";
document.body.appendChild(downloadLink);
downloadLink.click();
}
exportTableToCSV('', 'test.csv');
I have two functions
- Exports HTML Table
- Download CSV file
Unfortunately my file is downloading as "Undefined" with no .csv file type.
The format seems to be fine, since the file can be opened with any text editor and can be seen to have all table data with a ma as a delimitter. Which makes it easy for Excel to take in the file and seperate each column and row.
But I need the file to download with the .csv file extension.
Here are my two functions:
function exportTableToCSV(html, filename) {
var csv = [];
var rows = document.querySelectorAll("table tr");
for(var i = 0; i < rows.length; i++){
var row = [], cols = rows[i].querySelectorAll("td, th");
for(var j = 0; j < cols.length; j++){
row.push(cols[j].innerText);
}
csv.push(row.join(","));
}
// download csv file
downloadCSV(csv.join("\n"), filename);
}
function downloadCSV(csv, filename) {
var csvFile;
var downloadLink;
csvFile = new Blob([csv], {type:"text/csv"});
downloadLink = document.createElement("a");
downloadLink.download = filename;
downloadLink.href = window.URL.createObjectURL(csvFile);
downloadLink.style.display = "none";
document.body.appendChild(downloadLink);
downloadLink.click();
}
exportTableToCSV('', 'test.csv');
I believe the issue lies with 'function downloadCSV'. Any help or links to some current working examples are highly appreciated.
Share Improve this question edited Feb 5, 2018 at 20:35 Phillip Thomas 1,47911 silver badges21 bronze badges asked Feb 5, 2018 at 19:19 kris_needs_helpkris_needs_help 711 gold badge1 silver badge8 bronze badges 2- Your code appears to work fine, what browser are you using? – Phillip Thomas Commented Feb 5, 2018 at 19:32
- how can we tweak this to handle " in the strings? – mesqueeb Commented Aug 12, 2022 at 3:50
1 Answer
Reset to default 4Did you pass filename to exportTableToCSV like:
exportTableToCSV(null,'test.csv')
See https://jsfiddle/mdearman/d1zpndcu/
In some quick testing, it works in Chrome, but not IE (for what its worth).
本文标签: export to CSV with JavascriptStack Overflow
版权声明:本文标题:export to CSV with Javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744063066a2584484.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论