You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

NestJS集成ExcelJs报错:Cannot read properties of undefined (reading 'Workbook')

NestJS中使用ExcelJs创建Workbook报错的解决方案

问题重现

运行代码时出现以下错误:

const workbook = new Excel.Workbook();
^
TypeError: Cannot read properties of undefined (reading 'Workbook')

相关代码及依赖情况:

  • package.json依赖:"exceljs": "^4.3.0"
  • 核心实现代码:
import { Injectable } from '@nestjs/common';
import { PanelDto } from 'src/panel/dto/panel.dto';
import { PanelService } from 'src/panel/panel.service';
import * as path from 'path';
import Excel from 'exceljs';

@Injectable()
export class ExcelService {

  constructor(private panelService: PanelService) {}

  async createExcel(panelId: number) {
    const panel: PanelDto = await this.panelService.getOnePanelById(panelId);
    const unswers = panel.unswers;

    const workbook = new Excel.Workbook();

    const worksheet = workbook.addWorksheet(
      'Questionaire_' + panel.questionaire.name,
    );

    const worksheet = workbook.addWorksheet(
      'Questionaire_' + panel.questionaire.name,
    );

    const columns: Column[] = [
      { key: 'questionId', header: 'Question ID' },
      { key: 'question', header: 'Question' },
      { key: 'unswerId', header: 'Unswer ID' },
      { key: 'unswer', header: 'Unswer' },
    ];
    worksheet.columns = columns;

    unswers.forEach((u) => {
      const row: RowData = {
        questiondId: u.question.id,
        question: u.question.question,
        unswerId: u.id,
        unswer: u.unswer,
      };
      worksheet.addRow(row);
    });

    const exportPath = path.resolve(__dirname, 'questionaire.xlsx');

    await workbook.xlsx.writeFile(exportPath);
  }
}

interface RowData {
  questiondId: number;
  question: string;
  unswerId: number;
  unswer: string;
}

interface Column {
  key: string;
  header: string;
}

解决方案

1. 修正ExcelJS导入方式

ExcelJS v4版本的默认导出为ES模块,原导入方式无法正确获取到Workbook类,需调整为以下两种方式之一:

  • 方式一:使用命名空间导入
import * as Excel from 'exceljs';
  • 方式二:直接解构导入Workbook
import { Workbook } from 'exceljs';
// 后续直接使用 new Workbook()

2. 移除重复的worksheet声明

代码中重复声明了worksheet变量,会触发语法错误,需删除其中一行重复代码:

// 保留一行即可
const worksheet = workbook.addWorksheet(
  'Questionaire_' + panel.questionaire.name,
);

3. 修正RowData字段拼写错误

RowData中的questiondId拼写错误,与columns中的questionId不匹配,会导致数据无法正确映射到列,需修正为:

interface RowData {
  questionId: number; // 修正拼写
  question: string;
  unswerId: number;
  unswer: string;
}

// 对应addRow中的字段也要修正
const row: RowData = {
  questionId: u.question.id,
  question: u.question.question,
  unswerId: u.id,
  unswer: u.unswer,
};

4. 验证依赖安装(可选)

如果上述调整后仍有问题,可重新安装依赖确保版本正确:

npm install exceljs@4.3.0
# 或使用yarn
yarn add exceljs@4.3.0

修正后的完整代码

import { Injectable } from '@nestjs/common';
import { PanelDto } from 'src/panel/dto/panel.dto';
import { PanelService } from 'src/panel/panel.service';
import * as path from 'path';
import * as Excel from 'exceljs';

@Injectable()
export class ExcelService {

  constructor(private panelService: PanelService) {}

  async createExcel(panelId: number) {
    const panel: PanelDto = await this.panelService.getOnePanelById(panelId);
    const unswers = panel.unswers;

    const workbook = new Excel.Workbook();

    const worksheet = workbook.addWorksheet(
      'Questionaire_' + panel.questionaire.name,
    );

    const columns: Column[] = [
      { key: 'questionId', header: 'Question ID' },
      { key: 'question', header: 'Question' },
      { key: 'unswerId', header: 'Unswer ID' },
      { key: 'unswer', header: 'Unswer' },
    ];
    worksheet.columns = columns;

    unswers.forEach((u) => {
      const row: RowData = {
        questionId: u.question.id,
        question: u.question.question,
        unswerId: u.id,
        unswer: u.unswer,
      };
      worksheet.addRow(row);
    });

    const exportPath = path.resolve(__dirname, 'questionaire.xlsx');

    await workbook.xlsx.writeFile(exportPath);
  }
}

interface RowData {
  questionId: number;
  question: string;
  unswerId: number;
  unswer: string;
}

interface Column {
  key: string;
  header: string;
}

内容的提问来源于stack exchange,提问作者Andrea Mazzarotto

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.30 12:59:15