protobi / js-xlsx

XLSX / XLSM / XLSB (Excel 2007+ Spreadsheet) / ODS parser and writer
http://oss.sheetjs.com/js-xlsx
Other
814 stars 416 forks source link

Unable to import into ionic project #99

Open Ahmad19860 opened 6 years ago

Ahmad19860 commented 6 years ago

Hi, I created an excel-sheet using sheetJS library. But styling is not reflecting in the sheet. So I am trying to use xlsx-style into my ionic project to create excel-sheet with cell style. I imported it using CLI, but getting the error "Error: ./node_modules/xlsx-style/dist/cpexcel.js Module not found: Error: Can't resolve './cptable'"

Please suggest a link or sample code to achieve the goal. Here is my code-

import { Component } from '@angular/core';
import { NavController, Platform } from 'ionic-angular';
import * as XLSX from 'xlsx-style';
import { File } from '@ionic-native/file';
declare var cordova: any;
declare var window;

@Component({
  selector: 'page-home',
  templateUrl: 'home.html'
})
export class HomePage {

  constructor(public navCtrl: NavController, platform: Platform, public file: File) {
    var objects = [["Header 1", "Header 2", "Header 3"], ["Value 1 1", "Value 1 2", "Value 1 3"], ["Value 2 1", "Value 2 2", "Value 2 3"]];

    platform.ready().then(() => {
      console.log(cordova.file.externalCacheDirectory + "report.xlsx");
      this.createXSLX(objects);
    });
  }

  createXSLX(data: any) {

    var pathFile = "";
    var fileName = "report.xlsx";
    let ws_name = "OrderDetails";

    let wb: XLSX.WorkBook = {
      SheetNames: [],
      Sheets: {},
      Props: {}
    };
    let ws = this.sheet_from_array_of_arrays(data, {});

    /* add worksheet to workbook */
    wb.SheetNames.push(ws_name);
    wb.Sheets[ws_name] = ws;    
    let wbout = XLSX.write(wb, { bookType: 'xlsx', type: 'binary' });

    let xslxBlob = new Blob([this.s2ab(wbout)], { type: "application/octet-stream" });
    pathFile = cordova.file.externalCacheDirectory;
    this.file.writeFile(pathFile, fileName, xslxBlob);
  }

  datenum(v, date1904): any {
    if (date1904) v += 1462;
    let epoch: any = Date.parse(v);
    return (epoch - new Date(Date.UTC(1899, 11, 30)).getTime()) / (24 * 60 * 60 * 1000);
  }

  sheet_from_array_of_arrays(data, opts) {
    let ws = {};
    let range = { s: { c: 10000000, r: 10000000 }, e: { c: 0, r: 0 } };
    for (let R = 0; R != data.length; ++R) {
      for (let C = 0; C != data[R].length; ++C) {
        if (range.s.r > R) range.s.r = R;
        if (range.s.c > C) range.s.c = C;
        if (range.e.r < R) range.e.r = R;
        if (range.e.c < C) range.e.c = C;
        let cell: any = { v: data[R][C] };
        if (cell.v == null) continue;
        let cell_ref = XLSX.utils.encode_cell({ c: C, r: R });

        if (typeof cell.v === 'number') cell.t = 'n';
        else if (typeof cell.v === 'boolean') cell.t = 'b';
        else if (cell.v instanceof Date) {
          cell.t = 'n';
          //cell.z = XLSX.SSF._table[14];
          cell.v = this.datenum(cell.v, null);
        }
        else cell.t = 's';

        if (C == 0) {
          cell.s = {
            font: {
              sz: '16',
              bold: true
            }
          }
        }        

        if (R == 0) {
          cell.s = {
            fill: {
              fgColor: { rgb: "FFFFAA00" }
            }
          }
        }

        ws[cell_ref] = cell;
      }
    }
    if (range.s.c < 10000000) ws['!ref'] = XLSX.utils.encode_range(range);
    return ws;
  }

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

}