diff --git a/src/views/credit/creditCompany/index.vue b/src/views/credit/creditCompany/index.vue index a31d689..a203fa9 100644 --- a/src/views/credit/creditCompany/index.vue +++ b/src/views/credit/creditCompany/index.vue @@ -341,6 +341,7 @@ const exportData = () => { exportCreditData({ filename: '企业', + includeCompanyName: false, columns: [ { title: '原文件导入名称', dataIndex: 'name' }, { title: '系统匹配企业名称', dataIndex: 'matchName' }, diff --git a/src/views/credit/utils/export.ts b/src/views/credit/utils/export.ts index 8460592..2adeae8 100644 --- a/src/views/credit/utils/export.ts +++ b/src/views/credit/utils/export.ts @@ -13,6 +13,11 @@ interface ExportOptions { fetchData: () => Promise; beforeMessage?: string; successMessage?: string; + /** + * 是否自动插入“企业名称(companyName)”列(默认:true) + * 用于 credit 模块子表导出统一补齐企业名称 + */ + includeCompanyName?: boolean; } export async function exportCreditData({ @@ -20,14 +25,34 @@ export async function exportCreditData({ columns, fetchData, beforeMessage, - successMessage + successMessage, + includeCompanyName = true }: ExportOptions) { const hide = message.loading(beforeMessage || '正在导出数据...', 0); try { const list = await fetchData(); - const headerRow = columns.map((col) => col.title); + const normalizedColumns = [...columns]; + if ( + includeCompanyName && + !normalizedColumns.some((c) => c.dataIndex === 'companyName') + ) { + const companyNameCol: ExportColumn = { + title: '企业名称', + dataIndex: 'companyName' + }; + if ( + normalizedColumns.length > 0 && + normalizedColumns[0].dataIndex === 'id' + ) { + normalizedColumns.splice(1, 0, companyNameCol); + } else { + normalizedColumns.unshift(companyNameCol); + } + } + + const headerRow = normalizedColumns.map((col) => col.title); const dataRows = (list || []).map((item) => - columns.map((col) => { + normalizedColumns.map((col) => { if (col.formatter) { return col.formatter(item) ?? ''; } @@ -38,7 +63,7 @@ export async function exportCreditData({ const sheetName = filename.replace(/\s+/g, '_'); const sheet = utils.aoa_to_sheet([headerRow, ...dataRows]); - sheet['!cols'] = columns.map(() => ({ wch: 20 })); + sheet['!cols'] = normalizedColumns.map(() => ({ wch: 20 })); const workbook = { SheetNames: [sheetName],