Vue3 + Element Plus 导入功能

ElementPlus

<script setup lang="ts">

import { reactive, ref } from 'vue'
import * as XLSX from 'xlsx';


function readExcel(uploadFile) {

  return new Promise((resolve, reject) => {
    debugger
    const file = uploadFile.raw
    if (!file) {
      resolve(null);
      return
    }
    const reader = new FileReader();
    reader.onload = function (e) {
      const bytes = e.target.result;
      const workbook = XLSX.read(bytes, {
        type: 'binary'
      });
      // 获取第一个工作表
      const sheetName = workbook.SheetNames[0]
      const worksheet = workbook.Sheets[sheetName]

      // 将工作表转换为JSON
      const data = XLSX.utils.sheet_to_json(worksheet, { header: 1 })

      if (data.length === 0) {
        throw new Error('Excel文件为空')
      }

      // 获取表头
      const headers = data[0]

      // 查找"监测地点"列
      const locationColumnIndex = headers.findIndex(
        (header) => typeof header === 'string' && header.includes('监测地点')
      )

      // 查找"搜索名"列
      const searchColumnIndex = headers.findIndex(
        (header) => typeof header === 'string' && header.includes('搜索名')
      )

      // 提取数据行(跳过标题行)
      const rows = []
      for (let i = 1; i < data.length; i++) {
        const row = data[i]
        // 确保行数据存在且监测地点不为空
        if (row && row[locationColumnIndex] && row[locationColumnIndex].toString().trim() !== '') {
          rows.push(row)
        }
      }

      resolve({
        headers: headers,
        data: rows,
        locationColumnIndex: locationColumnIndex,
        searchColumnIndex: searchColumnIndex,
        originalLocationColumnIndex: locationColumnIndex // 保存原始位置列索引
      });
    };

    reader.readAsArrayBuffer(file)
  })

}

</script>

<template>

<el-upload
      class="upload-demo"
      action="#"
      :auto-upload="false"
      :on-change="handleFileChange"
      accept=".xlsx,.xls"
      drag
    >
      <el-icon class="el-icon--upload"><upload-filled /></el-icon>
      <div class="el-upload__text">将文件拖到此处,或<em>点击上传</em></div>
    </el-upload>

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值