123456789101112131415161718192021 |
- 'use strict';
- const Service = require('egg').Service;
- const ExcelJS = require('exceljs');
- class TemplateService extends Service {
- // 创建excel模板
- async excel({ type, _this }) {
- const workbook = new ExcelJS.Workbook();
- const sheet = workbook.addWorksheet('Sheet');
- const exportFiled = _this.app.config.exportFiled[`export${type}`];
- sheet.columns = exportFiled.map(e => ({ header: e.title, key: e.name, width: 30 }));
- return await workbook.xlsx.writeFile(`${_this.app.config.templatePath}/template${type}.xlsx`).then(function() {
- _this.ctx.set('Content-Type', 'application/vnd.ms-excel;charset=UTF-8');
- return `${_this.app.config.templatePath}/template${type}.xlsx`;
- }, function(err) {
- return err;
- });
- }
- }
- module.exports = TemplateService;
|