importExcel.vue 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <template>
  2. <div class="excel-import-container">
  3. <!-- 文件上传按钮 -->
  4. <el-upload
  5. class="upload-btn"
  6. action="#"
  7. :auto-upload="false"
  8. :show-file-list="false"
  9. :on-change="handleFileChange"
  10. accept=".xlsx, .xls"
  11. >
  12. <el-button type="primary" :icon="Upload">导入 Excel</el-button>
  13. </el-upload>
  14. <!-- 解析结果展示 -->
  15. <div v-if="tableData.length > 0" class="result-table">
  16. <span>
  17. <span style="font-weight: 900; font-size: 18px;">解析结果 (共 {{ tableData.length }} 条)</span>
  18. <span style="margin-left: 10px; color: #ff0000;">注意:如果Excel中有重复id字段,会导致数据覆盖</span>
  19. </span>
  20. <!-- <el-table :data="tableData" border style="width: 100%">
  21. <el-table-column
  22. v-for="(key, index) in Object.keys(tableData[0])"
  23. :key="index"
  24. :prop="key"
  25. :label="key"
  26. />
  27. </el-table> -->
  28. <pure-table
  29. row-key="id"
  30. align-whole="center"
  31. :header-cell-style="{
  32. background: 'var(--el-fill-color-light)',
  33. color: 'var(--el-text-color-primary)'
  34. }"
  35. :columns="columns"
  36. :table-key="tableKey"
  37. border
  38. :adaptive="false"
  39. showOverflowTooltip
  40. :loading="false"
  41. :data="
  42. tableData.slice(
  43. (pagination.currentPage - 1) * pagination.pageSize,
  44. pagination.currentPage * pagination.pageSize
  45. )
  46. "
  47. :pagination="pagination"
  48. @page-size-change="onSizeChange"
  49. @page-current-change="onCurrentChange"
  50. >
  51. </pure-table>
  52. </div>
  53. </div>
  54. </template>
  55. <script setup>
  56. import { ref, onMounted, reactive } from 'vue';
  57. import * as XLSX from 'xlsx';
  58. import { delObjectProperty } from "@pureadmin/utils";
  59. import { Upload } from '@element-plus/icons-vue';
  60. /** 分页配置 */
  61. const pagination = reactive({
  62. pageSize: 10,
  63. currentPage: 1,
  64. pageSizes: [10, 20, 40, 60],
  65. total: 0,
  66. align: "right",
  67. background: true,
  68. size: "default"
  69. });
  70. // 1. 声明要触发的自定义事件(可选,用于类型提示和校验)
  71. const emit = defineEmits(['onExcelData']);
  72. // 存储解析后的表格数据
  73. const tableData = ref([]);
  74. const columns = ref([]);
  75. // 处理文件上传
  76. const handleFileChange = (file) => {
  77. // 获取文件对象
  78. const rawFile = file.raw;
  79. if (!rawFile) return;
  80. // 创建文件读取器
  81. const reader = new FileReader();
  82. // 读取文件内容
  83. reader.onload = (e) => {
  84. try {
  85. // 解析 Excel 内容
  86. const data = e.target.result;
  87. const workbook = XLSX.read(data, { type: 'binary' });
  88. // 取第一个工作表(可根据实际需求修改)
  89. const firstSheetName = workbook.SheetNames[0];
  90. const worksheet = workbook.Sheets[firstSheetName];
  91. // 转换为 JSON 格式(header: 1 表示以数组形式返回,header: 'A' 表示带表头)
  92. const jsonData = XLSX.utils.sheet_to_json(worksheet, { header: 1 });
  93. // 处理表头和数据(如果需要结构化数据)
  94. if (jsonData.length > 0) {
  95. const headers = jsonData[0]; // 表头行
  96. const content = jsonData.slice(1); // 内容行
  97. console.log('Headers:', headers);
  98. for (const v of headers) {
  99. columns.value.push({
  100. label: v,
  101. prop: v
  102. });
  103. }
  104. // 转换为 { 表头1: 值1, 表头2: 值2 } 格式
  105. tableData.value = content.map(row => {
  106. const obj = {};
  107. headers.forEach((header, index) => {
  108. obj[header] = row[index];
  109. });
  110. if (!obj.id) {
  111. return null;
  112. } else {
  113. return obj;
  114. }
  115. });
  116. pagination.total = tableData.value.length;
  117. emit('onExcelData', tableData.value); // 触发自定义事件,传递解析后的数据
  118. }
  119. } catch (error) {
  120. console.error('Excel 解析失败:', error);
  121. alert('导入失败,请检查文件格式是否正确');
  122. }
  123. };
  124. // 以二进制方式读取文件
  125. reader.readAsBinaryString(rawFile);
  126. };
  127. onMounted(() => {
  128. // 初始化
  129. tableData.value = [];
  130. columns.value = [];
  131. });
  132. </script>
  133. <style scoped>
  134. .excel-import-container {
  135. padding: 20px;
  136. }
  137. .upload-btn {
  138. margin-bottom: 20px;
  139. }
  140. .result-table {
  141. margin-top: 20px;
  142. }
  143. h3 {
  144. margin-bottom: 10px;
  145. color: #333;
  146. }
  147. </style>