简体   繁体   English

使用 Nodejs ExcelJS package 解析从 csv 文件读取的数据

[英]Parse the data read from csv file with Nodejs ExcelJS package

With NodeJs I need to fill the Excel file with the data fetched from the csv file.使用 NodeJs,我需要使用从 csv 文件中获取的数据填充 Excel 文件。 I am using the ExcelJS npm package.我正在使用 ExcelJS npm package。

I sucessfully read the data from csv fiel and write it in console.log() but the problem is, it is very strange format.我成功地从 csv 字段读取数据并将其写入 console.log() 但问题是,它的格式非常奇怪。

Code:代码:

var Excel = require("exceljs");

exports.generateExcel = async () => {
  let workbookNew = new Excel.Workbook();

  let data = await workbookNew.csv.readFile("./utilities/file.csv");

  const worksheet = workbookNew.worksheets[0];

  worksheet.eachRow(function (row: any, rowNumber: number) {
    console.log(JSON.stringify(row.values));
  });

};

Data looks like this:数据如下所示:

[null,"Users;1;"]
[null,"name1;2;"]
[null,"name2;3;"]
[null,"name3;4;"]
[null,"Classes;5;"]
[null,"class1;6;"]
[null,"class2;7;"]
[null,"class3;8;"]
[null,"Teachers;9;"]
[null,"teacher1;10;"]
[null,"teacher2;11;"]
[null,"Grades;12;"]
[null,"grade1;13;"]
[null,"grade2;14;"]
[null,"grade3;15;"]

So the Excel file which I need to fill with this data is very complex.. In specific cells I need to insert the users, in other sheet I need some images with grades, etc...所以我需要用这些数据填充的 Excel 文件非常复杂。在特定的单元格中我需要插入用户,在其他工作表中我需要一些带有等级的图像等......

The Main question for me is: How can I parse and store the data which is displayed in my console.log() in separate variables like Users in separate variable, Grades in separate variable and Teachers in separate variable.我的主要问题是:如何解析和存储在我的 console.log() 中显示的数据在单独的变量中,例如单独变量中的用户、单独变量中的成绩和单独变量中的教师。

Example for users:用户示例:

  users = {
    title: "Users",
    names: ["name1", "name2", "name3"],
  };

There is no need to be exactly the same as example, but the something that can be reused when I will read different csv files with same structure and so I could easily access to the specific data and put it in specific cell in the Excel file.不需要与示例完全相同,但是当我将读取具有相同结构的不同 csv 文件时可以重用的东西,因此我可以轻松访问特定数据并将其放入 Excel 文件的特定单元格中。

Thank you very much.非常感谢。

I prepared example, how could you pare your file.我准备了例子,你怎么能削减你的文件。 As it was proposed in one answer above we use fast-csv.正如在上面的一个答案中提出的那样,我们使用 fast-csv。 The parsing is quite simple you split by separator and than took line[0] which is first element.解析非常简单,您可以按分隔符拆分,然后将 line[0] 作为第一个元素。

const fs = require('fs');
const csv = require('@fast-csv/parse');

fs.createReadStream('Test_12345.csv')
    .pipe(csv.parse())
    .on('error', error => console.error(error))
    .on('data', function (row) {
        var line = String(row)
        line = line.split(';')
        console.log(`${line[0]}`)
        
        })
    .on('end', rowCount => console.log(`Parsed ${rowCount} rows`));

If we put for input like this:如果我们这样输入:

Value1;1101;
Value2;2202;
Value3;3303;
Value4;4404;

your output is in this case like this:你的 output 在这种情况下是这样的:

Value1
Value2
Value3
Value4

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM