我正在尝试创建一个示例excel文件,但我收到错误'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 :(得分:0)
从“exceljs / dist / exceljs.js”导入*作为Excel;
要从javascript文件导入typescript,您需要使用库的类型定义文件。
可在npm中找到。
执行:
npm i @types/exceljs --save-dev
为了导入:
import * as Excel from 'exceljs';
您可以在“DefinitelyTyped”repo
中检查测试文件中的示例答案 1 :(得分: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;
}
}