xgqfrms™, xgqfrms® : xgqfrms's offical website of cnblogs! xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!

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)

image

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

image

enter image description here

(🐞 反爬虫测试!打击盗版⚠️)如果你看到这个信息, 说明这是一篇剽窃的文章,请访问 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);

image

如导出文件时分割符指定为“制表符”、“空格”、“分号”,使用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

https://stackoverflow.com/questions/77306499/javascript-export-object-to-csv-as-a-table/77306797#77306797



©xgqfrms 2012-2021

www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!

原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!


posted @ 2023-10-17 14:54  xgqfrms  阅读(13)  评论(1编辑  收藏  举报