本文共 1519 字,大约阅读时间需要 5 分钟。
node 导出csv文件
A great library you can use to quickly write an array of objects to a CSV file using Node.js is .
可以使用Node.js将数组快速写入CSV文件的出色库是 。
Many other libraries exist, of course. I found this useful for a project of mine where I had to generate a one-time CSV file, so I wrote this little tutorial.
当然,还有许多其他库。 我发现这对我的一个项目很有用,因为我必须生成一个一次性CSV文件,因此我写了这个小教程。
Using a -based library like might suits your needs in more performance-oriented applications.
使用像这样的基于的库可能适合您更注重性能的应用程序中的需求。
Install it using:
使用以下方法安装:
npm install objects-to-csv
then require it in your Node.js code:
然后在您的Node.js代码中要求它:
const ObjectsToCsv = require('objects-to-csv')
When you have an array of objects ready to write to CSV, initialize a new ObjectsToCsv object instance:
当您准备好要写入CSV的对象数组时,请初始化一个新的ObjectsToCsv对象实例:
const csv = new ObjectsToCsv(list)
then call csv.toDisk()
, passing the file you want to write to (relative to your app base path):
然后调用csv.toDisk()
,传递您要写入的文件(相对于您的应用程序基本路径):
await csv.toDisk('./list.csv')
This is a promise-based API and I used await
, so you need to call this inside an function.
这是一个基于promise的API,我使用了await
,因此您需要在一个函数中调用它。
The column names in the CSV are automatically inferred from the object properties names.
CSV中的列名称是根据对象属性名称自动推断出来的。
Note that this command overwrites the existing content of the file. To append to that file, pass a second object with the append
property set to true:
请注意,此命令将覆盖文件的现有内容。 要附加到该文件,请传递第二个对象,将append
属性设置为true:
await csv.toDisk('./list.csv', { append: true })
翻译自:
node 导出csv文件
转载地址:http://wlqgb.baihongyu.com/