我正在使用 Filesaver.js 将 json 文件导出到 csv和 json-export-excel.js .当逗号分隔符在字符串中看到逗号时,它会导致列移动。
Plunker演示
如何忽略字符串中的逗号?
<button ng-json-export-excel data="data" report-fields="{name: 'Name', quote: 'Quote'}" filename ="'famousQuote'" separator="," class="purple_btn btn">Export to Excel</button>
JS 文件:
$scope.data = [
{
name: "Jane Austen",
quote: "It isn\'t what we say or think that defines us, but what we do.",
},
{
name: "Stephen King",
quote: "Quiet people have the loudest minds.",
},
]
当前 CSV 输出(不需要):(注意:|
标记 csv 文件中的列)
Name Quote
Jane Austen | It isn't what we say or think that defines us| but what we do.|
Stephen King| Quiet people have the loudest minds. | |
所需的 CSV 输出:
Name Quote
Jane Austen | It isn't what we say or think that defines us, but what we do.|
Stephen King| Quiet people have the loudest minds. |
最佳答案
对于 Excel,您需要将值用引号引起来。 See this question .
在 json-export-excel.js
中,您会看到 _objectToString
方法将输出用引号括起来,但是因为 fieldValue
变量不是此示例从未调用过的对象。
function _objectToString(object) {
var output = '';
angular.forEach(object, function(value, key) {
output += key + ':' + value + ' ';
});
return '"' + output + '"';
}
var fieldValue = data !== null ? data : ' ';
if fieldValue !== undefined && angular.isObject(fieldValue)) {
fieldValue = _objectToString(fieldValue);
}
如果您向其中添加 else 语句
以将值括在引号中,CSV 将根据需要在 Excel 中打开。
} else if (typeof fieldValue === "string") {
fieldValue = '"' + fieldValue + '"';
}
关于javascript - 导出到 csv 时忽略 JSON 字符串中的逗号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41266251/