Przeglądaj źródła

perfect_importExcel

ananzhusen 2 lat temu
rodzic
commit
a2d9114150
2 zmienionych plików z 18 dodań i 12 usunięć
  1. 14 12
      src/services/excel.ts
  2. 4 0
      src/views/components/View.vue

+ 14 - 12
src/services/excel.ts

@@ -1,17 +1,17 @@
-import ExcelJS from "exceljs";
-import { saveAs } from "file-saver";
-import { MessagePlugin } from "tdesign-vue-next";
+import ExcelJS from 'exceljs';
+import { saveAs } from 'file-saver';
+import { MessagePlugin } from 'tdesign-vue-next';
 
 export async function importExcel(columns: any[]) {
   return new Promise((resolve, reject) => {
-    const input = document.createElement("input");
-    input.type = "file";
+    const input = document.createElement('input');
+    input.type = 'file';
     input.accept =
-      ".csv, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel,application/zip";
+      '.csv, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel,application/zip';
     input.onchange = (event) => {
       const elem: any = event.target;
 
-      if (elem.files && elem.files[0].name.indexOf(".xlsx") > 0) {
+      if (elem.files && elem.files[0].name.indexOf('.xlsx') > 0) {
         const workbook = new ExcelJS.Workbook();
         const reader = new FileReader();
         reader.readAsArrayBuffer(elem.files[0]);
@@ -23,7 +23,7 @@ export async function importExcel(columns: any[]) {
           // 获取sheet1的所有rows
           const rows = worksheet.getSheetValues();
           if (rows.length === 0) {
-            MessagePlugin.warning("导入的excel文件不可为空!");
+            MessagePlugin.warning('导入的excel文件不可为空!');
             return;
           }
           let data: any = [];
@@ -35,7 +35,9 @@ export async function importExcel(columns: any[]) {
                 let _index = columns.findIndex(
                   (item) => item.header === cell.value
                 );
-                indexKeyMap[colNumber] = columns[_index].key;
+                if (_index !== -1) {
+                  indexKeyMap[colNumber] = columns[_index].key;
+                }
               } else {
                 _data[indexKeyMap[colNumber]] = cell.value;
               }
@@ -45,7 +47,7 @@ export async function importExcel(columns: any[]) {
             }
           });
           resolve(data);
-          MessagePlugin.success("导入成功!");
+          MessagePlugin.success('导入成功!');
         };
       }
     };
@@ -71,9 +73,9 @@ export function saveAsExcel(
   worksheet.addRows(data);
   workbook.xlsx.writeBuffer().then((data) => {
     const blob = new Blob([data], {
-      type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=UTF-8",
+      type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=UTF-8',
     });
     saveAs(blob, fileName);
-    MessagePlugin.success("导出成功!");
+    MessagePlugin.success('导出成功!');
   });
 }

+ 4 - 0
src/views/components/View.vue

@@ -1018,6 +1018,10 @@ const importDataset = async () => {
       header: '类型',
       key: 'type',
     },
+    {
+      header: '场景',
+      key: 'case',
+    },
     {
       header: '值',
       key: 'value',