2017-12-18 167 views
1

我想創建一個示例excel文件,但我得到錯誤'fs.createWriteStream不是一個函數'。下面 是代碼來創建文件 -Ionic | TypeError | fs.createWriteStream不是函數

import { Component } from '@angular/core'; 
import { NavController, Platform } from 'ionic-angular'; 
import * as Excel from "exceljs/dist/exceljs.js"; 

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

    constructor(platform: Platform) { 
    platform.ready().then(() => { 
     this.createSheetUsingExcelJs(); 
    }); 
    } 

    ionViewDidLoad() { 
    } 

    createSheetUsingExcelJs() { 
    let workbook = new Excel.Workbook(); 
    var worksheet = workbook.addWorksheet('My Sheet'); 

    worksheet.columns = [ 
     { header: 'Id', key: 'id', width: 10 }, 
     { header: 'Name', key: 'name', width: 32 }, 
     { header: 'D.O.B.', key: 'DOB', width: 10 } 
    ]; 
    worksheet.addRow({ id: 1, name: 'Ionic Android', dob: new Date(1970, 1, 1) }); 
    worksheet.addRow({ id: 2, name: 'Ionic iOS', dob: new Date(1965, 1, 7) }); 
    var tempFilePath = 'C:/Users/mahmad/Downloads/temp.xlsx'; 
    console.log(workbook); 

    workbook.xlsx.writeFile('temp.xlsx').then(function() { 
     console.log('file is written'); 
    }); 
    } 
} 

請提出一個解決方案來解決這個問題

回答

0

import * as Excel from "exceljs/dist/exceljs.js";

從JavaScript文件導入打字稿,您需要使用圖書館的類型定義文件。

它在npm可用。

務必:

npm i @types/exceljs --save-dev 

爲了導入:

import * as Excel from 'exceljs'; 

您可以在 「DefinitelyTyped」 測試文件repo

+0

我試着用命令,但現在我得到錯誤「遺漏的類型錯誤:無法讀取屬性未定義的‘原型’」。 – user320676

+0

任何想法它扔在哪裏?也許值得提出一個github問題請求,如果問題與lib –

+0

它被拋出啓動屏幕上。即使我的主頁構造函數沒有調用。 – user320676

0

最後,任務是通過使用ts-xlsx library做檢查樣本。 下面是我的代碼 -

import { Component, Injectable } from '@angular/core'; 
import { NavController, Platform } from 'ionic-angular'; 
import * as XLSX from 'ts-xlsx'; 
import { File } from '@ionic-native/file'; 
import { EmailComposer } from '@ionic-native/email-composer'; 

declare var cordova: any; 
declare var window; 

@Component({ 
    selector: 'page-home', 
    templateUrl: 'home.html', 
    providers: [File] 
}) 

@Injectable() 
export class HomePage { 

    sheetNames: string[] = [] 
    sheets: any; 

    constructor(public emailComposer: EmailComposer, 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); 
     this.sendEmail(cordova.file.externalCacheDirectory + "report.xlsx"); 
    }); 
    } 

    createXSLX(data: any) { 

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

    let wb: XLSX.IWorkBook = { 
     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); 
    }  

    sendEmail(data) {  
    this.emailComposer.isAvailable().then((available: boolean) => { 
     if (available) { 
     //Now we know we can send 
     } 
    }); 

    let email = { 
     to: '', 
     attachments: [data], 
     subject: 'XLSX File', 
     body: 'How are you? Nice greetings from Leipzig', 
     isHtml: true 
    }; 

    // Send a text message using default options 
    this.emailComposer.open(email); 
    } 


    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'; 

     ws[cell_ref] = cell; 
     } 
    } 
    if (range.s.c < 10000000) ws['!ref'] = XLSX.utils.encode_range(range.s, range.e); 
    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; 
    } 
} 
相關問題