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

feat: 新增 @pureadmin/table 导出 `Excel` 示例

xiaoxian521 2 роки тому
батько
коміт
ab44405b52

+ 2 - 2
src/views/able/execl.vue

@@ -64,7 +64,7 @@ const exportExcel = () => {
   const workSheet = utils.aoa_to_sheet(res);
   const workBook = utils.book_new();
   utils.book_append_sheet(workBook, workSheet, "数据报表");
-  writeFile(workBook, "tale-list.xlsx");
+  writeFile(workBook, "tableV2.xlsx");
 };
 </script>
 
@@ -83,7 +83,7 @@ const exportExcel = () => {
       </div>
     </template>
-    <el-button type="primary" @click="exportExcel">导出Excel </el-button>
+    <el-button type="primary" @click="exportExcel">导出Excel</el-button>
     <div class="h-[25rem] mt-3">
       <el-auto-resizer>
         <template #default="{ height, width }">

+ 50 - 0
src/views/pure-table/high/execl/columns.tsx

@@ -0,0 +1,50 @@
+import { ref } from "vue";
+import { utils, writeFile } from "xlsx";
+import { tableDataDrag } from "../data";
+import { clone } from "@pureadmin/utils";
+import { message } from "@pureadmin/components";
+
+export function useColumns() {
+  const dataList = ref(clone(tableDataDrag, true));
+
+  const columns: TableColumnList = [
+    {
+      label: "ID",
+      prop: "id"
+    },
+    {
+      label: "日期",
+      prop: "date"
+    },
+    {
+      label: "姓名",
+      prop: "name"
+    }
+  ];
+
+  const exportExcel = () => {
+    const res = dataList.value.map(item => {
+      const arr = [];
+      columns.forEach(column => {
+        arr.push(item[column.prop as string]);
+      });
+      return arr;
+    });
+    const titleList = [];
+    columns.forEach(column => {
+      titleList.push(column.label);
+    });
+    res.unshift(titleList);
+    const workSheet = utils.aoa_to_sheet(res);
+    const workBook = utils.book_new();
+    utils.book_append_sheet(workBook, workSheet, "数据报表");
+    writeFile(workBook, "pure-admin-table.xlsx");
+    message.success("导出成功");
+  };
+
+  return {
+    columns,
+    dataList,
+    exportExcel
+  };
+}

+ 18 - 0
src/views/pure-table/high/execl/index.vue

@@ -0,0 +1,18 @@
+<script setup lang="ts">
+import { useColumns } from "./columns";
+
+const { columns, dataList, exportExcel } = useColumns();
+</script>
+
+<template>
+  <div>
+    <el-button
+      type="primary"
+      @click="exportExcel"
+      class="mb-[20px] float-right"
+    >
+      导出Excel
+    </el-button>
+    <pure-table row-key="id" border :data="dataList" :columns="columns" />
+  </div>
+</template>

+ 7 - 0
src/views/pure-table/high/list.tsx

@@ -1,6 +1,7 @@
 import RowDrag from "./drag/row/index.vue";
 import ColumnDrag from "./drag/column/index.vue";
 import Contextmenu from "./contextmenu/index.vue";
+import Execl from "./execl/index.vue";
 
 const rendContent = (val: string) =>
   `代码位置:src/views/pure-table/high/${val}/index.vue`;
@@ -23,5 +24,11 @@ export const list = [
     content: rendContent("contextmenu"),
     title: "右键菜单",
     component: Contextmenu
+  },
+  {
+    key: "execl",
+    content: rendContent("execl"),
+    title: "导出execl",
+    component: Execl
   }
 ];