Search code examples
angular6exceljs

How to use exceljs in angular 6


When I tried to use exceljs in Angular-6 like so

import * as excel from 'exceljs'

createExcel() {
  let workbook = new excel.Workbook()
}

even I just initialize a class and got this error. If I comment out this line let workbook = new excel.Workbook() then error gone

./node_modules/fs-constants/browser.js Module not found: Error: Can't resolve 'constants' in 'D:\Developer\spire-client\node_modules\fs-constants'

enter image description here


Solution

  • You could import it as follow

    import * as Excel from 'exceljs';
    

    after that, you can use Exceljs:

    const myWorkbook = new Excel.Workbook()
    

    or

    import * as Excel from "exceljs/dist/exceljs.min.js";
    import * as ExcelProper from "exceljs";
    
    let workbook: ExcelProper.Workbook = new Excel.Workbook();
    

    As long as you just use the import from exceljs for type definitions and only use functions from the import from exceljs/dist/exceljs.min.js, the app will run just fine and you still get type safety.

    ref: https://www.npmjs.com/package/exceljs#create-a-workbook https://github.com/guyonroche/exceljs/issues/348#issuecomment-320690232