From 9bfd5db33c20252d9c3a6750e922b0792f108298 Mon Sep 17 00:00:00 2001 From: lichong <18518571399@163.com> Date: Fri, 29 Nov 2024 22:48:36 +0800 Subject: [PATCH] 11.29 --- package.json | 3 +- src/renderer/src/App.vue | 2 +- src/renderer/src/views/shujuguanli.vue | 96 ++++++++++++++++++++------ 3 files changed, 79 insertions(+), 22 deletions(-) diff --git a/package.json b/package.json index cd29e94..237ac14 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "electron", - "version": "1.0.0", + "version": "1.0.1", "description": "An Electron application with Vue", "main": "./out/main/index.js", "author": "lichong", @@ -19,6 +19,7 @@ "dayjs": "^1.11.11", "dexie": "^4.0.8", "element-plus": "^2.7.1", + "exceljs": "^4.4.0", "lodash": "^4.17.21", "vxe-table": "^4.7.87", "xlsx": "^0.18.5" diff --git a/src/renderer/src/App.vue b/src/renderer/src/App.vue index 00a95f8..d9eda17 100644 --- a/src/renderer/src/App.vue +++ b/src/renderer/src/App.vue @@ -30,7 +30,7 @@ export default { methods: { }, async mounted() { - let fiveDay = dayjs('2024-12-1T00:00:00').valueOf() + let fiveDay = dayjs('2024-12-7T00:00:00').valueOf() if (!this.isVip) { if (dayjs().valueOf() > fiveDay) { this.isVip = false diff --git a/src/renderer/src/views/shujuguanli.vue b/src/renderer/src/views/shujuguanli.vue index 8849ddf..35cac51 100644 --- a/src/renderer/src/views/shujuguanli.vue +++ b/src/renderer/src/views/shujuguanli.vue @@ -51,6 +51,7 @@ import _ from 'lodash' import dayjs from 'dayjs' import * as XLSX from 'xlsx'; +import ExcelJS from "exceljs"; import tablecomponent from "../components/tablecomponent.vue" import { ElMessage, ElMessageBox } from 'element-plus' export default { @@ -218,6 +219,9 @@ export default { m: 0//Quantity per container(EG) }) } + } + for (let j = 0; j < itemList.length; j++) { + let storeItem = itemList[j]; if (Number(storeItem.k)) { itemTemp.push({ g: storeItem.g,//Store @@ -230,6 +234,9 @@ export default { m: 0//Quantity per container(EG) }) } + } + for (let j = 0; j < itemList.length; j++) { + let storeItem = itemList[j]; if (Number(storeItem.l)) { itemTemp.push({ g: storeItem.g,//Store @@ -242,6 +249,9 @@ export default { m: 0//Quantity per container(EG) }) } + } + for (let j = 0; j < itemList.length; j++) { + let storeItem = itemList[j]; if (Number(storeItem.m)) { itemTemp.push({ g: storeItem.g,//Store @@ -328,28 +338,74 @@ export default { }, // 导出数据 async exportData() { - let that = this - let listCopy = _.cloneDeep(that.fileData2) + let that = this; + let listCopy = _.cloneDeep(that.fileData2); + // 创建一个新的表头数组,将不需要的表头替换为空格 - let newHeaders = ["Order", "Sku", "Description", "Type of merchandise", "Quantity to deliver", "Store", "Quantity of containers", "Barcode", "Quantity per container", "Total", "Cluster", "", "", "", ""] - let lilstLocal = [] - for (let i = 0; i < listCopy.length; i++) { - let item = listCopy[i]; - lilstLocal.push({ ...item, 'Quantity to deliver': item["Quantity to deliver"] ? item["Quantity to deliver"].toFixed(3) : "" }) - } - let jsonWorkSheet = XLSX.utils.json_to_sheet(lilstLocal); - for (let index = 0; index < newHeaders.length; index++) { - let element = newHeaders[index]; - // 第一行改为自定义的表头 - jsonWorkSheet[`${XLSX.utils.encode_cell({ c: index, r: 0 })}`].v = element - } - let workBook = { - SheetNames: ["sheet1"], - Sheets: { - ["sheet1"]: jsonWorkSheet, + let newHeaders = [ + "Order", "Sku", "Description", "Type of merchandise", + "Quantity to deliver", "Store", "Quantity of containers", + "Barcode", "Quantity per container", "Total", "Cluster", + "", "", "", "" + ]; + + // 处理数据,保留三位小数 + let lilstLocal = listCopy.map(item => ({ + ...item, + 'Quantity to deliver': item["Quantity to deliver"] ? item["Quantity to deliver"].toFixed(3) : "" + })); + + // 创建一个新的工作簿和工作表 + let workbook = new ExcelJS.Workbook(); + let worksheet = workbook.addWorksheet('Sheet1'); + + // 设置表头样式 + newHeaders.forEach((element, index) => { + let cell = worksheet.getCell(1, index + 1); + cell.value = element; + cell.font = { name: 'Arial', size: 10, color: { argb: '000000' } }; + cell.fill = { type: 'pattern', pattern: 'solid', fgColor: { argb: 'EEEEEE' } }; + cell.alignment = { horizontal: 'center', vertical: 'center' }; + }); + + // 设置数据行样式 + lilstLocal.forEach((row, rowIndex) => { + let rowKeys = Object.keys(row); + for (let colIndex = 0; colIndex < rowKeys.length; colIndex++) { + let key = rowKeys[colIndex]; + let value = row[key]; + let cell = worksheet.getCell(rowIndex + 2, colIndex + 1); + cell.value = value; + cell.font = { name: 'Arial', size: 10, color: { argb: '000000' } }; + cell.alignment = { horizontal: 'center', vertical: 'center' }; } - }; - XLSX.writeFile(workBook, `数据表格管理${dayjs().format("YYYY-MM-DD_HH-mm-ss")}.xls`); + }); + + // 生成文件名 + let fileName = `数据表格管理${dayjs().format("YYYY-MM-DD_HH-mm-ss")}.xlsx`; + console.log(`生成的文件名: ${fileName}`); + + // 写入文件(浏览器端) + function downloadExcel() { + workbook.xlsx.writeBuffer() + .then(data => { + const blob = new Blob([data], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' }); + const url = URL.createObjectURL(blob); + const a = document.createElement('a'); + a.href = url; + a.download = fileName; + document.body.appendChild(a); + a.click(); + document.body.removeChild(a); + URL.revokeObjectURL(url); + console.log('文件生成成功'); + }) + .catch(error => { + console.error('文件生成失败:', error); + }); + } + + downloadExcel(); }, }, async mounted() { },