mirror of
https://github.com/pure-admin/vue-pure-admin.git
synced 2025-06-08 01:17:23 +08:00
feat: 新增 @pureadmin/table 导出 Excel
示例
This commit is contained in:
parent
c16ee6cf68
commit
ab44405b52
@ -64,7 +64,7 @@ const exportExcel = () => {
|
|||||||
const workSheet = utils.aoa_to_sheet(res);
|
const workSheet = utils.aoa_to_sheet(res);
|
||||||
const workBook = utils.book_new();
|
const workBook = utils.book_new();
|
||||||
utils.book_append_sheet(workBook, workSheet, "数据报表");
|
utils.book_append_sheet(workBook, workSheet, "数据报表");
|
||||||
writeFile(workBook, "tale-list.xlsx");
|
writeFile(workBook, "tableV2.xlsx");
|
||||||
};
|
};
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
@ -83,7 +83,7 @@ const exportExcel = () => {
|
|||||||
)
|
)
|
||||||
</div>
|
</div>
|
||||||
</template>
|
</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">
|
<div class="h-[25rem] mt-3">
|
||||||
<el-auto-resizer>
|
<el-auto-resizer>
|
||||||
<template #default="{ height, width }">
|
<template #default="{ height, width }">
|
||||||
|
50
src/views/pure-table/high/execl/columns.tsx
Normal file
50
src/views/pure-table/high/execl/columns.tsx
Normal file
@ -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
src/views/pure-table/high/execl/index.vue
Normal file
18
src/views/pure-table/high/execl/index.vue
Normal file
@ -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>
|
@ -1,6 +1,7 @@
|
|||||||
import RowDrag from "./drag/row/index.vue";
|
import RowDrag from "./drag/row/index.vue";
|
||||||
import ColumnDrag from "./drag/column/index.vue";
|
import ColumnDrag from "./drag/column/index.vue";
|
||||||
import Contextmenu from "./contextmenu/index.vue";
|
import Contextmenu from "./contextmenu/index.vue";
|
||||||
|
import Execl from "./execl/index.vue";
|
||||||
|
|
||||||
const rendContent = (val: string) =>
|
const rendContent = (val: string) =>
|
||||||
`代码位置:src/views/pure-table/high/${val}/index.vue`;
|
`代码位置:src/views/pure-table/high/${val}/index.vue`;
|
||||||
@ -23,5 +24,11 @@ export const list = [
|
|||||||
content: rendContent("contextmenu"),
|
content: rendContent("contextmenu"),
|
||||||
title: "右键菜单",
|
title: "右键菜单",
|
||||||
component: Contextmenu
|
component: Contextmenu
|
||||||
|
},
|
||||||
|
{
|
||||||
|
key: "execl",
|
||||||
|
content: rendContent("execl"),
|
||||||
|
title: "导出execl",
|
||||||
|
component: Execl
|
||||||
}
|
}
|
||||||
];
|
];
|
||||||
|
Loading…
x
Reference in New Issue
Block a user