ITADN
coston/json-to-csv-export
README.md
以下内容由 AI 翻译,如有问题请点此提交 issue 反馈

⬇️ json-to-csv-export

一个用于轻松生成 JSON 数据 CSV 下载的功能。✨

npm package version   npm downloads   code style: prettier

实时演示

https://json-to-csv-export.coston.io

功能

  • 从 json 数据创建 csv 下载
  • 轻量级
  • 易于使用
  • 可选的文件名
  • 可选的表头名称

安装

使用 npm 安装:

npm i json-to-csv-export

使用示例

// import jsonToCsvExport from "json-to-csv-export";

() => {
  const mockData = [
    {
      ID: 1,
      "First Name": "Sarajane",
      "Last Name": "Wheatman",
      Email: "swheatman0@google.nl",
      Language: "Zulu",
      "IP Address": "40.98.252.240",
    },
    {
      ID: 2,
      "First Name": "Linell",
      "Last Name": "Humpherston",
      Email: "lhumpherston1@google.com.br",
      Language: "Czech",
      "IP Address": "82.225.151.150",
    },
  ];

  return (
    <button onClick={() => jsonToCsvExport({ data: mockData })}>
      Download Data
    </button>
  );
};

带 Header 映射的示例

// import jsonToCsvExport from "json-to-csv-export";
() => {
  const mockData = [
    {
      id: 1,
      firstName: "Sarajane",
      lastName: "Wheatman",
      email: "swheatman0@google.nl",
      language: "Zulu",
      ip: "40.98.252.240",
    },
    {
      id: 2,
      firstName: "Linell",
      lastName: "Humpherston",
      email: "lhumpherston1@google.com.br",
      language: "Czech",
      ip: "82.225.151.150",
    },
  ];

  const headers = [
    { key: "id", label: "Identifier" },
    { key: "firstName", label: "First Name" },
    { key: "lastName", label: "Last Name" },
    { key: "email", label: "Email Address" },
    { key: "language", label: "Language" },
    { key: "ip", label: "IP Address" },
  ];

  return (
    <button onClick={() => jsonToCsvExport({ data: mockData, headers })}>
      Download Data
    </button>
  );
};

属性

interface HeaderMapping {
  label: string;
  key: string;
}
#PropertyTypeRequirementDefaultDescription
1dataRecord<string, any>[]required对象数组
2filenamestringoptional"export.csv"文件名。如果文件名中未包含 .csv 扩展名,则会自动添加
3delimiterstringoptional","字段分隔符
4headersstring[], HeaderMapping[]optional提供的数据对象键

从 1.x 版本迁移到 2.x 版本

migration.md

贡献

请帮助更快地提供高质量数据!提交任何问题和/或发起拉取请求!