const headerData = [
{
title: '分组一',
children: [
{
title: '今年',
dataIndex: 'ND',
key: 'ND',
width: 140,
showSorterTooltip: false,
align: 'center',
sorter: false
},
{
title: '明年',
dataIndex: 'GLDW1',
key: 'GLDW1',
width: 140,
showSorterTooltip: false,
align: 'center',
sorter: false
}
],
editable: true
},
{
title: '日期',
dataIndex: 'GLDW2',
key: 'GLDW2',
width: 140,
showSorterTooltip: false,
align: 'center',
sorter: false,
editable: true
}
];
const datas = [
{
PK_UID: 1,
GLDW1: '1',
GLDW2: '',
GLDW3: '',
ZQ: '1',
TBQS: '02',
LD_QS: '1',
TDSYQS: '1',
LMSYQS: '1',
LMQS: '1',
LQZBL: '1',
LD_QML: 1,
LD_ZL: 1,
LD_SLD: 1,
LD_GTG: 1,
LD_QTG: 1,
LD_WCL: 1,
LD_WLM: 1,
LD_MPD: 1,
BHYY: '1',
BZ: '1',
TBRY: '1',
TBID: '1',
ND: '2023',
GLDW1_DESC: '1',
GLDW2_DESC: '',
GLDW3_DESC: '',
ZQ_DESC: '1',
TBQS_DESC: '2期',
LD_QS_DESC: '国有',
TDSYQS_DESC: '国有',
LMSYQS_DESC: '国有',
LMQS_DESC: '国有',
LQZBL_DESC: '办理'
}
];
/** 导出 Excel 表格 可过滤不需要的字段 */
export const exportExcel = (
headerData: Record<string, any>[],
dataSource: Record<string, any>[],
xlsxName: string,
filterHeader: string[] = [],
) => {
const optionKey = getTableActions().dataIndex;
const filterFields = [optionKey, ...filterHeader];
// 过滤表头不需要的字段
const filteredHeaderData = headerData.filter((item) => {
// 如果需要保留该字段,返回 true;否则返回 false
return !filterFields.includes(item.dataIndex);
});
// 将 filteredHeaderData 中的 dataIndex 提取为表头数组
const headers = filteredHeaderData.map((item) => item.title);
// 将 datas 中的数据转换为二维数组
const dataRows = dataSource.map((data) =>
filteredHeaderData.map((item) => {
return data[`${item.dataIndex}_DESC`] || data[`${item.dataIndex}`];
}),
);
// 创建一个新的工作簿
const workbook = XLSX.utils.book_new();
// 创建一个工作表
const worksheet = XLSX.utils.aoa_to_sheet([headers, ...dataRows]);
// 将工作表添加到工作簿
XLSX.utils.book_append_sheet(workbook, worksheet);
// 导出Excel文件
const excelBuffer = XLSX.write(workbook, { bookType: 'xlsx', type: 'array' });
const excelData = new Blob([excelBuffer], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' });
saveAs(excelData, xlsxName);
};
table-xlsx