UNPKG

yoyo-ng-modulewindy

Version:

服务于52ABP模板的前端开源的相关组件内容。整合了ng-alain和你NG ZORRO的内容

94 lines 3.8 kB
import { Injectable, Inject } from '@angular/core'; import { HttpClient } from '@angular/common/http'; import { saveAs } from 'file-saver'; import { LazyService } from 'yoyo-ng-module/util'; import { DA_XLSX_CONFIG, } from './interface'; var XlsxService = /** @class */ (function () { function XlsxService(config, http, lazy) { this.config = config; this.http = http; this.lazy = lazy; } XlsxService.prototype.init = function () { var config = Object.assign({ url: "//cdn.bootcss.com/xlsx/0.12.12/xlsx.full.min.js", modules: [], }, this.config); return this.lazy.load([config.url].concat(config.modules)); }; XlsxService.prototype.read = function (wb) { var ret = {}; wb.SheetNames.forEach(function (name) { var sheet = wb.Sheets[name]; ret[name] = XLSX.utils.sheet_to_json(sheet, { header: 1 }); }); return ret; }; /** * 导入Excel并输出JSON,支持 `<input type="file">`、URL 形式 * @param rABS 加载数据方式 `readAsBinaryString` (默认) 或 `readAsArrayBuffer`,[更多细节](http://t.cn/R3n63A0) */ XlsxService.prototype.import = function (fileOrUrl, rABS) { var _this = this; if (rABS === void 0) { rABS = 'readAsBinaryString'; } return new Promise(function (resolver, reject) { _this.init().then(function () { // from url if (typeof fileOrUrl === 'string') { _this.http .request('GET', fileOrUrl, { responseType: 'arraybuffer' }) .subscribe(function (res) { var wb = XLSX.read(new Uint8Array(res), { type: 'array' }); resolver(_this.read(wb)); }, function (err) { reject(err); }); return; } // from file var reader = new FileReader(); reader.onload = function (e) { var wb = XLSX.read(e.target.result, { type: 'binary' }); resolver(_this.read(wb)); }; reader[rABS](fileOrUrl); }); }); }; /** 导出 */ XlsxService.prototype.export = function (options) { return this.init().then(function () { var wb = XLSX.utils.book_new(); if (Array.isArray(options.sheets)) { options.sheets.forEach(function (value, index) { var ws = XLSX.utils.aoa_to_sheet(value.data); XLSX.utils.book_append_sheet(wb, ws, value.name || "Sheet" + (index + 1)); }); } else { wb.SheetNames = Object.keys(options.sheets); wb.Sheets = options.sheets; } if (options.callback) options.callback(wb); var wbout = XLSX.write(wb, Object.assign({ bookType: 'xlsx', bookSST: false, type: 'array', }, options.opts)); saveAs(new Blob([wbout], { type: 'application/octet-stream' }), options.filename || 'export.xlsx'); }); }; XlsxService.decorators = [ { type: Injectable }, ]; /** @nocollapse */ XlsxService.ctorParameters = function () { return [ { type: undefined, decorators: [{ type: Inject, args: [DA_XLSX_CONFIG,] }] }, { type: HttpClient }, { type: LazyService } ]; }; return XlsxService; }()); export { XlsxService }; //# sourceMappingURL=xlsx.service.js.map