V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
hytex
V2EX  ›  问与答

现在所有的列表接口都要添加导出,请问有什么好的解决方案吗?

  •  
  •   hytex · 2021-03-19 17:52:03 +08:00 · 743 次点击
    这是一个创建于 1106 天前的主题,其中的信息可能已经有所发展或是发生改变。

    今天加需求所有的列表接口都需要做导出 excel 功能
    导出简单,但是顶不住列表接口多,请问有什么方案或者方法可以尽量少的编写,然后适用到全部或者大部分列表接口吗? 谢谢。

    3 条回复    2021-03-20 14:57:05 +08:00
    KouShuiYu
        1
    KouShuiYu  
       2021-03-19 18:36:56 +08:00
    我的做法:
    + 写了一个把返回的结果转 table 文件的函数
    + 利用原请求函数返回文件

    代码差不多长这样
    ```js
    // 允许不分页
    async index() {
    const { ctx, service } = this;
    const mixedParams = this.getMixedParams();

    ctx.noPaging = ctx.noPaging || !!mixedParams.noPaging;

    const rules = {
    ...optionalRules({
    page: RULES.page,
    limit: RULES.limit,
    ids: RULES.intArray,
    advIds: RULES.intArray,
    advOfferIds: RULES.strArray,
    manauls: RULES.manaulsTypeEnum,
    }),
    };

    const error = ctx.parameter.validate(rules, mixedParams);
    if (error) {
    return ctx.error(ctx.ERROR.ILLEGAL_PARAMETER, error);
    }

    const { page, limit } = mixedParams;
    const resList = await service.offer.list(mixedParams);
    return ctx.ok(Object.assign(resList, { page, limit }));
    }

    async download() {
    const { ctx } = this;
    ctx.noPaging = true;
    const result = await this.index();
    if (!result || result.code !== 0) {
    return ctx.error(result);
    }

    const fromatRows = result.data.rows.map(val => {
    return val;
    });
    return ctx.file('offer.xlsx', rows2XlsxBuffer(fromatRows));
    }
    ```
    baibaibaibai
        2
    baibaibaibai  
       2021-03-20 09:37:01 +08:00
    datatable->excel
    hytex
        3
    hytex  
    OP
       2021-03-20 14:57:05 +08:00
    @KouShuiYu 谢谢谢谢,不过我需要的后端的处理方法。前端这样子,我也去试试。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2444 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 15:51 · PVG 23:51 · LAX 08:51 · JFK 11:51
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.