Steve Klock
Steve Klock

Reputation: 125

ExcelJS Not Working In Production in Angular 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.

Upvotes: 6

Views: 5443

Answers (3)

Kaushik
Kaushik

Reputation: 1

  • I got this issue in Sharepoint-spfx.
  • For solve it- use the version exceljs version 4.0.1. using npm install [email protected].

Upvotes: 0

Megh09
Megh09

Reputation: 61

Upvotes: 0

Arabinda Nanda
Arabinda Nanda

Reputation: 209

  • 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()

Upvotes: 6

Related Questions