template.js 797 B

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