Ishan Malik
Ishan Malik

Reputation: 19

What to write in module - Module not found: Error: Can't resolve 'xlsx' in angular

I'm trying to use xlsx library in my project but I'm continuously getting a following error

Module not found: Error: Can't resolve 'xlsx'.

I know, I have to declare in app.module but what would I declare here Here is my code; please guide me

This is component file -

import { Component, EventEmitter, OnInit } from "@angular/core";
import { Router } from "@angular/router";
import { FileUploader } from "ng2-file-upload";
import * as XLSX from "xlsx";

@Component({
selector: "import-user",
templateUrl: "import-user.component.html"
})
export class ImportUserComponent implements OnInit {

public importResults: any[];
public errorMessage: string;
static reader: FileReader = new FileReader();

ngOnInit(): void {

}

public uploader: FileUploader = new FileUploader({
    url: this.getUploadUrl(),
    disableMultipart: false,
    autoUpload: false,
    removeAfterUpload: false,
    method: 'post',
    allowedFileType: ['.xlsx', '.xls', '.csv'],
});

constructor(private router: Router) {
}

private initializeUploader() {
    this.uploader.onErrorItem = (item, response) => { this.errorMessage = response; };
}

public onFileSelected(event: EventEmitter<File[]>) {
    this.errorMessage = '';
    const file: File = event[0];
    console.log(file);
}

public getXLSXRequest(file: any){
    var wb = XLSX.readFile(file);
  }

}

This is module file

import { FormsModule, ReactiveFormsModule } from "@angular/forms";
import { BrowserModule } from '@angular/platform-browser';
import { CommonModule } from "@angular/common";
import { NgModule } from "@angular/core";
import { routing } from "./routing";
import { FileUploadModule } from "ng2-file-upload";
import { ImportUserComponent } from "./components/import-user.component";

@NgModule({
imports: [
    CommonModule,
    routing,
    SharedModule,
    FormsModule,
    PortalModuleExports,
    FileUploadModule,
    BrowserModule
],
declarations: [
    ImportUserComponent
],
providers: [

]
})
export class AdministrationModule {
}

Here is the package as well.

package file

Upvotes: 0

Views: 8595

Answers (2)

Misael C. Homem
Misael C. Homem

Reputation: 121

I also faced the same problem, referring to the website below:

https://www.c-sharpcorner.com/article/xlsx-file-read-in-angular/

You just need after running the npm i xlsx

In component of your Angular project, just include this import, no need to declare in app.module.ts

import * as XLSX from 'xlsx';

[EDIT]

Follow this tutorial here

https://medium.com/@madhavmahesh/exporting-an-excel-file-in-angular-927756ac9857

I manage to get the implementation successful, you just have to apply two more additional commands to install the file-saver package and its types:

npm i file-saver --save npm install @types/file-saver --save

Upvotes: 0

axtck
axtck

Reputation: 3965

Have you installed the library itself? You can check your dependencies in your package.json file.

Otherwise run:

npm install xlsx

Upvotes: 0

Related Questions