6

ExcelJS Export Not Working in Angular 6 Production Environment, but works fine in development environment.

I am using "exceljs": "^1.13.0" with "file-saver": "^2.0.2". So far I have tried updating:

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

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

Additionally, I've tried two different import sets:

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

and

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

I have also tried adding:

declare const ExcelJS: any;

Here is the excel service.

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

  }

//end of class }

My exception is for the spreadsheet to download in the browser upon the completion of the method in my Excel Service in production, which is does in the development environment.

Steve Klock
  • 115
  • 1
  • 8

3 Answers3

6
  • Import like below, observed that you have made it Excel instead of ExcelJS

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

  • Keep this code declare const ExcelJS: any; as it is

  • Keep "node_modules/exceljs/dist/exceljs.min.js" in the script path of angular.json file

  • Remove ExcelProper import - Not Required
  • Wherever you are creating WorkBook, use ExcelJS.Workbook() instead of Excel.Workbook()
Arabinda Nanda
  • 209
  • 1
  • 4
  • 14
0
  • The latest version of exceljs@1.13.0 (any version > 1.10.0) works fine in development configuration but does not work in production configuration

  • So downgrading the 'exceljs' npm package version to exceljs@1.10.0 will solve the problem

  • Here is the GitHub issue link for reference : https://github.com/exceljs/exceljs/issues/871#issue-459504298

Megh09
  • 61
  • 5
0
  • I got this issue in Sharepoint-spfx.
  • For solve it- use the version exceljs version 4.0.1. using npm install exceljs@4.0.1.
Kaushik
  • 1
  • 2