在模块中写入什么 - 找不到模块:错误:无法解析“xlsx”有角度的
我正在尝试在我的项目中使用 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您是否安装了图书馆本身?您可以在 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
我也遇到了同样的问题,参考下面的网站:
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