ImportExcel.vue 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. <template>
  2. <PageWrapper title="excel数据导入示例">
  3. <ImpExcel @success="loadDataSuccess" dateFormat="YYYY-MM-DD">
  4. <a-button class="m-3"> 导入Excel </a-button>
  5. </ImpExcel>
  6. <BasicTable v-for="(table, index) in tableListRef" :key="index" :title="table.title" :columns="table.columns" :dataSource="table.dataSource" />
  7. </PageWrapper>
  8. </template>
  9. <script lang="ts">
  10. import { defineComponent, ref } from 'vue';
  11. import { ImpExcel, ExcelData } from '/@/components/Excel';
  12. import { BasicTable, BasicColumn } from '/@/components/Table';
  13. import { PageWrapper } from '/@/components/Page';
  14. export default defineComponent({
  15. components: { BasicTable, ImpExcel, PageWrapper },
  16. setup() {
  17. const tableListRef = ref<
  18. {
  19. title: string;
  20. columns?: any[];
  21. dataSource?: any[];
  22. }[]
  23. >([]);
  24. function loadDataSuccess(excelDataList: ExcelData[]) {
  25. tableListRef.value = [];
  26. console.log(excelDataList);
  27. for (const excelData of excelDataList) {
  28. const {
  29. header,
  30. results,
  31. meta: { sheetName },
  32. } = excelData;
  33. const columns: BasicColumn[] = [];
  34. for (const title of header) {
  35. columns.push({ title, dataIndex: title });
  36. }
  37. tableListRef.value.push({ title: sheetName, dataSource: results, columns });
  38. }
  39. }
  40. return {
  41. loadDataSuccess,
  42. tableListRef,
  43. };
  44. },
  45. });
  46. </script>