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


[![enter image description here][1]][1]
[1]: https://i.stack.imgur.com/sEgtw.png
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
未经授权禁止转载,违者必究!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· .NET10 - 预览版1新功能体验(一)
2022-10-17 git & Sourcetree & git workflow All In One
2022-10-17 git rebase 与 git merge 的区别是什么 All In One
2021-10-17 Linux system environment All In One
2020-10-17 如何使用 js 检测控制台被用户打开了 All In One
2020-10-17 如何禁用 Chrome Taps Group feature 💩
2020-10-17 console.warn All In One
2020-10-17 js console.log all in one