excel导出功能时诸多后台管理系统必备的功能,本文主要研究在基于egg.js的系统中,封装exceljs,从而大大简化excel导出。
这里我们扩展helper,直接上代码
const Excel = require('exceljs');
/**
* 请求接口得到数据并生成excel
* 支持复杂表头(m1:合并单元格左上坐标;m2:合并单元格右下坐标)
* 支持合计行 totalRowText totalRow
* 支持数据自定义 func
* 支持数据字典
* 支持日期
* @param {string} url 接口地址:相对地址或者http开头完整地址
* @param {object} req 请求数据
* @param {Array} headers excel标题栏
* @param {string} name 文件名称
* @param {function} func 数据自定义函数
*/
async excelNew(url, req, headers, name, func) {
let columns = [];//exceljs要求的columns
let hjRow = {};//合计行
let titleRows = headers.length;//标题栏行数
//处理表头
for (let i = 0; i < titleRows; i++) {
let row = headers[i];
for (let j = 0, rlen = row.length; j < rlen; j++) {
let col = row[j];
let { f, t, w = 15 } = col;
if (!f) continue;//不存在f则跳过
if (col.totalRow) hjRow[f] = true;
if (col.totalRowText) hjRow[f] = col.totalRowText;
col.style = { alignment: { vertical: 'middle', horizontal: 'center' } };
col.header = t;
col.key = f;
col.width = w;
columns.push(col);
}
}
const result = await this.post(url, req);//请求数据
let data = result.data;
if (func) data = func(data);
//处理合计行
if (JSON.stringify(hjRow) != "{}") {
let tr = {};
for (let i = 0, len = data.data.length; i < len; i++) {
let item = data.data[i];
for (let key in item) {
if (hjRow[key] === true) {
tr[key] = (tr[key] || 0) + item[key];
continue;
}
tr[key] = hjRow[key] || '';
}
}
data.data.push(tr);
}
let workbook = new Excel.Workbook();
let sheet = workbook.addWorksheet('My Sheet', { views: [{ xSplit: 1, ySplit: 1 }] });
sheet.columns = columns;
sheet.addRows(data.data);
//处理复杂表头
if (titleRows > 1) {
for (let i = 1; i < titleRows; i++) sheet.spliceRows(1, 0, []);//头部插入空行
for (let i = 0; i < titleRows; i++) {
let row = headers[i];
for (let j = 0, rlen = row.length; j < rlen; j++) {
let col = row[j];
if (!col.m1) continue;
sheet.getCell(col.m1).value = col.t;
sheet.mergeCells(col.m1 + ":" + col.m2);
}
}
}
//处理样式、日期、字典项
let that = this;
sheet.eachRow(function (row, rowNumber) {
//设置行高
row.height = 25;
row.eachCell({ includeEmpty: true }, function (cell, colNumber) {
//设置边框 黑色 细实线
let top = left = bottom = right = { style: 'thin', color: { argb: '000000' } };
cell.border = { top, left, bottom, right };
//设置标题部分为粗体
if (rowNumber <= titleRows) { cell.font = { bold: true }; return; }
//处理数据项里面的日期和字典
let {type,dict} = columns[colNumber - 1];
if (type && (cell.value || cell.value == 0)) return;//非日期、字典或值为空的直接返回
switch(type){
case 'date': cell.value = that.parseDate(cell.value);break;
case 'dict': cell.value = that.parseDict(cell.value.toString(), dict);break;
}
});
});
this.ctx.set('Content-Type', 'application/vnd.openxmlformats');
this.ctx.set('Content-Disposition', "attachment;filename*=UTF-8' '" + encodeURIComponent(name) + '.xlsx');
this.ctx.body = await workbook.xlsx.writeBuffer();
}
使用方式:
async excel(ctx) {
let req = ctx.helper.data(['strBeginTime', 'strEndTime', 'deptId']);
req.deptId = req.deptId || ctx.session.user.deptId;
let headers = [[
{ t: '单位名称',f: 'deptName', w: 20, m1:'A1',m2:'A3',totalRowText: '合计'},
{ t: '办理身份证证件',m1:'B1',m2:'M1'},
{ t: '临时身份证证件', m1:'N1',m2:'O1' },
{ t: '总计', m1:'P1',m2:'R2' }
], [
{ t: '申领', m1:'B2',m2:'D2' },
{ t: '换领', m1:'E2',m2:'G2' },
{ t: '补领', m1:'H2',m2:'J2' },
{ t: '小计', m1:'K2',m2:'M2' },
{ t: '临时身份证', m1:'N2',m2:'O2' }
], [
{ t: '本地人数', f: 'slbdCount', totalRow: true },
{ t: '异地人数', f: 'slydCount', totalRow: true },
{ t: '金额', f: 'slJe', totalRow: true },
{ t: '本地人数', f: 'hlbdCount', totalRow: true },
{ t: '异地人数', f: 'hlydCount', totalRow: true },
{ t: '金额', f: 'hlJe', totalRow: true },
{ t: '本地人数', f: 'blbdCount', totalRow: true },
{ t: '异地人数', f: 'blydCount', totalRow: true },
{ t: '金额', f: 'blJe', totalRow: true },
{ t: '本地人数', f: 'xj_bdrs', totalRow: true },
{ t: '异地人数', f: 'xj_ydrs', totalRow: true },
{ t: '金额', f: 'xj_je', totalRow: true },
{ t: '人数', f: 'lsCount', totalRow: true },
{ t: '金额', f: 'lsJe', totalRow: true },
{ t: '本地人数', f: 'hj_bdrs', totalRow: true },
{ t: '异地人数', f: 'hj_ydrs', totalRow: true },
{ t: '金额', f: 'hj_je', totalRow: true }
]];
await ctx.helper.excelNew('/bill/querySfzbb', req, headers, '身份证受理统计',function(res){
for (let i = 0, len = res.data.length; i < len; i++) {
let r = res.data[i];
r.xj_bdrs = r.slbdCount + r.hlbdCount + r.blbdCount;
r.xj_ydrs = r.slydCount + r.hlydCount + r.blydCount;
r.xj_je = r.slJe + r.hlJe + r.blJe;
r.hj_bdrs = r.slbdCount + r.hlbdCount + r.blbdCount + r.lsCount;
r.hj_ydrs = r.slydCount + r.hlydCount + r.blydCount;
r.hj_je = r.slJe + r.hlJe + r.blJe + r.lsJe;
}
return res;
});
}