ImportExcel.vue 1.5 KB

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