Browse Source

导出Excel优化

gzb
wangzhonglu 8 months ago
parent
commit
f19b6c6b99

+ 40
- 1
src/utils/exportToExcel.js View File

@@ -57,6 +57,42 @@ const openDownloadDialog = (blob, fileName) => {
57 57
   }
58 58
   aLink.dispatchEvent(event);
59 59
 };
60
+// 列自适应宽度
61
+const setColFitWidth = (tableData, headerList, sheet) => {
62
+  function getCellWidth(value) {
63
+    // 判断是否为null或undefined
64
+    if (!value) {
65
+      return 10;
66
+    } else if (/.*[\u4e00-\u9fa5]+.*$/.test(value)) {
67
+      // 判断是否包含中文
68
+      return value.toString().length * 2.1;
69
+    } else {
70
+      return value.toString().length * 1.1;
71
+    }
72
+  }
73
+  let colWidths = []; // 所有列的名称数组
74
+  // 计算每一列的所有单元格宽度
75
+  // 先遍历行
76
+  tableData.forEach((row) => {
77
+    // 遍历列
78
+    for (const key in row) {
79
+      let index = headerList.findIndex((v) => v === key);
80
+      if (!colWidths[index]) {
81
+        colWidths[index] = [];
82
+      }
83
+      colWidths[index].push(getCellWidth(row[key]));
84
+    }
85
+  });
86
+  sheet["!cols"] = [];
87
+  // 每一列取最大值最为列宽
88
+  colWidths.forEach((widths, index) => {
89
+    // 计算列头的宽度
90
+    widths.push(getCellWidth(headerList[index]));
91
+    // 设置最大值为列宽
92
+    sheet["!cols"].push({ wch: Math.max(...widths, 10) });
93
+  });
94
+  return sheet;
95
+};
60 96
 /**
61 97
  * 导出Excel表格
62 98
  * @param sheetListInfo sheet列表信息 [{list:数组数据, name: sheet名称}]
@@ -65,12 +101,14 @@ const openDownloadDialog = (blob, fileName) => {
65 101
 export const exportToExcel = (sheetListInfo, excelName) => {
66 102
   const wb = XLSX2.utils.book_new();
67 103
   sheetListInfo.forEach((sheetItem, sheetIndex) => {
104
+    let headerList = [];
68 105
     sheetItem.list.forEach((item) => {
69 106
       for (const key in item) {
107
+        headerList.push(key);
70 108
         item[key] = item[key] || "";
71 109
       }
72 110
     });
73
-    const sheet = XLSX2.utils.json_to_sheet(sheetItem.list);
111
+    let sheet = XLSX2.utils.json_to_sheet(sheetItem.list);
74 112
     // 设置每个单元格的样式
75 113
     let range = XLSX.utils.decode_range(sheet["!ref"]);
76 114
     for (let R = range.s.r; R <= range.e.r; ++R) {
@@ -83,6 +121,7 @@ export const exportToExcel = (sheetListInfo, excelName) => {
83 121
         }
84 122
       }
85 123
     }
124
+    sheet = setColFitWidth(sheetItem.list, headerList, sheet);
86 125
     XLSX2.utils.book_append_sheet(
87 126
       wb,
88 127
       sheet,

+ 3
- 0
src/views/platformSection/regionManage/adminManage.vue View File

@@ -346,6 +346,7 @@ export default {
346 346
         timer: null,
347 347
         second: 60,
348 348
         adminid: null,
349
+        atype: null,
349 350
         aname: "",
350 351
         loginname: "",
351 352
         loginpwd: "",
@@ -492,6 +493,7 @@ export default {
492 493
         timer: null,
493 494
         second: 60,
494 495
         adminid: null,
496
+        atype: null,
495 497
         aname: "",
496 498
         loginname: "",
497 499
         loginpwd: generateRandomString(),
@@ -622,6 +624,7 @@ export default {
622 624
         timer: null,
623 625
         second: 60,
624 626
         adminid: row.adminid,
627
+        atype: row.atype,
625 628
         aname: row.aname,
626 629
         loginname: row.loginname,
627 630
         loginpwd: row.loginpwd,

+ 10
- 10
src/views/schoolSection/deviceManage/deviceManage.vue View File

@@ -885,17 +885,17 @@ export default {
885 885
               value: params.value,
886 886
               unit: "B"
887 887
             };
888
-            if (memoryInfo.value / 1024 < 1024) {
888
+            if (memoryInfo.value / 1000 < 1000) {
889 889
               memoryInfo.value =
890
-                Math.floor((memoryInfo.value / 1024) * 100) / 100;
890
+                Math.floor((memoryInfo.value / 1000) * 100) / 100;
891 891
               memoryInfo.unit = "KB";
892
-            } else if (memoryInfo.value / 1024 / 1024 < 1024) {
892
+            } else if (memoryInfo.value / 1000 / 1000 < 1000) {
893 893
               memoryInfo.value =
894
-                Math.floor((memoryInfo.value / 1024 / 1024) * 100) / 100;
894
+                Math.floor((memoryInfo.value / 1000 / 1000) * 100) / 100;
895 895
               memoryInfo.unit = "MB";
896 896
             } else {
897 897
               memoryInfo.value =
898
-                Math.floor((memoryInfo.value / 1024 / 1024 / 1024) * 100) / 100;
898
+                Math.floor((memoryInfo.value / 1000 / 1000 / 1000) * 100) / 100;
899 899
               memoryInfo.unit = "GB";
900 900
             }
901 901
             return `${params.name}:${memoryInfo.value}${memoryInfo.unit}`;
@@ -927,17 +927,17 @@ export default {
927 927
                   value: params.value,
928 928
                   unit: "B"
929 929
                 };
930
-                if (memoryInfo.value / 1024 < 1024) {
930
+                if (memoryInfo.value / 1000 < 1000) {
931 931
                   memoryInfo.value =
932
-                    Math.floor((memoryInfo.value / 1024) * 100) / 100;
932
+                    Math.floor((memoryInfo.value / 1000) * 100) / 100;
933 933
                   memoryInfo.unit = "KB";
934
-                } else if (memoryInfo.value / 1024 / 1024 < 1024) {
934
+                } else if (memoryInfo.value / 1000 / 1000 < 1000) {
935 935
                   memoryInfo.value =
936
-                    Math.floor((memoryInfo.value / 1024 / 1024) * 100) / 100;
936
+                    Math.floor((memoryInfo.value / 1000 / 1000) * 100) / 100;
937 937
                   memoryInfo.unit = "MB";
938 938
                 } else {
939 939
                   memoryInfo.value =
940
-                    Math.floor((memoryInfo.value / 1024 / 1024 / 1024) * 100) /
940
+                    Math.floor((memoryInfo.value / 1000 / 1000 / 1000) * 100) /
941 941
                     100;
942 942
                   memoryInfo.unit = "GB";
943 943
                 }

+ 2
- 2
src/views/schoolSection/log/deviceEvents.vue View File

@@ -237,9 +237,9 @@ export default {
237 237
                     // 0不违规1违规
238 238
                     违规:
239 239
                       item.violated === 1
240
-                        ? "违规"
240
+                        ? ""
241 241
                         : item.violated === 0
242
-                        ? "不违规"
242
+                        ? ""
243 243
                         : "",
244 244
                     时间: item.createtime
245 245
                   };

Loading…
Cancel
Save