Fre*_*Sop 6 javascript arrays json object export-to-csv
我知道这是一个已经回答的问题,但找不到适合我的解决方案。
我有一个 HTML 按钮,单击该按钮时,会在打字稿(基本上是 javascript)中生成一个对象数组,我想生成一个将下载的 csv 文件。
下面是 javascript 对象数组的示例:
var items = [
{ "name": "Item 1", "color": "Green", "size": "X-Large" },
{ "name": "Item 2", "color": "Green", "size": "X-Large" },
{ "name": "Item 3", "color": "Green", "size": "X-Large" }];
// Loop the array of objects
for(let row = 0; row < items.length; row++){
let keysAmount = Object.keys(items[row]).length
let keysCounter = 0
// If this is the first row, generate the headings
if(row === 0){
// Loop each property of the object
for(let key in items[row]){
// This is to not add a comma at the last cell
// The '\n' adds a new line
csv += key + (keysCounter+1 < keysAmount ? ',' : '\r\n' )
keysCounter++
}
}else{
for(let key in items[row]){
csv += items[row][key] + (keysCounter+1 < keysAmount ? ',' : '\r\n' )
keysCounter++
}
}
keysCounter = 0
}
console.log(csv)
var hiddenElement = document.createElement('a');
hiddenElement.href = 'data:text/csv;charset=utf-8,' + encodeURI(csv);
hiddenElement.target = '_blank';
hiddenElement.download = 'people.csv';
hiddenElement.click();
Run Code Online (Sandbox Code Playgroud)
我尝试了这个示例代码,但在我的情况下,它创建了 csv 文件,但没有在 csv 文件中正确插入 csv 字符串,csv 字符串具有由逗号分隔的元素行,每行由新行确定,但是这里每行的所有数据都位于 csv 文件的一个单元格中,我怎样才能使每行的每个值位于不同的单元格中?
这是 csv 字符串:
name,color,size
Item 2,Green,X-Large
Item 3,Green,X-Large
Run Code Online (Sandbox Code Playgroud)
CSV 只是一个文件,每个单元格用逗号分隔,每行用新行分隔。您希望将每一列命名为对象的键。如果我理解正确的话:
{ "name": "Item 1", "color": "Green", "size": "X-Large" }
Run Code Online (Sandbox Code Playgroud)
将:
---------------------------
| name | color | size |
---------------------------
| Item 1 | Green | X-Large |
---------------------------
Run Code Online (Sandbox Code Playgroud)
更新:这是更新版本。
因此,您可以循环数组并使用 HTML5 中的文件 API 相应地生成 csv:
let csv
// Loop the array of objects
for(let row = 0; row < items.length; row++){
let keysAmount = Object.keys(items[row]).length
let keysCounter = 0
// If this is the first row, generate the headings
if(row === 0){
// Loop each property of the object
for(let key in items[row]){
// This is to not add a comma at the last cell
// The '\r\n' adds a new line
csv += key + (keysCounter+1 < keysAmount ? ',' : '\r\n' )
keysCounter++
}
}else{
for(let key in items[row]){
csv += items[row][key] + (keysCounter+1 < keysAmount ? ',' : '\r\n' )
keysCounter++
}
}
keysCounter = 0
}
// Once we are done looping, download the .csv by creating a link
let link = document.createElement('a')
link.id = 'download-csv'
link.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(csv));
link.setAttribute('download', 'yourfiletextgoeshere.csv');
document.body.appendChild(link)
document.querySelector('#download-csv').click()
Run Code Online (Sandbox Code Playgroud)
顺便说一句,如果您使用 TypeScript,那么最后一行document.querySelector('#download-csv').click()必须是“<HTMLElement>document.querySelector('#download-csv').click()
让我知道它是否对您有用”。
适用于 TypeScript 和 javascript 的 Fiddle(仅更改最后一行): https://jsfiddle.net/0p8revuh/4/
更新:要查看 Excel 中格式正确的文件(每个逗号分隔值的单元格),请执行以下操作:
单击next和finish。现在您应该能够看到数据格式正确。
| 归档时间: |
|
| 查看次数: |
35719 次 |
| 最近记录: |