3

Am getting this while making build using jenkins on build server but i am trying this on local machine then it working fine with error

15:07:39 "",
15:07:39 "",
15:07:39 "ERROR in src/services/excel.service.ts:2:23 - error TS2307: Cannot find module 'xlsx'.",
15:07:39 "",
15:07:39 "2 import * as XLSX from 'xlsx';",
15:07:39 " ~~~~~~"
15:07:39 

install xlsx using npm install xlsx

And import xlsx module. import * as XLSX from 'xlsx';

import { Injectable } from '@angular/core';
import * as XLSX from 'xlsx';
import * as _ from 'lodash';

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

  constructor() { }
  wopts: XLSX.WritingOptions = { bookType: 'xlsx', type: 'array' };
  exportAsExcelFile(json: any, fileName:any): void {
    /* generate worksheet */
    // const ws: XLSX.WorkSheet = XLSX.utils.aoa_to_sheet(this.data);
    const ws: XLSX.WorkSheet = XLSX.utils.json_to_sheet(json);

    /* generate workbook and add the worksheet */
    const wb: XLSX.WorkBook = XLSX.utils.book_new();
    XLSX.utils.book_append_sheet(wb, ws, 'Sheet1');

    /* save to file */
    XLSX.writeFile(wb, fileName);
  }

}
Trott
  • 66,479
  • 23
  • 173
  • 212
harkesh kumar
  • 833
  • 2
  • 13
  • 35

3 Answers3

6

import xlsx from 'xlsx/xlsx';

This line works for me on node/typescript environment.

2

I resolved the error by this line:

import * as XLSX from "xlsx/xlsx";
Tyler2P
  • 2,324
  • 26
  • 22
  • 31
1

I got the same error message. I was running xlsx 18.5, so I removed it and reloaded 17.1 and everything works fine now with a standard

import XLSX from 'xlsx'

No particular reason for 17.1 except it was working fine in another app and I wanted to remove that as a possibility.

Bill
  • 1,423
  • 2
  • 27
  • 51