123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657 |
- import { Controller, Body, Fields, Files, Post, Inject } from '@midwayjs/core';
- import { Context } from '@midwayjs/koa';
- import { ApiTags } from '@midwayjs/swagger';
- import { UtilService } from '../service/util.service';
- const Excel = require('exceljs');
- @ApiTags(['工具'])
- @Controller('/util')
- export class UtilController {
- @Inject()
- service: UtilService;
- @Inject()
- ctx: Context;
- @Post('/toExport')
- async toExport(@Body() data: object) {
- const result = await this.service.toExport(data);
- return result;
- }
- @Post('/toImport')
- async upload(@Files() files, @Fields() fields) {
- const workbook = new Excel.Workbook();
- await workbook.xlsx.read(files);
- const result = [];
- const sheetList = [];
- workbook.eachSheet(sheet => {
- // 整理出的数据
- const rows = [];
- sheet.eachRow((row, ri) => {
- if (ri !== 1) {
- const rd = [null];
- row.eachCell({ includeEmpty: true }, c => {
- const value = c.value;
- rd.push(value);
- });
- rows.push(rd);
- }
- });
- sheetList.push({ sheet: sheet.name, rows });
- });
- for (const s of sheetList) {
- const { sheet, rows } = s;
- const func = this.service.nameToFunction(sheet);
- if (!func) continue;
- try {
- const num = await this.service[func](rows);
- result[sheet] = num.result;
- result.push({ key: sheet, num: num.result, errorList: num.errorList.join(',') });
- } catch (error) {
- console.log(error);
- result.push({ key: sheet, num: 'error' });
- }
- }
- this.ctx.ok({ data: result });
- }
- }
|