How to Upload Excel Data in Angular: A Step-by-Step Guide

Chandan Kumar
2 min readAug 22, 2024

--

In today’s data-driven world, the ability to upload and process Excel files is a common requirement for many web applications. Angular, with its robust ecosystem, provides an excellent platform for handling such tasks. In this blog post, we’ll walk through the process of uploading and processing Excel data in an Angular application.

Prerequisites

Before we begin, make sure you have the following:

  1. Angular CLI installed
  2. A basic understanding of Angular and TypeScript
  3. Node.js and npm installed on your system

Step 1: Set Up Your Angular Project

If you haven’t already, create a new Angular project:

ng new excel-upload-app
cd excel-upload-app

Step 2: Install Required Libraries

We’ll use the xlsx library to read Excel files. Install it using npm:

npm install xlsx

Step 3: Create a Component

Generate a new component for our Excel upload functionality:

ng generate component excel-upload

Step 4: Implement the Upload Functionality

Open src/app/excel-upload/excel-upload.component.ts and add the following code:

import { Component } from '@angular/core';
import * as XLSX from 'xlsx';

@Component({
selector: 'app-excel-upload',
templateUrl: './excel-upload.component.html',
styleUrls: ['./excel-upload.component.css']
})
export class ExcelUploadComponent {
excelData: any[];

onFileChange(event: any) {
const file = event.target.files[0];
const reader = new FileReader();
reader.onload = (e: any) => {
const workbook = XLSX.read(e.target.result, { type: 'binary' });
const firstSheetName = workbook.SheetNames[0];
const worksheet = workbook.Sheets[firstSheetName];
this.excelData = XLSX.utils.sheet_to_json(worksheet, { raw: true });
console.log('Excel data:', this.excelData);
};
reader.readAsBinaryString(file);
}
}

Step 5: Create the Template

Open src/app/excel-upload/excel-upload.component.html and add the following HTML:

<div>
<input type="file" (change)="onFileChange($event)" accept=".xlsx, .xls">
</div>

<div *ngIf="excelData">
<h3>Uploaded Data:</h3>
<pre>{{ excelData | json }}</pre>
</div>

Step 6: Update the App Component

Open src/app/app.component.html and add the Excel upload component:

<h1>Excel Upload Demo</h1>
<app-excel-upload></app-excel-upload>

Step 7: Run the Application

Start your Angular application:

ng serve

Navigate to http://localhost:4200 in your browser. You should see a file input where you can select an Excel file to upload.

How It Works

  1. We use an <input type="file"> element to allow users to select an Excel file.
  2. When a file is selected, the onFileChange method is triggered.
  3. We use a FileReader to read the file contents.
  4. The xlsx library is used to parse the Excel data.
  5. We convert the worksheet to JSON format using XLSX.utils.sheet_to_json.
  6. The parsed data is stored in the excelData property and displayed on the page.

Conclusion

This simple implementation allows you to upload and display Excel data in your Angular application. From here, you can further process the data, send it to a server, or perform any other required operations.

Remember to handle errors and edge cases in a production environment. You might also want to add features like progress indicators for large files or support for multiple sheets.

By leveraging Angular’s powerful features and the xlsx library, you can create robust Excel upload functionality in your web applications with ease.

--

--

Chandan Kumar
Chandan Kumar

Written by Chandan Kumar

Software Engineer | CS-Engineering Graduate | Mean Stack Developer | @Jobluu https://www.linkedin.com/in/developerchandan/

No responses yet