import.vue 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <script setup lang="ts">
  2. import { ElMessage } from 'element-plus'
  3. import { useExcel } from '@/hooks/useExcel'
  4. const { importExcel, toTwoArray } = useExcel()
  5. // 表格数据
  6. const tableData = ref<Record<string, any>[]>([])
  7. // 表格二维数据
  8. const tableDataTwo = ref<Record<string, any>[]>([])
  9. // 表头数据
  10. const column = ref<Array<string>>(['A', 'B', 'C', 'D', 'E', 'F', 'G'])
  11. const importFile = (file: File) => {
  12. importExcel(file).then(({ title, list }: any) => {
  13. column.value = title
  14. tableData.value = list
  15. tableDataTwo.value = toTwoArray(list)
  16. })
  17. return false
  18. }
  19. const getTwoData = () => {
  20. console.log(tableDataTwo.value)
  21. ElMessage.success('数据已打印在控制台')
  22. }
  23. </script>
  24. <template>
  25. <el-card shadow="never">
  26. <div class="flex">
  27. <el-upload action="" accept=".xls,.xlsx" :show-upload-list="false" :before-upload="importFile">
  28. <el-button type="primary">导入</el-button>
  29. </el-upload>
  30. <el-button type="primary" @click="getTwoData" class="ml-3">获取表格二维数据</el-button>
  31. </div>
  32. <table class="table" border="1">
  33. <colgroup>
  34. <col width="52" />
  35. <col v-for="item in column" :key="item" />
  36. </colgroup>
  37. <thead>
  38. <tr>
  39. <th></th>
  40. <th v-for="item in column" :key="item" style="text-align: center">
  41. {{ item }}
  42. </th>
  43. </tr>
  44. </thead>
  45. <tbody>
  46. <tr v-for="(item, index) in tableData" :key="index">
  47. <td style="text-align: center">{{ index + 1 }}</td>
  48. <template v-for="key in column">
  49. <td
  50. v-if="item[key]?.colspan !== 0 && item[key].rowspan !== 0"
  51. :key="key"
  52. :colspan="item[key].colspan"
  53. :rowspan="item[key].rowspan"
  54. style="text-align: center"
  55. >
  56. {{ item[key].value }}
  57. </td>
  58. </template>
  59. </tr>
  60. <tr v-if="!tableData.length">
  61. <td :colspan="column.length + 1" style="text-align: center; background: none">暂无数据</td>
  62. </tr>
  63. </tbody>
  64. </table>
  65. </el-card>
  66. </template>
  67. <style scoped>
  68. .table {
  69. width: 100%;
  70. border-collapse: collapse;
  71. border-spacing: 1px;
  72. border-color: var(--el-border-color);
  73. }
  74. </style>