From 127cb068471cbd52c0c64c200a0dec661c06ca8a Mon Sep 17 00:00:00 2001 From: zhaoyf <1783123481@qq.com> Date: Sat, 31 Oct 2026 15:53:39 +0800 Subject: [PATCH] =?UTF-8?q?add:=E4=BB=93=E5=82=A8=E7=AE=A1=E7=90=86?= =?UTF-8?q?=E5=87=BA=E5=85=A5=E5=BA=93=E5=8D=95=E6=8D=AE=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?=E5=AF=BC=E5=87=BA=E6=98=8E=E7=BB=86=E5=88=B0excel?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- nladmin-ui/src/utils/export-excel.js | 47 ++++++++++ nladmin-ui/src/views/wms/st/inbill/index.vue | 87 +++++++++++++++++-- nladmin-ui/src/views/wms/st/outbill/index.vue | 87 +++++++++++++++++-- 3 files changed, 205 insertions(+), 16 deletions(-) create mode 100644 nladmin-ui/src/utils/export-excel.js diff --git a/nladmin-ui/src/utils/export-excel.js b/nladmin-ui/src/utils/export-excel.js new file mode 100644 index 00000000..52c8ad47 --- /dev/null +++ b/nladmin-ui/src/utils/export-excel.js @@ -0,0 +1,47 @@ +import XLSX from 'xlsx' + +/** + * 生成文件名中的时间戳,如 20261031_152919 + */ +export function formatExportTime(date) { + const pad = value => String(value).padStart(2, '0') + return date.getFullYear() + pad(date.getMonth() + 1) + pad(date.getDate()) + '_' + + pad(date.getHours()) + pad(date.getMinutes()) + pad(date.getSeconds()) +} + +/** + * 将数据按列定义导出为 excel 文件 + * + * @param {Object} options + * @param {Array} options.columns 列定义 [{ label, prop, width, value }],value 为可选的自定义取值函数 + * @param {Array} options.rows 数据行 + * @param {String} options.sheetName sheet 名称 + * @param {String} options.fileName 文件名(不含时间戳和后缀) + */ +export function exportRowsToExcel({ columns, rows, sheetName = '明细', fileName = '明细' }) { + if (!columns || !columns.length) { + throw new Error('未定义导出列') + } + const header = columns.map(col => col.label) + const body = rows.map(row => columns.map(col => { + const value = typeof col.value === 'function' ? col.value(row) : row[col.prop] + if (value === null || value === undefined) { + return '' + } + return value + })) + const sheet = XLSX.utils.aoa_to_sheet([header].concat(body)) + sheet['!cols'] = columns.map(col => ({ wch: col.width || 14 })) + if (body.length) { + sheet['!autofilter'] = { + ref: XLSX.utils.encode_range({ + s: { r: 0, c: 0 }, + e: { r: body.length, c: header.length - 1 } + }) + } + } + const workbook = XLSX.utils.book_new() + XLSX.utils.book_append_sheet(workbook, sheet, sheetName) + XLSX.writeFile(workbook, fileName + '_' + formatExportTime(new Date()) + '.xlsx') + return body.length +} diff --git a/nladmin-ui/src/views/wms/st/inbill/index.vue b/nladmin-ui/src/views/wms/st/inbill/index.vue index c56a892b..7f2c3458 100644 --- a/nladmin-ui/src/views/wms/st/inbill/index.vue +++ b/nladmin-ui/src/views/wms/st/inbill/index.vue @@ -130,6 +130,18 @@ > 强制确认 + + 导出明细 + 1) { - this.$refs.table.clearSelection() - this.$refs.table.toggleRowSelection(val.pop()) - this.buttonChange(row) - } else if (val.length === 1) { - this.buttonChange(row) - } else { + if (val.length === 0) { this.handleCurrentChange(null) + return + } + // 记录最后勾选的行,供分配、强制确认等单条操作使用 + this.buttonChange(row) + if (val.length > 1) { + // 多选只用于批量导出明细,单条操作按钮置为不可用,避免误操作 + this.dis_flag = true + this.task_flag = true } }, onSelectAll() { - this.$refs.table.clearSelection() + // 允许全选,便于一次性导出当前页全部单据的明细 this.handleCurrentChange(null) }, buttonChange(currentRow) { @@ -375,7 +391,62 @@ export default { this.divShow = true }) }, + exportDetail() { + const selections = this.crud.selections || [] + if (!selections.length) { + this.crud.notify('请先勾选要导出的单据', CRUD.NOTIFICATION_TYPE.INFO) + return + } + this.exporting = true + const promises = selections.map(bill => { + return crudRawAssist.getIODtl({ 'iostorinv_id': bill.iostorinv_id }).then(res => { + const dtlList = res.data || [] + return dtlList.map(dtl => ({ ...dtl, stor_name: dtl.stor_name || bill.stor_name })) + }) + }) + Promise.all(promises).then(results => { + const allDtls = results.reduce((acc, cur) => acc.concat(cur), []) + if (allDtls.length === 0) { + this.crud.notify('未查询到明细数据', CRUD.NOTIFICATION_TYPE.INFO) + return + } + const total = exportRowsToExcel({ + columns: this.detailColumns(), + rows: allDtls, + sheetName: '入库明细', + fileName: '入库明细' + }) + this.crud.notify('导出成功,共 ' + total + ' 条明细', CRUD.NOTIFICATION_TYPE.SUCCESS) + }).catch(() => { + this.crud.notify('导出失败,请重试', CRUD.NOTIFICATION_TYPE.ERROR) + }).finally(() => { + this.exporting = false + }) + }, + detailColumns() { + return [ + { label: '单据号', prop: 'bill_code', width: 20 }, + { label: '仓库', prop: 'stor_name', width: 18 }, + { label: '行号', prop: 'seq_no', width: 8 }, + { label: '物料编码', prop: 'material_code', width: 20 }, + { label: '物料名称', prop: 'material_name', width: 28 }, + { label: '批次号', prop: 'pcsn', width: 18 }, + { label: '明细状态', prop: 'bill_status', width: 12, value: row => this.dict.label.io_bill_status[row.bill_status] || row.bill_status }, + { label: '计划数量', prop: 'plan_qty', width: 12 }, + { label: '已分配数量', prop: 'assign_qty', width: 12 }, + { label: '未分配数量', prop: 'unassign_qty', width: 12 }, + { label: '实际数量', prop: 'real_qty', width: 12 }, + { label: '单位', prop: 'qty_unit_name', width: 8 }, + { label: '载具类型', prop: 'vehicle_type', width: 14 }, + { label: '载具编号', prop: 'storagevehicle_code', width: 18 }, + { label: '来源单号', prop: 'source_bill_code', width: 20 }, + { label: '备注', prop: 'remark', width: 24 } + ] + }, querytable() { + if (this.$refs.table) { + this.$refs.table.clearSelection() + } this.onSelectAll() this.crud.toQuery() } diff --git a/nladmin-ui/src/views/wms/st/outbill/index.vue b/nladmin-ui/src/views/wms/st/outbill/index.vue index 21f23187..992e6b62 100644 --- a/nladmin-ui/src/views/wms/st/outbill/index.vue +++ b/nladmin-ui/src/views/wms/st/outbill/index.vue @@ -159,6 +159,17 @@ @click="printBill" >出库标签打印 + 导出明细 + 1) { - this.$refs.table.clearSelection() - this.$refs.table.toggleRowSelection(val.pop()) - this.buttonChange(row) - } else if (val.length === 1) { - this.buttonChange(row) - } else { + if (val.length === 0) { this.handleCurrentChange(null) + return + } + // 记录最后勾选的行,供分配、强制确认等单条操作使用 + this.buttonChange(row) + if (val.length > 1) { + // 多选只用于批量导出明细,单条操作按钮置为不可用,避免误操作 + this.dis_flag = true + this.confirm_flag = true } }, onSelectAll() { - this.$refs.table.clearSelection() + // 允许全选,便于一次性导出当前页全部单据的明细 this.handleCurrentChange(null) }, doPrint() { @@ -394,6 +409,59 @@ export default { this.fetchDetailsAndPrint(selections) }).catch(() => {}) }, + exportDetail() { + const selections = this.crud.selections || [] + if (!selections.length) { + this.crud.notify('请先勾选要导出的单据', CRUD.NOTIFICATION_TYPE.INFO) + return + } + this.exporting = true + const promises = selections.map(bill => { + return checkoutbill.getOutBillDtl({ 'iostorinv_id': bill.iostorinv_id }).then(res => { + const dtlList = res.data || [] + return dtlList.map(dtl => ({ ...dtl, stor_name: dtl.stor_name || bill.stor_name })) + }) + }) + Promise.all(promises).then(results => { + const allDtls = results.reduce((acc, cur) => acc.concat(cur), []) + if (allDtls.length === 0) { + this.crud.notify('未查询到明细数据', CRUD.NOTIFICATION_TYPE.INFO) + return + } + const total = exportRowsToExcel({ + columns: this.detailColumns(), + rows: allDtls, + sheetName: '出库明细', + fileName: '出库明细' + }) + this.crud.notify('导出成功,共 ' + total + ' 条明细', CRUD.NOTIFICATION_TYPE.SUCCESS) + }).catch(() => { + this.crud.notify('导出失败,请重试', CRUD.NOTIFICATION_TYPE.ERROR) + }).finally(() => { + this.exporting = false + }) + }, + detailColumns() { + return [ + { label: '单据号', prop: 'bill_code', width: 20 }, + { label: '仓库', prop: 'stor_name', width: 18 }, + { label: '行号', prop: 'seq_no', width: 8 }, + { label: '物料编码', prop: 'material_code', width: 20 }, + { label: '物料名称', prop: 'material_name', width: 28 }, + { label: '批次号', prop: 'pcsn', width: 18 }, + { label: '明细状态', prop: 'bill_status', width: 12, value: row => this.dict.label.io_bill_status[row.bill_status] || row.bill_status }, + { label: '计划数量', prop: 'plan_qty', width: 12 }, + { label: '已分配数量', prop: 'assign_qty', width: 12 }, + { label: '未分配数量', prop: 'unassign_qty', width: 12 }, + { label: '实际数量', prop: 'real_qty', width: 12 }, + { label: '单位', prop: 'qty_unit_name', width: 8 }, + { label: '载具类型', prop: 'vehicle_type', width: 14 }, + { label: '载具编号', prop: 'storagevehicle_code', width: 18 }, + { label: '用料位置', prop: 'source_load_port', width: 16 }, + { label: '来源单号', prop: 'source_bill_code', width: 20 }, + { label: '备注', prop: 'remark', width: 24 } + ] + }, fetchDetailsAndPrint(selections) { const promises = selections.map(bill => { return checkoutbill.getOutBillDtl({ 'iostorinv_id': bill.iostorinv_id }).then(res => { @@ -546,6 +614,9 @@ export default { }) }, querytable() { + if (this.$refs.table) { + this.$refs.table.clearSelection() + } this.onSelectAll() this.crud.toQuery() this.handleCurrentChange(null)