如何将JSON导出到CSV或Excel - Angular 2 [英] How to Export JSON to CSV or Excel - Angular 2

查看:603
本文介绍了如何将JSON导出到CSV或Excel - Angular 2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我的json是这样的:

Say my json is like this:

var readyToExport = [
   {id: 1, name: 'a'},
   {id: 2, name: 'b'},
   {id: 3, name: 'c'}
];

如何在Angular2中将此JSON导出为CSV或Excel文件?

How can I export this JSON into CSV or Excel file in Angular2?

我使用的浏览器是Chrome。

The browser that I'm using is Chrome.

也许Angular2不相关,但是,是否有任何第三方插件可以在Angular2中注入并执行此任务?

Maybe Angular2 is not relevant, however, is there any third party plugin that can be injected in Angular2 and perform this task?

推荐答案

您使用Angular这一事实并不是那么重要,尽管它确实为更多的lib开放。

The fact that you are using Angular isn't all that important, though it does open up for some more libs.

你基本上有两个选择。


  1. 编写你自己的json2csv转换器,这是一切都那么难。你已经拥有了JSON,你可以转向JS对象,然后迭代每个对象并获得当前列的正确字段。

  2. 你可以使用像 https://github.com/zemirco/json2csv 为你做的。

  1. Write your own json2csv converter, which isn't all that hard. You already have the JSON, which you can turn to JS objects, and then just iterate over every object and get the correct field for the current column.
  2. You can use a lib like https://github.com/zemirco/json2csv which does it for you.

此外,这个问题可能会回答你的问题如何将JSON转换为CSV格式并存储在变量中

Also, this SO question probably answers your question How to convert JSON to CSV format and store in a variable

CSV是类似Excel的程序的基本格式。除非你真的需要,否则不要乱用x​​ls(x)。它会让你的大脑受伤。

CSV is the basic format for Excel-like programs. Don't go messing with xls(x) unless you really have to. It will make your brain hurt.

这篇关于如何将JSON导出到CSV或Excel - Angular 2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