zoukankan      html  css  js  c++  java
  • 纯前端 vue+ js-xlsx 导入excel表格

    <template>
    <div class="index">
    <input type="file" @change="importFile(this)" id="imFile" style="display: none"
    accept="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel"/>
    <Button type="primary" @click="uploadFile">导入</Button>
    </div>
    </template>

    <script>
    // 引入xlsx
    var XLSX = require("xlsx");
    export default {
    name: "Index",
    data() {
    return {
    fullscreenLoading: false, // 加载中
    imFile: "", // 导入文件el
    errorMsg: "", // 错误信息内容
    };
    },
    mounted() {
    this.imFile = document.getElementById("imFile");
    },
    methods: {
    uploadFile: function() {
    // 点击导入按钮
    this.imFile.click();
    },

    importFile: function() {
    // 导入excel
    this.fullscreenLoading = true;
    let obj = this.imFile;
    if (!obj.files) {
    this.fullscreenLoading = false;
    return;
    }
    var f = obj.files[0];
    var reader = new FileReader();
    let $t = this;
    reader.onload = function(e) {
    var data = e.target.result;
    if ($t.rABS) {
    $t.wb = XLSX.read(btoa(this.fixdata(data)), {
    // 手动转化
    type: "base64"
    });
    } else {
    $t.wb = XLSX.read(data, {
    type: "binary"
    });
    }
    let json = XLSX.utils.sheet_to_json($t.wb.Sheets[$t.wb.SheetNames[0]]);
    $t.dealFile(json); // analyzeData: 解析导入数据
    };
    if (this.rABS) {
    reader.readAsArrayBuffer(f);
    } else {
    reader.readAsBinaryString(f);
    }
    },
    dealFile: function(data) {
    // 处理导入的数据
    console.log(data);
    this.imFile.value = "";
    this.fullscreenLoading = false;
    if (data.length <= 0) {
    this.errorMsg = "请导入正确信息";
    } else {
    //导入成功,处理数据
    this.$http.post("collector/import", data).then(
    function(response) {
    if (response.body.status == 1) {

    } else {
    if (response.body.status == "10101") {
    this.$Message.info("未登录");
    this.$router.push("/");
    }
    this.$Message.info(response.body.message);
    }
    },
    function() {}
    );
    }
    },

    fixdata: function(data) {
    // 文件流转BinaryString
    var o = "";
    var l = 0;
    var 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>


  • 相关阅读:
    .NET 内存分配笔记
    MYSQL知识点
    NOPI导入导出
    【链接】各类学习资源
    【原创】重绘winform的GroupBox
    高仿淘宝滑动验证码插件
    Winform窗体控件级权限处理
    .NET中的Func委托用法
    关于IBatisNet的配置文件中数据库连接字符串加密处理
    Oracle连接字符串大全
  • 原文地址:https://www.cnblogs.com/weiyuanquyu/p/9811581.html
Copyright © 2011-2022 走看看