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

2021-03-19 17:52:03 +08:00
 hytex

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

759 次点击
所在节点    问与答
3 条回复
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
2021-03-20 09:37:01 +08:00
datatable->excel
hytex
2021-03-20 14:57:05 +08:00
@KouShuiYu 谢谢谢谢,不过我需要的后端的处理方法。前端这样子,我也去试试。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/763271

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX