From b0f5c34d99fcf04b731fc30892831e12f2ec979f Mon Sep 17 00:00:00 2001 From: toesbieya <1647775459@qq.com> Date: Wed, 13 May 2020 07:52:43 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0core-js=E4=BE=9D=E8=B5=96=20e?= =?UTF-8?q?xcel=E5=AF=BC=E5=87=BA=E9=97=AE=E9=A2=98=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- vue/package.json | 1 + vue/src/utils/excel.js | 39 ++++++++++--------- .../components/ExportExcelTest.vue | 6 +-- vue/src/views/purchase/inbound/index.vue | 16 ++++---- vue/src/views/purchase/order/index.vue | 20 +++++----- vue/src/views/sell/order/index.vue | 20 +++++----- vue/src/views/sell/outbound/index.vue | 16 ++++---- vue/src/views/stock/current/index.vue | 20 +++++----- 8 files changed, 70 insertions(+), 68 deletions(-) diff --git a/vue/package.json b/vue/package.json index 8377b29..4bf7350 100644 --- a/vue/package.json +++ b/vue/package.json @@ -9,6 +9,7 @@ }, "dependencies": { "axios": "^0.19.2", + "core-js": "^3.6.4", "decimal.js": "^10.2.0", "element-ui": "^2.13.0", "fuse.js": "^3.6.1", diff --git a/vue/src/utils/excel.js b/vue/src/utils/excel.js index c1c96ea..56ce142 100644 --- a/vue/src/utils/excel.js +++ b/vue/src/utils/excel.js @@ -3,7 +3,13 @@ import {download} from "@/utils/file" import request from "@/config/request" import {elError} from "@/utils/message" -//导出excel +/** + * 搜索表格页导出excel + * 响应头为json时进行前端导出,否则直接下载文件 + * @param url 搜索的请求地址 + * @param searchForm 搜索参数 + * @param options 合并选项{column, merge},只有前端导出时才需要 + */ export function exportExcel(url, searchForm, options) { request({url, method: 'post', responseType: 'blob', data: searchForm}) .then(({headers, data}) => { @@ -21,9 +27,8 @@ export function exportExcel(url, searchForm, options) { exportExcelByJs(sheet, filename) } reader.readAsText(data) - return } - download(data, filename) + else download(data, filename) }) .catch(e => elError(e)) } @@ -52,13 +57,13 @@ export function exportExcelByJs(sheet, filename) { */ export function json2sheet(column, data, mergeOption, headerRows = 1) { const header = [] - const aliasMap = {} + const propMap = {} const colWidth = [] const needMerge = [] column.forEach((item, i) => { if (!isEmpty(item.header)) header[i] = item.header - if (!isEmpty(item.alias)) aliasMap[item.alias] = i - if (item.merge) needMerge[i] = item.alias + if (!isEmpty(item.prop)) propMap[item.prop] = i + if (item.merge) needMerge[i] = item.prop colWidth[i] = {wch: item.width || 20} }) @@ -69,7 +74,7 @@ export function json2sheet(column, data, mergeOption, headerRows = 1) { } let result = data.map(i => Object.keys(i).reduce((arr, key) => { - arr[aliasMap[key]] = i[key] + arr[propMap[key]] = i[key] return arr }, [])) @@ -85,23 +90,23 @@ export function json2sheet(column, data, mergeOption, headerRows = 1) { * 合并excel * @param primaryKey 该行json的唯一标识字段名 * @param indexKey 该行json的序号字段名 - * @param alias 表头别名,用于映射json + * @param prop 表头的属性名称,用于映射json * @param data json数组 * @param ignoreRows 忽略的行数,默认为1(忽略表头) */ -function mergeExcel(alias, data, primaryKey, indexKey, ignoreRows = 1) { +function mergeExcel(prop, data, primaryKey, indexKey, ignoreRows = 1) { const result = [] const startRows = ignoreRows + 1 - const merge = alias.map(_ => isEmpty(_) ? undefined : [1]) - const temp = alias.map(_ => isEmpty(_) ? undefined : 1) + const merge = prop.map(_ => isEmpty(_) ? undefined : [1]) + const temp = prop.map(_ => isEmpty(_) ? undefined : 1) //序号从1开始 let indexAfterMerge = 1 data[0][indexKey] = indexAfterMerge function compare(currentRow, lastRow, nextRow, colIndex) { - const attr = alias[colIndex] + const attr = prop[colIndex] //若与上一行的primaryKey相同且属性相同,则temp对应项+1 if (currentRow[primaryKey] === lastRow[primaryKey] @@ -142,7 +147,7 @@ function mergeExcel(alias, data, primaryKey, indexKey, ignoreRows = 1) { nextRow[indexKey] = currentRow[indexKey] } - alias.forEach((_, colIndex) => !isEmpty(_) && compare(currentRow, lastRow, nextRow, colIndex)) + prop.forEach((_, colIndex) => !isEmpty(_) && compare(currentRow, lastRow, nextRow, colIndex)) } function number2excelColumnHeader(n) { @@ -166,9 +171,7 @@ function mergeExcel(alias, data, primaryKey, indexKey, ignoreRows = 1) { }) } - merge.forEach((arr, index) => { - arr && mergeResultConstructor(arr, index) - }) + merge.forEach((arr, index) => arr && mergeResultConstructor(arr, index)) return result } @@ -177,9 +180,7 @@ function mergeExcel(alias, data, primaryKey, indexKey, ignoreRows = 1) { const defaultCellStyle = { font: {name: '宋体'}, alignment: { - // 自动换行 - wrapText: 1, - // 居中 + wrapText: 1,// 自动换行 horizontal: "center", vertical: "center", indent: 0 diff --git a/vue/src/views/example/developingTest/components/ExportExcelTest.vue b/vue/src/views/example/developingTest/components/ExportExcelTest.vue index 9af92d9..4e88778 100644 --- a/vue/src/views/example/developingTest/components/ExportExcelTest.vue +++ b/vue/src/views/example/developingTest/components/ExportExcelTest.vue @@ -20,9 +20,9 @@ return { loading: false, column: [ - {header: '序号', alias: 'no', merge: true}, - {header: '名称', alias: 'name', merge: true}, - {header: '日期', alias: 'date'}, + {header: '序号', prop: 'no', merge: true}, + {header: '名称', prop: 'name', merge: true}, + {header: '日期', prop: 'date'}, ], row: { no: 1, diff --git a/vue/src/views/purchase/inbound/index.vue b/vue/src/views/purchase/inbound/index.vue index a797930..41af5aa 100644 --- a/vue/src/views/purchase/inbound/index.vue +++ b/vue/src/views/purchase/inbound/index.vue @@ -126,14 +126,14 @@ }, excel: { column: [ - {header: '序号', alias: 'id'}, - {header: '采购订单单号', alias: 'pid', width: 30}, - {header: '创建人', alias: 'cname'}, - {header: '创建时间', alias: 'ctime'}, - {header: '审核人', alias: 'vname'}, - {header: '审核时间', alias: 'vtime'}, - {header: '状态', alias: 'status'}, - {header: '备注', alias: 'remark', width: 50} + {header: '序号', prop: 'id'}, + {header: '采购订单单号', prop: 'pid', width: 30}, + {header: '创建人', prop: 'cname'}, + {header: '创建时间', prop: 'ctime'}, + {header: '审核人', prop: 'vname'}, + {header: '审核时间', prop: 'vtime'}, + {header: '状态', prop: 'status'}, + {header: '备注', prop: 'remark', width: 50} ] }, } diff --git a/vue/src/views/purchase/order/index.vue b/vue/src/views/purchase/order/index.vue index 1763c67..215140c 100644 --- a/vue/src/views/purchase/order/index.vue +++ b/vue/src/views/purchase/order/index.vue @@ -158,16 +158,16 @@ api: {search, del, getSubById}, excel: { column: [ - {header: '序号', alias: 'id'}, - {header: '供应商', alias: 'sname', width: 30}, - {header: '创建人', alias: 'cname'}, - {header: '创建时间', alias: 'ctime'}, - {header: '审核人', alias: 'vname'}, - {header: '审核时间', alias: 'vtime'}, - {header: '状态', alias: 'status'}, - {header: '完成情况', alias: 'finish'}, - {header: '完成时间', alias: 'ftime'}, - {header: '备注', alias: 'remark', width: 50} + {header: '序号', prop: 'id'}, + {header: '供应商', prop: 'sname', width: 30}, + {header: '创建人', prop: 'cname'}, + {header: '创建时间', prop: 'ctime'}, + {header: '审核人', prop: 'vname'}, + {header: '审核时间', prop: 'vtime'}, + {header: '状态', prop: 'status'}, + {header: '完成情况', prop: 'finish'}, + {header: '完成时间', prop: 'ftime'}, + {header: '备注', prop: 'remark', width: 50} ] } } diff --git a/vue/src/views/sell/order/index.vue b/vue/src/views/sell/order/index.vue index 7f6bd04..f4e2487 100644 --- a/vue/src/views/sell/order/index.vue +++ b/vue/src/views/sell/order/index.vue @@ -158,16 +158,16 @@ api: {search, del, getSubById}, excel: { column: [ - {header: '序号', alias: 'id'}, - {header: '客户', alias: 'custoemr_name', width: 30}, - {header: '创建人', alias: 'cname'}, - {header: '创建时间', alias: 'ctime'}, - {header: '审核人', alias: 'vname'}, - {header: '审核时间', alias: 'vtime'}, - {header: '状态', alias: 'status'}, - {header: '完成情况', alias: 'finish'}, - {header: '完成时间', alias: 'ftime'}, - {header: '备注', alias: 'remark', width: 50} + {header: '序号', prop: 'id'}, + {header: '客户', prop: 'customer_name', width: 30}, + {header: '创建人', prop: 'cname'}, + {header: '创建时间', prop: 'ctime'}, + {header: '审核人', prop: 'vname'}, + {header: '审核时间', prop: 'vtime'}, + {header: '状态', prop: 'status'}, + {header: '完成情况', prop: 'finish'}, + {header: '完成时间', prop: 'ftime'}, + {header: '备注', prop: 'remark', width: 50} ] } } diff --git a/vue/src/views/sell/outbound/index.vue b/vue/src/views/sell/outbound/index.vue index 086401b..be9eb3a 100644 --- a/vue/src/views/sell/outbound/index.vue +++ b/vue/src/views/sell/outbound/index.vue @@ -126,14 +126,14 @@ }, excel: { column: [ - {header: '序号', alias: 'id'}, - {header: '销售订单单号', alias: 'pid', width: 30}, - {header: '创建人', alias: 'cname'}, - {header: '创建时间', alias: 'ctime'}, - {header: '审核人', alias: 'vname'}, - {header: '审核时间', alias: 'vtime'}, - {header: '状态', alias: 'status'}, - {header: '备注', alias: 'remark', width: 50} + {header: '序号', prop: 'id'}, + {header: '销售订单单号', prop: 'pid', width: 30}, + {header: '创建人', prop: 'cname'}, + {header: '创建时间', prop: 'ctime'}, + {header: '审核人', prop: 'vname'}, + {header: '审核时间', prop: 'vtime'}, + {header: '状态', prop: 'status'}, + {header: '备注', prop: 'remark', width: 50} ] } } diff --git a/vue/src/views/stock/current/index.vue b/vue/src/views/stock/current/index.vue index 2aeb829..ef90bd4 100644 --- a/vue/src/views/stock/current/index.vue +++ b/vue/src/views/stock/current/index.vue @@ -104,16 +104,16 @@ detailDialog: false, excel: { column: [ - {header: '序号', alias: 'id', width: 20, merge: true}, - {header: '分类名称', alias: 'cname', width: 20, merge: true}, - {header: '库存总数', alias: 'total_num', width: 20, merge: true}, - {header: '库存总值', alias: 'total_price', width: 20, merge: true}, - {header: '采购订单号', alias: 'cgddid', width: 20, merge: true}, - {header: '采购单价', alias: 'cg_price', width: 20, merge: true}, - {header: '采购数量', alias: 'cg_num', width: 20, merge: true}, - {header: '采购入库单号', alias: 'cgrkid', width: 20}, - {header: '入库时间', alias: 'ctime', width: 20}, - {header: '入库数量', alias: 'rk_num', width: 20}, + {header: '序号', prop: 'id', width: 20, merge: true}, + {header: '分类名称', prop: 'cname', width: 20, merge: true}, + {header: '库存总数', prop: 'total_num', width: 20, merge: true}, + {header: '库存总值', prop: 'total_price', width: 20, merge: true}, + {header: '采购订单号', prop: 'cgddid', width: 20, merge: true}, + {header: '采购单价', prop: 'cg_price', width: 20, merge: true}, + {header: '采购数量', prop: 'cg_num', width: 20, merge: true}, + {header: '采购入库单号', prop: 'cgrkid', width: 20}, + {header: '入库时间', prop: 'ctime', width: 20}, + {header: '入库数量', prop: 'rk_num', width: 20}, ], merge: {primaryKey: 'cid', orderKey: 'id'} }