vue2+element环境搭建利用xlsx实现excel解析预览效果

本文介绍了如何在Vue2项目中使用npm的xlsx库处理Excel文件上传,包括设置路由、组件设计以及解析Excel内容并显示在表格中。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 前言

95a01ceab4bc1cf494865905bf012428.png

今天继续带来vue2中关于表格的处理

   安装

ec60b892a84e3076eb646b884993551d.png

npm i xlsx

  安装过程

57088d0f79a3dce45c31d188ec653693.png

f55c679673071957aeca7f2db079a5cc.png

 页面增加一个路由

e53102d2c6d97fb66917c604def65473.png

import Vue from 'vue'
import VueRouter from 'vue-router'
import HomeView from '../views/HomeView.vue'




Vue.use(VueRouter)




const routes = [
  {
    path: '/',
    name: 'home',
    component: HomeView
  },
  {
    path: '/about',
    name: 'about',
    component: () => import('../views/AboutView.vue')
  },
  {
    path: '/excel',
    name: 'excel',
    component: () => import('../views/ExcelParse.vue')
  }
]




const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})




export default router

 增加一个主要文件

8ca80e50f7bb5a9183d98de9fd5ade30.png

<template>
  <div>
    <el-upload
      action="https://jsonplaceholder.typicode.com/posts/"
      :on-success="handleChange"
      :file-list="fileList"
      class="el-upload"
      >上传文件</el-upload
    >
    <el-table :data="tableData[0]" style="width: 100%">
      <el-table-column prop="name" label="姓名" width="180"> </el-table-column>
      <el-table-column prop="age" label="年龄" width="180"> </el-table-column>
    </el-table>
  </div>
</template>
  <script>
import { read, utils } from "xlsx";
export default {
  name: "Parse",
  data() {
    return {
      fileList: [],
      tableData: [],
      tableHead: [],
    };
  },
  computed: {},
  methods: {
    // 导入成功时执行
    handleChange(res, file, fileList) {
      // 将文件放入
      for (let i = 0; i < fileList.length; i++) {
        if (file.name != fileList[i].name) {
          this.fileList &&
            this.fileList.push({
              name: file.name,
              url: "",
              uid: file.uid,
            });
        }
      }
      const files = { 0: file };
      this.readExcel(files);
    },
    readExcel(file) {
      const fileReader = new FileReader();




      fileReader.onload = (ev) => {
        try {
          const data = ev.target.result;
          const workbook = read(data, { type: "binary" });
          const params = [];
          // 取对应表生成json表格内容
          workbook.SheetNames.forEach((item) => {
            this.tableData.push(utils.sheet_to_json(workbook.Sheets[item]));
          });
          // 该算法仅针对表头无合并的情况
          if (this.tableData.length > 0) {
            // 获取excel中第一个表格数据tableData[0][0],并且将表头提取出来
            for (const key in this.tableData[0][0]) {
              this.tableHead.push(key);
            }
          }
          console.log( this.tableData[0], "data is");
          // 重写数据
        } catch (e) {
          console.log("error:" + e);
          return false;
        }
      };
      fileReader.readAsBinaryString(file[0].raw);
    },
  },
};
</script>
  
  <style lang='scss'>
</style>

   制作表格 上传

11cca2bb14773ce19cec9c3d340ca0ba.png

baa92e247d1d724b62054d2b95ec8137.png

 运行结果

0747a6cd5b51f9a89c1a9c6aaa6bc74f.png

59ef804747efb132aa834dc1f06fbb41.png

点击上方蓝字关注我们

c1bbfc0ecd256e1c1f61eae936ea96c5.png

下方查看历史文章

2de23d23386c1f5cc2ab9a573c675b67.png

ES6模块化开发计算器小案例

ES5计算器小案例

原型和原型链深入

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值