Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 596
Steve Klock Avatar asked Jul 11 '19 20:07

Steve Klock


1 Answers

  • 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()
like image 105
user3751824 Avatar answered Dec 24 '22 02:12

user3751824