前端实现导入Excel进行数据展示、导出

需求

一个 excel 文档 需要对文档里面的数据进行筛选拆分重组 由于数据量巨大 后端又抽不出来手 于是使用纯前端解决方案

解决思路

前端导入excel

把 excel 的数据解析为 json 格式

对数据进行相应操作后

重新导出为新 excel

虽笨但有效

第一步 导入excel

该方案需引入以下第三方库

复制代码
xlsx.core.min.js

原生写法

复制代码
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
    </style>
</head>

<body>
    <span>excel导入:</span>
    <input type="file" onchange="importf(this)" />
    <p id="excelContent"></p>
</body>

<script src="./js/jQuery3.7.1min.js"></script>
<script src="https://cdn.bootcss.com/xlsx/0.11.5/xlsx.core.min.js"></script>
<script src="./js/dist_jquery.table2excel.min.js"></script>
<script>
    // 导入
    let data = null // 总数据

    var wb;//读取
    var rABS = false;

    //开始导入
    function importf(obj) {
        if (!obj.files) {
            return;
        }
        var f = obj.files[0];
        var reader = new FileReader();
        reader.onload = function (e) {
            var data = e.target.result;
            if (rABS) {
                wb = XLSX.read(btoa(fixdata(data)), {//手动转化
                    type: 'base64'
                });
            } else {
                wb = XLSX.read(data, {
                    type: 'binary'
                });
            }
            /**
             * wb.SheetNames[0]是获取Sheets中第一个Sheet的名字
             * wb.Sheets[Sheet名]获取第一个Sheet的数据
             */
            var excelJson = XLSX.utils.sheet_to_json(wb.Sheets[wb.SheetNames[0]]);
            console.log(excelJson);
            document.getElementById("excelContent").innerHTML = JSON.stringify(excelJson);
        };
        if (rABS) {
            reader.readAsArrayBuffer(f);
        } else {
            reader.readAsBinaryString(f);
        }
    }

    //文件流转BinaryString
    function fixdata(data) {
        var o = "",
            l = 0,
            w = 10240;
        for (; l < data.byteLength / w; ++l) o += String.fromCharCode.apply(null, new Uint8Array(data.slice(l * w, l * w +

            w)));
        o += String.fromCharCode.apply(null, new Uint8Array(data.slice(l * w)));
        return o;
    }

</script>

</html>

Vue写法

须在代码中稍作修改

复制代码
<input type="file" @change="importf" />



data() {
    wb: null,
    rABS: false,
}



methods:{
    // 导入excel
    importf(event) {
    const file = event.target.files[0];
    const reader = new FileReader();
    reader.onload = (e) => {
      const data = e.target.result;
      if (this.rABS) {
         this.wb = XLSX.read(btoa(fixdata(data)), {
           type: 'base64'
         });
       } else {
           this.wb = XLSX.read(data, {
           type: 'binary'
         });
       }
       const excelJson = XLSX.utils.sheet_to_json(this.wb.Sheets[this.wb.SheetNames[0]]);
       this.getTableData(excelJson)
     };
     if (this.rABS) {
       reader.readAsArrayBuffer(file);
     } else {
       reader.readAsBinaryString(file);
     }
   },
   //文件流转BinaryString
   fixdata(data) {
     let o = "",
     l = 0,
     w = 10240;
     for (; l < data.byteLength / w; ++l) o += String.fromCharCode.apply(null, new Uint8Array(data.slice(l * w, l * w + w)));
     o += String.fromCharCode.apply(null, new Uint8Array(data.slice(l * w)));
     return o;
   }
}

实现结果

第二步 修改导入进去的excel数据

显而易见 以原生写法为例 变量 excelJson 即为excel中导入出的数据

这里把数据赋值给了excelJson 变量

在此 可以对 excelJson 中的数据进行修改

!!

需要注意的是 如果仅仅是把 excel 中的数据导入到页面中进行展示

那看到这里就结束了 但如果需要导出 且是纯前端实现导出 看第三步

第三步 导出

纯前端实现表格导出 可以参考我以前的文章

前端JS导出Excel表格 可筛选列 table2excel_js-table2excel-CSDN博客

如果完全看完以上文章 我想你会明白要怎么做了

首先 excel 导入进来的数据处理后 需要渲染为 table 原生表格 形式 然后按照以上文章中的table2excel 该第三方库进行导出

非常easy

导出需要依赖 jquery 建议 vue 项目为后端导出 或寻找适合vue的table第三方库进行导出

原生项目的话 引入 jquery 库后 点击导出时 可以生成一个隐藏的 table dom 进行渲染 导出后对该 dom 进行删除 即可实现无感导出

相关推荐
UIUV34 分钟前
模块化CSS学习笔记:从作用域问题到实战解决方案
前端·javascript·react.js
aoi34 分钟前
解决 Vue 2 大数据量表单首次交互卡顿 10s 的性能问题
前端·vue.js
Kakarotto35 分钟前
使用ThreeJS绘制东方明珠塔模型
前端·javascript·vue.js
幽络源小助理35 分钟前
springboot校园车辆管理系统源码 – SpringBoot+Vue项目免费下载 | 幽络源
vue.js·spring boot·后端
donecoding36 分钟前
TypeScript `satisfies` 的核心价值:两个例子讲清楚
前端·javascript
德育处主任37 分钟前
『NAS』在群晖部署一个文件加密工具-hat.sh
前端·算法·docker
cup11339 分钟前
【原生 JS】支持加密的浏览器端 BYOK AI SDK,助力 Vibe Coding
前端
Van_Moonlight41 分钟前
RN for OpenHarmony 实战 TodoList 项目:顶部导航栏
javascript·开源·harmonyos
技术狂小子41 分钟前
前端开发中那些看似微不足道却影响体验的细节
javascript
用户120391129472642 分钟前
使用 Tailwind CSS 构建现代登录页面:从 Vite 配置到 React 交互细节
前端·javascript·react.js