Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to use SheetJS (js-xlsx) in angular 2

Tags:

angular

xlsx

I'm learning angular2 and i wanted to use js-xlsx library in my project.

I installed xlsx npm install xlsx and jszip npm install jszip and added them in my index.html

<script src="node_modules/xlsx/dist/xlsx.core.min.js"></script>

<script src="node_modules/jszip/dist/jszip.min.js"></script>

and added the typescript defitions tsd install xlsx

and I'm using webpack

but when I used it in

import * as xlsx from 'xlsx';

but i get error module build failed: error: cannot resolve module 'xlsx'

like image 882
Maged Milad Avatar asked Aug 06 '16 12:08

Maged Milad


2 Answers

The following is a working component who exports an xlsx file from an array of objects using js-xlsx lib on Angular 2/4:

import { Component, OnInit } from '@angular/core';

import { utils, write, WorkBook } from 'xlsx';

import { saveAs } from 'file-saver';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
  title = 'app';
  table = [
    {
      First: 'one',
      Second: 'two',
      Third: 'three',
      Forth: 'four',
      Fifth: 'five'
    },
    {
      First: 'un',
      Second: 'deux',
      Third: 'trois',
      Forth: 'quatre',
      Fifth: 'cinq'
    },
  ];

  ngOnInit() {
    const ws_name = 'SomeSheet';
    const wb: WorkBook = { SheetNames: [], Sheets: {} };
    const ws: any = utils.json_to_sheet(this.table);
    wb.SheetNames.push(ws_name);
    wb.Sheets[ws_name] = ws;
    const wbout = write(wb, { bookType: 'xlsx', bookSST: true, type: 
'binary' });

function s2ab(s) {
  const buf = new ArrayBuffer(s.length);
  const view = new Uint8Array(buf);
  for (let i = 0; i !== s.length; ++i) {
    view[i] = s.charCodeAt(i) & 0xFF;
  };
  return buf;
}

saveAs(new Blob([s2ab(wbout)], { type: 'application/octet-stream' }), 'exported.xlsx');
  }
}

You have to install xlsx and file-saver for this to work.

For the working example on exporting a xlsx file from an array of jsons on Angular2/4:

https://github.com/bogdancar/xlsx-json-to-xlsx-demo-Angular2

like image 89
BogdanC Avatar answered Oct 21 '22 22:10

BogdanC


An easier method would be not using typings at all:

  1. Add xlsx.core.min.js to your index.html file as you did. (I'm using angular-cli so my js files get copied to the dist/vendor directory)

    <script src="vendor/xlsx/dist/xlsx.core.min.js"></script>

  2. In your module, do not use import but declare XLSX under your imports block.

    declare var XLSX: any;

If you're using angular-cli, you will need to add xlsx to your angular-cli-build.js file.

module.exports = function(defaults) {
  return new Angular2App(defaults, {
    vendorNpmFiles: [
      'systemjs/dist/system-polyfills.js',
      'systemjs/dist/system.src.js',
      'zone.js/dist/**/*.+(js|js.map)',
      'es6-shim/es6-shim.js',
      'reflect-metadata/**/*.+(ts|js|js.map)',
      'rxjs/**/*.+(js|js.map)',
      '@angular/**/*.+(js|js.map)',
      'moment/moment.js',
       ....
      'xlsx/**/*.+(js|js.map)'
    ]})
};
like image 21
Roy S. Avatar answered Oct 21 '22 21:10

Roy S.