Appearance
导出定制化样式的excel文件
背景
某团队通过织信搭建了一份全国企业信用查询系统,该系统需提供对外导出企业报告信息单功能。
在企业报告信息单中需要有工商代码等一对一基础信息,以及主要成员、主要变更的股权等一对多的复杂关系。
实现步骤
- 创建网站和资源托管模块
- 上传Excel模板
- 在表单设置中添加按钮调用自动化
- 创建调用自动化
- 通过调用脚本生成最终需要的文件
javascript
/**
* 构造为跨行的集
* @param list
* @param key
*/
const buildCellList = (list, key) => {
// 分组
const map = new Map();
list.forEach(item => {
let children = map.get(item[key]);
if (!children) {
children = [];
map.set(item[key], children);
}
children.push(item);
});
// 设置跨行
map.forEach((children, k) => {
if (children.length > 1) {
// 每个分组的首个 元素进行跨行合并
const item = children[0];
item[key] = informat.excel.createTemplateCell({
content: item[key],
colspan: 1,
rowspan: children.length,
});
}
});
}
// 开始执行...
// 获取记录
const record = automatic.getVar('record');
// 测试数据
// const record = {"date":new Date(),"legalRepresentative":"张某帅","unifiedSocialCreditCode":"SZ00000000001","officialWebsite":"https://informat.cn/","address":"深圳市南山区","registeredCapital":"800 亿元","phone":"1304900xxxx","companyName":"深圳市XXX股份有限公司","logo":{"path":"nzt2qnr0kny3p/jnno0wb5vny65/40b141a9a26a4eabaf04148269cbf668.png","thumbnail":"3e1695e23b9f49c4880ce95d0d99a0e5.png","size":2271,"name":"图片1.png","id":"40b141a9a26a4eabaf04148269cbf668.png","md5":"1338ee9bf691a747e7bbb72b5fa94ab1"},"id":"r7hh35ymsm30s","incorporationDate":"2016-06-01"};
// 先将静态 模块的模板文件下载至当前环境
informat.website.download('website','template.xlsx','template.xlsx');
// 根据关联ID获取到主要成员
const userList = informat.table.queryList('userList', {
filter:{
conditionList:[
{fieldId:'companyRel',opt:'eq',value: record.id}
]
}
});
// 测试数据
// const userList = [{"gender":"1","shareholdingRatio":"42.14%","companyRel":"r7hh35ymsm30s","name":"张某帅","companyRel_name":"深圳市XXX股份有限公司","position":"实际控制人、最终受益人、大股东","id":"owmn1qa6p5fst","ultimateBeneficialShare":"42.15%","individualResume":"低代码行业先驱领军者,在行业..."},{"gender":"2","shareholdingRatio":"34.9%","companyRel":"r7hh35ymsm30s","name":"王小美","companyRel_name":"深圳市XXX股份有限公司","position":"监事\t","id":"foxp35iywuef4","ultimateBeneficialShare":"34.9%","individualResume":"低代码行业先驱领军者,在行业..."}];
// 根据关联ID获取企业变更
let changeList = informat.table.queryList('changeList', {
filter:{
conditionList:[
{fieldId:'companyRel',opt:'eq',value: record.id}
]
},
});
// 测试数据
// let changeList = [{"date":"2022-11-07","companyRel":"r7hh35ymsm30s","companyRel_name":"深圳市XXX股份有限公司","afterChange":"2022-10-10","id":"msq11e046za3g","changeProject":"章程或章程修正案通过日期","beforeChange":"2022-05-24"},{"date":"2022-11-07","companyRel":"r7hh35ymsm30s","companyRel_name":"深圳市XXX股份有限公司","afterChange":"张某帅 800.0(亿元)42.14%\n王小美* 600.0(亿元)38.70%\nA市区龙王集团有限公司 5.2(亿元)18.00%","id":"jd67yjxhrni18","changeProject":"投资人(包括出资额、出资方式、出资日期、投资人名称等)","beforeChange":"张某帅 800.0(亿元)42.14%\n王小美* 600.0(亿元)38.70%\n深圳市小卡拉米企业管理合伙企业(有限合伙) 690.0(万元)0.01%【退出】\nA市区龙王集团有限公司 5.2(亿元)18.00%"},{"date":"2022-06-09","companyRel":"r7hh35ymsm30s","companyRel_name":"深圳市XXX股份有限公司","afterChange":"-","id":"qx0tv0m7bfxd9","changeProject":"股权和公证书","beforeChange":"-"}];
// 根据关联ID获取企业变更
let taxRevenueList = informat.table.queryList('taxRevenueList', {
filter:{
conditionList:[
{fieldId:'companyRel',opt:'eq',value: record.id}
]
},
});
// 测试数据
// let taxRevenueList = [{"date":1640966400000,"note":"22年1月","amount":10000.0,"taxPeriod":1672502400000,"companyRel":"r7hh35ymsm30s","companyRel_name":"深圳市XXX股份有限公司","id":"t5vdqirf4fig9"},{"date":1643644800000,"note":"22年2月","amount":20000.0,"taxPeriod":1675180800000,"companyRel":"r7hh35ymsm30s","companyRel_name":"深圳市XXX股份有限公司","id":"xqkic6tnoimd4"}]
// 根据日期降序排序
// ...
// 通过date 将changeList进行分组 相同日期的数据跨行合并起来
buildCellList(changeList,'date');
const workbook = informat.excel.openWithTemplate('example.xlsx','template.xlsx',{
companyName: record.companyName,
date: informat.Date.now(),
legalRepresentative: record.legalRepresentative,
registeredCapital: record.registeredCapital,
unifiedSocialCreditCode: record.unifiedSocialCreditCode,
officialWebsite: record.officialWebsite,
incorporationDate: record.incorporationDate,
address: record.address,
phone: record.phone,
// 企业logo 跨列、跨行
logo: informat.excel.createTemplatePicture({
type:'storage',
content:record.logo.path,
colspan: 2,
rowspan: 4
}),
userList: userList,
changeList: changeList,
taxRevenueList: taxRevenueList,
});
// 写入到文件
workbook.write();
自动化流程图
点击导出按钮查看最终结果
模板 指令介绍
模板是处理复杂Excel的简单方法,复杂的Excel样式,可以用Excel直接编辑,完美的避开了代码编写样式的雷区,同时指令的支持,也提了模板的有效性 下面列举下EasyPoi支持的指令以及作用,最主要的就是各种fe的用法
- 空格分割
- 三目运算
{{test ? obj:obj2}}
例如:value == '1'(操作符必须要用空格隔开,可用操作符:> < == !=) - n: 表示 这个cell是数值类型
{{n:}}
- le: 代表长度
{{le:()}}
在if/else 运用{{le:() > 8 ? obj1 : obj2}}
- fd: 格式化时间
{{fd:(obj;yyyy-MM-dd)}}
- fn: 格式化数字
{{fn:(obj;###.00)}}
- fe: 遍历数据,创建row
- !fe: 遍历数据不创建row
- $fe: 下移插入,把当前行,下面的行全部下移.size()行,然后插入
- #fe: 横向遍历
- v_fe: 横向遍历值
- !if: 删除当前列
{{!if:(test)}}
- 单引号表示常量值 '' 比如'1' 那么输出的就是 1
- &NULL& 空格
- ]] 换行符 多行遍历导出
- sum: 统计数据
采用的写法是{{}}
代表表达式,然后根据表达式里面的数据取值
更多示例查看easypoi