Forráskód Böngészése

节点分析导出修改

zhongxiaoyu 2 éve
szülő
commit
9b1ecd49a1

+ 34 - 9
src/views/statisticsCharts/views/nodeStatisticsCharts.vue

@@ -558,6 +558,7 @@ export default {
         if (Number(res.code) === 0) {
           const { listValues } = res.returnData
           if (listValues.length) {
+            // 生成表格数据
             const xlsxDatas = [[]]
             const listArray = listValues.map(record =>
               Object.entries(record).filter(([key, value]) => key === 'a4' || this.checkList.includes(key))
@@ -565,12 +566,35 @@ export default {
             xlsxDatas[0].push(...listArray[0].map(([key, value]) => key))
             xlsxDatas.push(...listArray.map(record => record.map(([key, value]) => value)))
             xlsxDatas[0][0] = '日期'
+            // 添加合计行
+            if (xlsxDatas.length > 2) {
+              const summaryRow = ['合计']
+              const colNum = xlsxDatas[0].length
+              for (let columnIndex = 1; columnIndex < colNum; columnIndex++) {
+                summaryRow[columnIndex] = xlsxDatas.reduce((pre, currentRow, currentRowIndex) => {
+                  if (currentRowIndex === 0) {
+                    return pre
+                  } else {
+                    return pre + currentRow[columnIndex]
+                  }
+                }, 0)
+              }
+              xlsxDatas.push(summaryRow)
+            }
+            // 添加节点扫描率列,计算列宽
             const columnWidths = []
-            const rowNum = xlsxDatas.length
-            for (let rowIndex = 0; rowIndex < rowNum; rowIndex++) {
-              const colNum = xlsxDatas[rowIndex].length
-              for (let columnIndex = 0; columnIndex < colNum; columnIndex++) {
-                const cellTextLength = xlsxDatas[rowIndex][columnIndex]
+            xlsxDatas.forEach((row, rowIndex) => {
+              // 从行李总件数后一列开始遍历,添加节点扫描率数据
+              for (let columnIndex = 2; columnIndex < row.length; columnIndex += 2) {
+                const totalcell = row[1]
+                const cell = row[columnIndex]
+                const newCell =
+                  rowIndex === 0 ? `${cell.slice(0, 2)}扫描率` : `${cell ? ((cell / totalcell) * 100).toFixed(2) : 0}%`
+                row.splice(columnIndex + 1, 0, newCell)
+              }
+              // 计算每一列宽度
+              row.forEach((cell, columnIndex) => {
+                const cellTextLength = cell
                   .toString()
                   .split('')
                   .reduce((pre, curr) => {
@@ -580,22 +604,23 @@ export default {
                 if ((!columnWidths[columnIndex] && cellTextLength > 0) || cellTextLength > columnWidths[columnIndex]) {
                   columnWidths[columnIndex] = cellTextLength
                 }
-              }
-            }
+              })
+            })
+            // 生成表格
             const sheet = XLSX.utils.aoa_to_sheet(xlsxDatas)
+            // 添加列宽度
             sheet['!cols'] = columnWidths.map(width => ({
               wch: width + 2
             }))
             const workBook = XLSX.utils.book_new()
             XLSX.utils.book_append_sheet(workBook, sheet, '扫描节点与位置分析')
-            // console.log(workBook)
-            // return
             const tableWrite = XLSX_STYLE.write(workBook, {
               bookType: 'xlsx',
               bookSST: true,
               type: 'buffer',
               cellStyles: true
             })
+            // 下载表格
             const fileName = `扫描节点与位置分析-${queryData.join('-')}.xlsx`
             FileSaver.saveAs(new Blob([tableWrite], { type: 'application/octet-stream' }), fileName)
           }