admin管理员组文章数量:1394077
I export JSON data to CSV format and download it with JavaScript. Everything works fine except if the data has the hash sign #. The function does not export all data, for example:
this is my first C# lesson in the academy, it exports only this is my first C and ignore the rest of it.
This is my code
this.handleRow = function (row) {
var finalVal = '';
for (var j = 0; j < row.length; j++) {
var innerValue = "";
if (row[j]) {
innerValue = row[j].toString();
}
if (row[j] instanceof Date) {
innerValue = row[j].toLocaleString();
}
var result = innerValue.replace(/"/g, '""');
if (result.search(/("|,|\n)/g) >= 0) {
result = '"' + result + '"';
}
if (j > 0) finalVal += ',';
finalVal += result;
}
return finalVal + '\n';
};
this.jsonToCsv = function (filename, rows) {
var csvFile = '';
for (var i = 0; i < rows.length; i++) {
csvFile += this.handleRow(rows[i]);
}
var blob = new Blob([csvFile], { type: 'text/csv;charset=utf-8;'});
if (navigator.msSaveBlob) { // IE 10+
navigator.msSaveBlob(blob, filename);
} else {
var link = $window.document.createElement("a");
if (typeof link.download === "string") {
link.setAttribute("href", "data:text/csv;charset=utf-8,%EF%BB%BF" + encodeURI(csvFile));
link.setAttribute("download", filename);
link.style.visibility = 'hidden';
$window.document.body.appendChild(link);
link.click();
$window.document.body.removeChild(link);
}
}
};
I export JSON data to CSV format and download it with JavaScript. Everything works fine except if the data has the hash sign #. The function does not export all data, for example:
this is my first C# lesson in the academy, it exports only this is my first C and ignore the rest of it.
This is my code
this.handleRow = function (row) {
var finalVal = '';
for (var j = 0; j < row.length; j++) {
var innerValue = "";
if (row[j]) {
innerValue = row[j].toString();
}
if (row[j] instanceof Date) {
innerValue = row[j].toLocaleString();
}
var result = innerValue.replace(/"/g, '""');
if (result.search(/("|,|\n)/g) >= 0) {
result = '"' + result + '"';
}
if (j > 0) finalVal += ',';
finalVal += result;
}
return finalVal + '\n';
};
this.jsonToCsv = function (filename, rows) {
var csvFile = '';
for (var i = 0; i < rows.length; i++) {
csvFile += this.handleRow(rows[i]);
}
var blob = new Blob([csvFile], { type: 'text/csv;charset=utf-8;'});
if (navigator.msSaveBlob) { // IE 10+
navigator.msSaveBlob(blob, filename);
} else {
var link = $window.document.createElement("a");
if (typeof link.download === "string") {
link.setAttribute("href", "data:text/csv;charset=utf-8,%EF%BB%BF" + encodeURI(csvFile));
link.setAttribute("download", filename);
link.style.visibility = 'hidden';
$window.document.body.appendChild(link);
link.click();
$window.document.body.removeChild(link);
}
}
};
Share
Improve this question
edited Mar 23, 2021 at 16:41
mason
32.7k11 gold badges83 silver badges126 bronze badges
asked Mar 23, 2021 at 16:28
Daina HodgesDaina Hodges
8533 gold badges15 silver badges39 bronze badges
1
- 3 Note that # is not "the sharp character". The actual sharp character is ♯. The # character (notice the subtle difference) is called the number sign, hash, or pound sign. The # is monly used with the C♯ programming language, because there's not a simple way to type the ♯ character. – mason Commented Mar 23, 2021 at 16:37
1 Answer
Reset to default 9encodeURI
encodes characters which are forbidden in a URI, not ones which have special meaning.
The #
won't be encoded but it starts the fragment identifier portion of the URI.
Use encodeURIComponent
instead.
本文标签: Export CSV data withcharacter in JavaScript does not workStack Overflow
版权声明:本文标题:Export CSV data with # character in JavaScript does not work - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744672035a2618883.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论