J.Doe
J.Doe

Reputation: 596

Import Data in Angular

I tried using csv files in my Angular projects like this:

import * as data1 from "./data.csv";
import * as data2 from "./data2.csv";

They are located in the same folder as the .ts i am trying to use them with. I got following error:

Cannot find module 'data.csv'

What I also tried was moving them to the assets folder:

import * as data1 from '/assets/data.csv';

But the error still shows up. What am I doing wrong?

Upvotes: 1

Views: 1292

Answers (2)

Emre Koca
Emre Koca

Reputation: 1

on your .ts file

public csvData: any;

public nColumns: string[] = ['name', 'lastname', 'phone', 'cardId', 'gender'];

public importData() {
    this.csvData.forEach((data: any) => {

      const user: any = {
        name: data.name,
        lastname: data.lastname,
        phone: data.phone,
        cardId: data.cardid,
        gender: data.gender
      };

      this.saveData(user);

    });

  }

you can try like this.

Upvotes: 0

galvan
galvan

Reputation: 7466

Read the csv file content using HttpClient, for example:

constructor (private http: HttpClient) {}

readCsvData () {
   this.http.get('path.to.csv')
   .subscribe(
        data => console.log(data),
        err => console.log(err)
    );
}

Upvotes: 5

Related Questions