How to Upload and Download CSV Files With Angular

Datasets are extremely essential in building API models, and various business processes. This is why importing and exporting CSV is an often-needed functionality.

In this tutorial you will learn now to download and import a CSV file within an Angular app. We’ll be working with a CSV file that contains employee details. The code will be written in TypeScript. 

Before you start, you’ll need to set up a new Angular environment, create a new workspace with an initial application and start serving. A step-by-step guide to creating the boilerplate for your Angular app can be found in the official tutorial.

Here’s the folder structure of the boilerplate application:

1. Create a Simple Model

Firstly, let’s create a simple class to model the employee. This model will be consumed by the Angular components.

The model for holding the employee details is given below. This is just a basic model with the employee’s name, email and city. 

2. Build the Service

Angular applications have a modular structure. This makes the application robust, and easy to maintain. One of the primary rules of any Angular application is that components must not be allowed to save or fetch data directly. That is why you have to make use of services to access, and present data. In our use case, we require a service for downloading, and importing CSV data.

Our service file is csv.services.ts

Once the service is created, it can be injected into any component. Before making the CSV.service available for injection, it needs to be registered in app.module.ts as a provider.

In this demo, we will be coding two different types of functionalities:

  • saving data into a CSV
  • importing data from a CSV

Method to Save Data Into a CSV

Firstly, let us deal with simplest functionality. Saving data into a CSV involves reading an array of objects. Objects in Javascript come with properties that have a value. We will read all these properties for each object and join them with a comma. The below line of code will achieve this:

Once the titles are joined, they will be appended as the first row. Following which, a loop will run through the array and read all the properties and its values. These values will be appended into the CSV file. 

The complete piece of code, for building the header, reading the contents of the array, and building the CSV is given below.

Method to Import Data from a CSV

Our next service is to import data from a CSV. For this, we will define a function that would take the contents of a file in the form of a string. The property names, and data rows will be created from the contents of the file. 

With the above lines of code, we will have both the property names and data. With this information, we are only left with the job of printing the contents of the CSV. Code for printing the contents is given below:

4. Building the UI Elements

Next, we are going to build the HTML elements for importing, and exporting the CSV files. 

The file for UI elements is app.component.html.

Saving Contents of a CSV 

To save a CSV file, we will be using a very traditional way of downloading files in JavaScript. 

  1. Create a hidden anchor element <a>.
  2. Set the href type of this anchor element to data:text/csv.
  3. Automatically trigger the click event.

The complete piece of code, for downloading the CSV file is given below. This code will be in app.component.ts.

Here’s some sample data we can passed into the above function. As you can see, we are using the model created in step 1.

Next, we shall link the above functionality to a button that will be displayed on the screen for us to click. This will be entered in app.component.html.

Reading the Contents of a CSV

It is quite simple to open and read files in HTML using the <input> tag. The input tag comes with properties like type, [accept] and (change).

  • type has to be 'file' to indicate that a file will be chosen by the user. 
  • [accept] has to be '.csv' to indicate that only CSV files should be shown to the user for selection. 
  • (change) willcall the importDataFromCSV function with a parameter $event that has properties with the contents of the file.

The $event property carries the file inside event.target.files, where files is an array. Contents of the file can be extracted in different formats. For our use case, we’ll be extracting text() from the file. Remember, the method for reading the file should asynchronous. Which is why, we will use await and async.  The simple piece of code for reading text from a CSV file is here:

Once text content is extracted from the CSV file, we can use the service defined in step 2. 

And, the above function can be linked with a button in app.component.html.

5. Integrating the Whole Application

Now, it is time to build app.module.ts. This is where all providers, imports, declarations and the bootstrap component will be registered. 

With this change, you will be able to see the following screen.

Live Demo

See the Pen
Javascript Infinite Scrolling
by DDev (@divyaddev)
on CodePen.

Conclusion

In this post, you saw how to upload and download CSV data, and how to parse CSV data into and out of a text file. 


This content originally appeared on Envato Tuts+ Tutorials and was authored by Divya Dev

Datasets are extremely essential in building API models, and various business processes. This is why importing and exporting CSV is an often-needed functionality.

In this tutorial you will learn now to download and import a CSV file within an Angular app. We'll be working with a CSV file that contains employee details. The code will be written in TypeScript. 

Before you start, you'll need to set up a new Angular environment, create a new workspace with an initial application and start serving. A step-by-step guide to creating the boilerplate for your Angular app can be found in the official tutorial.

Here's the folder structure of the boilerplate application:

1. Create a Simple Model

Firstly, let's create a simple class to model the employee. This model will be consumed by the Angular components.

The model for holding the employee details is given below. This is just a basic model with the employee's name, email and city. 

2. Build the Service

