【问题标题】:ExcelJS Not Working In Production in Angular 6ExcelJS 在 Angular 6 中无法用于生产
【发布时间】:2019-07-11 20:22:02
【问题描述】:

ExcelJS 导出在 Angular 6 生产环境中不工作,但在开发环境中工作正常。

我正在使用“exceljs”:“^1.13.0”和“file-saver”:“^2.0.2”。到目前为止,我已经尝试过更新:

angular.json scripts with "node_modules/exceljs/dist/exceljs.min.js",

tsconfig.json with "paths": {"exceljs": ["node_modules/exceljs/dist/exceljs.min"].

此外,我尝试了两种不同的导入集:

import * as Excel from 'exceljs/dist/exceljs.min.js';
import * as FileSaver from 'file-saver';

import * as Excel from "exceljs/dist/exceljs.min.js";
import * as ExcelProper from "exceljs";
import * as FileSaver from 'file-saver';

我也试过添加:

declare const ExcelJS: any;

这里是excel服务。

import { Injectable } from '@angular/core';
import * as Excel from "exceljs/dist/exceljs.min.js";
import * as ExcelProper from "exceljs";
import * as FileSaver from 'file-saver';

const EXCEL_TYPE = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=UTF-8';
const EXCEL_EXTENSION = '.xlsx';

// declare const ExcelJS: any;

@Injectable({
  providedIn: 'root'
})
export class ExcelService {

    workbook: Excel.Workbook;
  worksheet: any;

  constructor() { }


  generateExcel() {

    // Create workbook and worksheet
    this.workbook = new Excel.Workbook();

    // Add a Worksheet
    this.worksheet = this.workbook.addWorksheet('File');


    //Add Test to cell A1
    this.worksheet.getCell('A1').value = 'TEST';

    // Generate Excel File
    this.workbook.xlsx.writeBuffer().then((data) => {
        console.log('buffer data', data);
        const blob = new Blob([data], {type: EXCEL_TYPE});
        console.log('blob', blob);
      FileSaver.saveAs(blob, 'quote.xlsx');
    });

  }

//课程结束 }

我的例外是电子表格在我的生产环境中的 Excel 服务中完成该方法后在浏览器中下载,这在开发环境中也是如此。

【问题讨论】:

    标签: angular6 production-environment filesaver.js exceljs


    【解决方案1】:
    • 如下所示导入,观察到您已将其设为 Excel 而不是 ExcelJS

    import * as ExcelJS from "exceljs/dist/exceljs.min.js";

    • 保留此代码 declare const ExcelJS: any; 原样

    • "node_modules/exceljs/dist/exceljs.min.js"保留在angular.json文件的脚本路径中

    • 删除 ExcelProper 导入 - 不需要
    • 无论您在何处创建 WorkBook,请使用 ExcelJS.Workbook() 而不是 Excel.Workbook()

    【讨论】:

    • 谢谢,我的朋友。来自巴西的赞美
    • 太棒了,一年后它确实帮助了我:D
    【解决方案2】:
    • exceljs@1.13.0 的最新版本(任何版本> 1.10.0)development 配置下工作正常,但在production 配置下不工作 p>

    • 所以将 'exceljs' npm 包版本降级为exceljs@1.10.0 即可解决问题

    • 这里是GitHub问题链接供参考:https://github.com/exceljs/exceljs/issues/871#issue-459504298

    【讨论】:

      【解决方案3】:
      • 我在 Sharepoint-spfx 中遇到了这个问题。
      • 为了解决它 - 使用版本 exceljs 版本 4.0.1。使用 npm 安装 exceljs@4.0.1。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-12-15
        • 1970-01-01
        • 1970-01-01
        • 2014-01-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-08-24
        相关资源
        最近更新 更多