Переглянути джерело

feat(examine-person-page): add excel export function for assessment data

1. 新增导出Excel按钮并默认隐藏
2. 添加公式列表获取接口请求方法
3. 实现考核分数自定义公式计算逻辑
4. 完成考核数据导出Excel的完整功能
SanHQin 2 днів тому
батько
коміт
edb738fbc4

+ 76 - 6
src/components/pages/test/examine/conpoments/personPage.vue

@@ -2,8 +2,11 @@
   <div>
     <div class="personPage">
       <div
-        style="width: 100%;display: flex;justify-content: flex-end;align-items: center;"
-      >
+       style="width: 100%;display: flex;justify-content: flex-end;align-items: center;"
+     >
+        <div style="margin-left: 10px;" v-show="false">
+          <el-button type="primary" @click="exportListExcel" size="mini">导出Excel</el-button>
+        </div>
         <!-- <el-select v-model="teas" multiple placeholder="请选择">
           <el-option
             v-for="item in TeaList"
@@ -468,8 +471,9 @@ export default {
       PageBaseDataTwo: [],
       remDig: false,
       timeSortList:[],
-      typeStatusList:["待办","已完成",'/'],
-      formula:""
+     typeStatusList:["待办","已完成",'/'],
+      formula:"",
+      formulaList:""
     };
   },
   watch: {
@@ -1467,9 +1471,75 @@ ws['!page'] = {
           this.$message.success("保存成功")
         })
         .catch(error => {
-          console.log(error);
+         console.log(error);
+       });
+   },
+
+    fetchFormulaForList() {
+      if (this.formulaList) return Promise.resolve(this.formulaList);
+      let params = { typ: this.pType, org: this.org, oid: this.oid };
+      return this.ajax.get(this.$store.state.api + "selectTestExamineBase", params).then(res => {
+        this.formulaList = res.data[0][0].formula ? res.data[0][0].formula : "";
+        return this.formulaList;
+      }).catch(() => { this.formulaList = ""; return ""; });
+    },
+
+    computeFormulaForPerson(personJson, formula) {
+      if (!formula || !personJson) return "0.00";
+      let scoreObj = {};
+      personJson.forEach(i => {
+        if (!scoreObj[i.id]) scoreObj[i.id] = 0;
+        if (i.children) {
+          i.children.forEach(i2 => {
+            if (i2.sco2) scoreObj[i.id] += parseFloat(i2.sco2);
+          });
+        }
+      });
+      let formulaText = formula;
+      for (const key in scoreObj) {
+        if (scoreObj.hasOwnProperty(key)) {
+          const regex = new RegExp("\\b" + key + "\\b", "g");
+          formulaText = formulaText.replace(regex, scoreObj[key]);
+        }
+      }
+      if (!formulaText) return "0.00";
+      try {
+        return eval(formulaText).toFixed(2);
+      } catch (e) {
+        return "0.00";
+      }
+    },
+
+    exportListExcel() {
+      this.fetchFormulaForList().then(formula => {
+        let headers = [["教师名称", "认定分", "加权分"]];
+        let rows = this.copyTableData.map(person => {
+          let cogSco = person.cogSco || "0.00";
+          let formulaScore = "0.00";
+          if (formula && person.json) {
+            formulaScore = this.computeFormulaForPerson(person.json, formula);
+          }
+          return [person.username || "", cogSco, formulaScore];
         });
-    }
+        let data = [...headers, ...rows];
+        const wb = XLSX.utils.book_new();
+        let ws = XLSX.utils.aoa_to_sheet(data);
+        const commonStyle = {
+          alignment: { horizontal: "center", vertical: "center", wrapText: true },
+          font: { sz: 12 }
+        };
+        for (let r = 0; r < data.length; ++r) {
+          for (let c = 0; c < data[r].length; ++c) {
+            const cellRef = XLSX.utils.encode_cell({ r, c });
+            ws[cellRef] = ws[cellRef] || {};
+            ws[cellRef].s = commonStyle;
+          }
+        }
+        ws["!cols"] = [{ wch: 20 }, { wch: 12 }, { wch: 12 }];
+        XLSX.utils.book_append_sheet(wb, ws, "考核列表");
+        XLSX.writeFile(wb, "考核数据导出.xlsx");
+      });
+    },
   }
 };
 </script>