How to export objects to a CSV file using pure JavaScript All In One
How to export objects to a CSV file using pure JavaScript All In One
如何使用纯 JavaScript 将对象导出到 CSV 文件
CSV
CSV 使用
,
/逗号
分隔
CSV file separator
CSV 文件分隔符
Comma-separated values (CSV)
Comma-separated values, a file format and extension
逗号分隔值、文件格式和扩展名
https://en.wikipedia.org/wiki/Comma-separated_values
https://en.wikipedia.org/wiki/CSV
solution
const obj = {
0: ["1405", "text string 1 #something"],
1: ["1366", "text string 2 #hashtag"],
603: ["92", "text string 603"],
};
const autoDonwload = (obj) => {
let result = `Views, Title`;
for(const key in obj) {
const [num, str] = obj[key];
result += `\n`;
result += `${num}, ${str}`;
}
console.log(`result =\n`, result);
const blob = new Blob([result], { type: "text/csv; charset=utf-8;" });
const url = URL.createObjectURL(blob);
const link = document.createElement("a");
link.setAttribute("href", url);
link.setAttribute("download", "export.csv");
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
}
demos
https://codepen.io/xgqfrms/pen/xxmodaB?editors=1011
![](https://img2023.cnblogs.com/blog/740516/202310/740516-20231017144546167-1303533680.png)
![image](https://user-images.githubusercontent.com/7291672/275735806-77054f7e-edd6-4865-9bd2-226752d0d5fa.png)
[![enter image description here][1]][1]
[1]: https://i.stack.imgur.com/sEgtw.png
(🐞 反爬虫测试!打击盗版⚠️)如果你看到这个信息, 说明这是一篇剽窃的文章,请访问 https://www.cnblogs.com/xgqfrms/ 查看原创文章!
CSV 指定分隔符
fix string contains
,
symbol issue
const autoDonwload = (obj) => {
// use `;` instead of `,` ✅
let result = `Views; Title`;
for(const key in obj) {
const [num, str] = obj[key];
result += `\n`;
result += `${num}; ${str}`;
}
const blob = new Blob([result], { type: "text/csv; charset=utf-8;" });
const url = URL.createObjectURL(blob);
const link = document.createElement("a");
link.setAttribute("href", url);
link.setAttribute("download", "export.csv");
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
};
const obj = {
// string contains `,` symbol ❓
0: ["1405", "text, string 1, #something"],
1: ["1366", "text string 2 #hashtag"],
603: ["92", "text string 603"],
};
autoDonwload(obj);
如导出文件时分割符
指定为“制表符”、“空格”、“分号
”,使用Excel打开导出的CSV文件时,1条记录的数据将集中在1个单元格内。
https://jp.cybozu.help/k/zh/trouble_shooting/app_trouble/csv_delimiter.html
https://get.kintone.help/k/zh/trouble_shooting/app_trouble/csv_delimiter.html
import csv
# Set the input and output file paths
input_file = 'input.csv'
output_file = 'output.csv'
# Open the input file for reading and the output file for writing
with open(input_file, 'r') as f_in, open(output_file, 'w', newline='') as f_out:
# Create a CSV reader and writer, specifying the current delimiter and the new delimiter
reader = csv.reader(f_in, delimiter=',')
writer = csv.writer(f_out, delimiter=';')
# Iterate through each row in the input file, convert the delimiter, and write to the output file
for row in reader:
writer.writerow(row)
https://www.quicktable.io/apps/zh/csv-change-delimiter/
refs
©xgqfrms 2012-2021
www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!
原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!
本文首发于博客园,作者:xgqfrms,原文链接:https://www.cnblogs.com/xgqfrms/p/17769709.html
未经授权禁止转载,违者必究!