在模块中写入什么 - 找不到模块:错误:无法解析“xlsx”有角度的

发布于 2025-01-17 23:14:12 字数 2091 浏览 3 评论 0原文

我正在尝试在我的项目中使用 xlsx 库,但我不断收到以下错误

找不到模块:错误:无法解析“xlsx”。

我知道,我必须在 app.module 中声明,但我会在这里声明什么 这是我的代码;请指导我

这是组件文件 -

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);
  }

}

这是模块文件

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 {
}

这也是包。

包文件

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

初见你 2025-01-24 23:14:12

您是否安装了图书馆本身?您可以在 package.json 文件中查看依赖项。

否则运行:

NPM安装XLSX

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

Otherwise run:

npm install xlsx

つ可否回来 2025-01-24 23:14:12

我也遇到了同样的问题,参考下面的网站:

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

运行后您只需要
npm i xlsx

在 Angular 项目的组件中,只需包含此导入,无需在 app.module.ts 中声明

import * as XLSX from 'xlsx';

[编辑]

在此遵循本教程

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

我设法成功实施,您只需应用另外两个命令即可安装file-saver 包及其类型:

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文