diff --git a/src/views/material/cost/component/unreportHome.vue b/src/views/material/cost/component/unreportHome.vue index a62284ac..010ddd38 100644 --- a/src/views/material/cost/component/unreportHome.vue +++ b/src/views/material/cost/component/unreportHome.vue @@ -427,6 +427,7 @@ @@ -531,6 +532,7 @@ @@ -1284,6 +1286,256 @@ export default { }); }, + // 导出历史报表列表为Excel + exportHistoryReportList() { + if (!this.historyReportList || this.historyReportList.length === 0) { + this.$message.warning('没有可导出的数据'); + return; + } + + try { + // 定义Excel列配置 + const columns = [ + { key: 'index', title: '序号' }, + { key: 'agreementCode', title: '协议号' }, + { key: 'unitName', title: '结算单位' }, + { key: 'projectName', title: '结算工程' }, + { key: 'settlementType', title: '结算类型' }, + { key: 'leaseCost', title: '租赁费用' }, + { key: 'repairCost', title: '维修费用' }, + { key: 'loseCost', title: '丢失费用' }, + { key: 'scrapCost', title: '报废费用' }, + { key: 'totalCost', title: '合计费用(元)' } + ]; + + // 准备Excel数据 + const excelData = []; + + // 添加表头 + const headerRow = columns.map(col => col.title); + excelData.push(headerRow); + + // 添加数据行 + this.historyReportList.forEach((row, index) => { + const dataRow = columns.map(col => { + let value = ''; + + if (col.key === 'index') { + value = index + 1; + } else if (col.key === 'settlementType') { + const typeMap = { + 0: '总费用', + 1: '工器具', + 2: '安全工器具' + }; + value = typeMap[row.settlementType] || ''; + } else if (col.key === 'totalCost') { + // 合计费用:转换为数字,保留2位小数 + value = parseFloat(this.calculateTotalCost(row).toFixed(2)); + } else if (['leaseCost', 'repairCost', 'loseCost', 'scrapCost'].includes(col.key)) { + // 其他金额字段:转换为数字,保留2位小数 + const numValue = row[col.key] || 0; + value = parseFloat(parseFloat(numValue).toFixed(2)); + } else { + value = row[col.key] || ''; + } + + return value; + }); + excelData.push(dataRow); + }); + + // 创建工作簿和工作表 + const ws = XLSX.utils.aoa_to_sheet(excelData); + const wb = XLSX.utils.book_new(); + XLSX.utils.book_append_sheet(wb, ws, '历史报表'); + + // 设置列宽 + const colWidths = [ + { wch: 8 }, // 序号 + { wch: 15 }, // 协议号 + { wch: 15 }, // 结算单位 + { wch: 15 }, // 结算工程 + { wch: 12 }, // 结算类型 + { wch: 12 }, // 租赁费用 + { wch: 12 }, // 维修费用 + { wch: 12 }, // 丢失费用 + { wch: 12 }, // 报废费用 + { wch: 15 } // 合计费用 + ]; + ws['!cols'] = colWidths; + + // 设置边框和居中对齐 + const border = { + top: { style: 'thin' }, + bottom: { style: 'thin' }, + left: { style: 'thin' }, + right: { style: 'thin' } + }; + const alignment = { horizontal: 'center', vertical: 'center', wrapText: true }; + + // 遍历所有单元格,添加边框和居中对齐 + for (let row = 0; row < excelData.length; row++) { + for (let col = 0; col < excelData[row].length; col++) { + const cellAddress = XLSX.utils.encode_cell({ r: row, c: col }); + if (!ws[cellAddress]) { + ws[cellAddress] = {}; + } + ws[cellAddress].border = border; + ws[cellAddress].alignment = alignment; + } + } + + // 导出文件 + const fileName = `历史报表_${this.historyReportParams.yearMonth}.xlsx`; + XLSX.writeFile(wb, fileName); + + this.$message.success('导出成功'); + } catch (error) { + console.error('导出失败:', error); + this.$message.error('导出失败'); + } + }, + + // 导出历史费用详情为Excel + exportHistoryCostDetail() { + if (!this.historyCostDetailList || this.historyCostDetailList.length === 0) { + this.$message.warning('没有可导出的数据'); + return; + } + + try { + // 根据费用类型定义不同的列配置 + let columns = []; + let fileName = ''; + let sheetName = ''; + + switch (this.historyCostDetailType) { + case 'lease': + columns = [ + { key: 'index', title: '序号' }, + { key: 'name', title: '名称' }, + { key: 'model', title: '型号' }, + { key: 'unit', title: '单位' }, + { key: 'unitPrice', title: '单价(元)' }, + { key: 'quantity', title: '数量' }, + { key: 'returnQuantity', title: '归还数量' }, + { key: 'startTime', title: '租赁开始日期' }, + { key: 'endTime', title: '租赁结束日期' }, + { key: 'leaseDays', title: '租赁天数' }, + { key: 'cost', title: '费用(元)' } + ]; + fileName = `历史租赁费用详情_${this.currentHistoryRow.agreementCode}_${this.historyReportParams.yearMonth}`; + sheetName = '历史租赁费用'; + break; + case 'repair': + columns = [ + { key: 'index', title: '序号' }, + { key: 'name', title: '机具名称' }, + { key: 'model', title: '规格型号' }, + { key: 'unit', title: '单位' }, + { key: 'quantity', title: '维修数量' }, + { key: 'cost', title: '维修费用(元)' } + ]; + fileName = `历史维修费用详情_${this.currentHistoryRow.agreementCode}_${this.historyReportParams.yearMonth}`; + sheetName = '历史维修费用'; + break; + case 'lose': + columns = [ + { key: 'index', title: '序号' }, + { key: 'name', title: '机具名称' }, + { key: 'model', title: '规格型号' }, + { key: 'unit', title: '单位' }, + { key: 'quantity', title: '丢失数量' }, + { key: 'cost', title: '丢失费用(元)' } + ]; + fileName = `历史丢失费用详情_${this.currentHistoryRow.agreementCode}_${this.historyReportParams.yearMonth}`; + sheetName = '历史丢失费用'; + break; + case 'scrap': + columns = [ + { key: 'index', title: '序号' }, + { key: 'name', title: '机具名称' }, + { key: 'model', title: '机具规格' }, + { key: 'unit', title: '单位' }, + { key: 'quantity', title: '报废数量' }, + { key: 'cost', title: '报废费用(元)' } + ]; + fileName = `历史报废费用详情_${this.currentHistoryRow.agreementCode}_${this.historyReportParams.yearMonth}`; + sheetName = '历史报废费用'; + break; + default: + this.$message.error('未知的费用类型'); + return; + } + + // 准备Excel数据 + const excelData = []; + + // 添加表头 + const headerRow = columns.map(col => col.title); + excelData.push(headerRow); + + // 添加数据行 + this.historyCostDetailList.forEach((row, index) => { + const dataRow = columns.map(col => { + let value = ''; + + if (col.key === 'index') { + value = index + 1; + } else if (['cost', 'unitPrice'].includes(col.key)) { + // 金额字段:转换为数字,保留2位小数 + const numValue = row[col.key] || 0; + value = parseFloat(parseFloat(numValue).toFixed(2)); + } else { + value = row[col.key] || ''; + } + + return value; + }); + excelData.push(dataRow); + }); + + // 创建工作簿和工作表 + const ws = XLSX.utils.aoa_to_sheet(excelData); + const wb = XLSX.utils.book_new(); + XLSX.utils.book_append_sheet(wb, ws, sheetName); + + // 设置列宽 + const colWidths = columns.map(() => ({ wch: 15 })); + ws['!cols'] = colWidths; + + // 设置边框和居中对齐 + const border = { + top: { style: 'thin' }, + bottom: { style: 'thin' }, + left: { style: 'thin' }, + right: { style: 'thin' } + }; + const alignment = { horizontal: 'center', vertical: 'center', wrapText: true }; + + // 遍历所有单元格,添加边框和居中对齐 + for (let row = 0; row < excelData.length; row++) { + for (let col = 0; col < excelData[row].length; col++) { + const cellAddress = XLSX.utils.encode_cell({ r: row, c: col }); + if (!ws[cellAddress]) { + ws[cellAddress] = {}; + } + ws[cellAddress].border = border; + ws[cellAddress].alignment = alignment; + } + } + + // 导出文件 + XLSX.writeFile(wb, `${fileName}.xlsx`); + + this.$message.success('导出成功'); + } catch (error) { + console.error('导出失败:', error); + this.$message.error('导出失败'); + } + }, + }, }