Angular applications have a modular structure. This makes the application robust, and easy to maintain. One of the primary rules of any Angular application is that components must not be allowed to save or fetch data directly. That is why you have to make use of services to access, and present data. In our use case, we require a service for downloading, and importing CSV data.

Our service file is csv.services.ts

Once the service is created, it can be injected into any component. Before making the CSV.service available for injection, it needs to be registered in app.module.ts as a provider.

In this demo, we will be coding two different types of functionalities:

  • saving data into a CSV
  • importing data from a CSV

Method to Save Data Into a CSV

Firstly, let us deal with simplest functionality. Saving data into a CSV involves reading an array of objects. Objects in Javascript come with properties that have a value. We will read all these properties for each object and join them with a comma. The below line of code will achieve this:

Once the titles are joined, they will be appended as the first row. Following which, a loop will run through the array and read all the properties and its values. These values will be appended into the CSV file. 

The complete piece of code, for building the header, reading the contents of the array, and building the CSV is given below.

Method to Import Data from a CSV

Our next service is to import data from a CSV. For this, we will define a function that would take the contents of a file in the form of a string. The property names, and data rows will be created from the contents of the file. 

With the above lines of code, we will have both the property names and data. With this information, we are only left with the job of printing the contents of the CSV. Code for printing the contents is given below:

4. Building the UI Elements

Next, we are going to build the HTML elements for importing, and exporting the CSV files. 

The file for UI elements is app.component.html.

Saving Contents of a CSV 

To save a CSV file, we will be using a very traditional way of downloading files in JavaScript. 

  1. Create a hidden anchor element <a>.
  2. Set the href type of this anchor element to data:text/csv.
  3. Automatically trigger the click event.

The complete piece of code, for downloading the CSV file is given below. This code will be in app.component.ts.

Here's some sample data we can passed into the above function. As you can see, we are using the model created in step 1.

Next, we shall link the above functionality to a button that will be displayed on the screen for us to click. This will be entered in app.component.html.

Reading the Contents of a CSV

It is quite simple to open and read files in HTML using the <input> tag. The input tag comes with properties like type, [accept] and (change).

  • type has to be 'file' to indicate that a file will be chosen by the user. 
  • [accept] has to be '.csv' to indicate that only CSV files should be shown to the user for selection. 
  • (change) willcall the importDataFromCSV function with a parameter $event that has properties with the contents of the file.

The $event property carries the file inside event.target.files, where files is an array. Contents of the file can be extracted in different formats. For our use case, we'll be extracting text() from the file. Remember, the method for reading the file should asynchronous. Which is why, we will use await and async.  The simple piece of code for reading text from a CSV file is here:

Once text content is extracted from the CSV file, we can use the service defined in step 2. 

And, the above function can be linked with a button in app.component.html.

5. Integrating the Whole Application

Now, it is time to build app.module.ts. This is where all providers, imports, declarations and the bootstrap component will be registered. 

With this change, you will be able to see the following screen.

Live Demo

Conclusion

In this post, you saw how to upload and download CSV data, and how to parse CSV data into and out of a text file. 


This content originally appeared on Envato Tuts+ Tutorials and was authored by Divya Dev


Print Share Comment Cite Upload Translate Updates
APA

Divya Dev | Sciencx (2018-07-09T18:10:09+00:00) How to Upload and Download CSV Files With Angular. Retrieved from https://www.scien.cx/2018/07/09/how-to-upload-and-download-csv-files-with-angular/

MLA
" » How to Upload and Download CSV Files With Angular." Divya Dev | Sciencx - Monday July 9, 2018, https://www.scien.cx/2018/07/09/how-to-upload-and-download-csv-files-with-angular/
HARVARD
Divya Dev | Sciencx Monday July 9, 2018 » How to Upload and Download CSV Files With Angular., viewed ,<https://www.scien.cx/2018/07/09/how-to-upload-and-download-csv-files-with-angular/>
VANCOUVER
Divya Dev | Sciencx - » How to Upload and Download CSV Files With Angular. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2018/07/09/how-to-upload-and-download-csv-files-with-angular/
CHICAGO
" » How to Upload and Download CSV Files With Angular." Divya Dev | Sciencx - Accessed . https://www.scien.cx/2018/07/09/how-to-upload-and-download-csv-files-with-angular/
IEEE
" » How to Upload and Download CSV Files With Angular." Divya Dev | Sciencx [Online]. Available: https://www.scien.cx/2018/07/09/how-to-upload-and-download-csv-files-with-angular/. [Accessed: ]
rf:citation
» How to Upload and Download CSV Files With Angular | Divya Dev | Sciencx | https://www.scien.cx/2018/07/09/how-to-upload-and-download-csv-files-with-angular/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.