vue项目中引用spreadjs方法

摘要:
spreadjs/scripts/gc.spred.sheets.all.13.0.min.js“>spreadjs/scripts/gc.sprad.sheets.resources.zh.13.0.minjs”>scripttype=“text/javascript”src=“<spreajs/scripts/FileSaver.min.js”>

1.下载插件

2.放到项目文件夹的public目录下面

vue项目中引用spreadjs方法第1张

 3.在项目根路径index.html中全局引入Spread插件的JS和CSS

vue项目中引用spreadjs方法第2张

<script type="text/javascript" src="<%= BASE_URL %>spreadjs/scripts/gc.spread.sheets.all.13.0.0.min.js"></script>
    <script type="text/javascript" src="<%= BASE_URL %>spreadjs/scripts/gc.spread.excelio.13.0.0.min.js"></script>
    <script type="text/javascript" src="<%= BASE_URL %>spreadjs/scripts/gc.spread.sheets.resources.zh.13.0.0.min.js"></script>
    <script type="text/javascript" src="<%= BASE_URL %>spreadjs/scripts/FileSaver.min.js"></script>

 4.在vue文件中使用

<template>
  <div class="app-container pull-auto">
    <basic-container>
      <div class="filter-container crud-menu">
        <div class="crud-menu_left">
          <input type="file" class="form-control" id="upload_file" name="upload_file" accept=".xlsx"
                 style="display: none;" @change='changeExcel'>
          <Input placeholder="请选择文件..." v-model="fileName" id="upload_file_tmp" style=" 250px"
                 onclick="upload_file.click();" readonly/>
          <Button type="primary" icon="md-arrow-round-up" @click="selectExcel">浏览</Button>
          <Button type="primary" icon="md-arrow-round-down" @click="saveExcel">导出</Button>
          <Button type="primary" icon="ios-egg" @click="saveTemplate">保存模板</Button>
        </div>
      </div>

      <div ref="excelView" id="excelView" :style="spreadStyle"></div>

    </basic-container>
  </div>

</template>
<style scoped>

</style>

<script>
  import axios from '@/router/axios'
  export default {
    data () {
      return {
        fileName: "",
        excelIo: {},
        spread: {},
        spreadStyle: {
           '100%',
          height: '430px'
        }
      };
    },
    mounted () {
      this.spread = new GC.Spread.Sheets.Workbook(document.getElementById("excelView"), {sheetCount: 1});
      this.excelIo = new GC.Spread.Excel.IO();
    },
    methods: {
      //上传EXCEL
      upExcel(){
        var excelFile = document.getElementById("upload_file").files[0];
        this.excelIo.open(excelFile, (json) => {
          var workbookObj = json;
          this.spread.fromJSON(workbookObj);
        }, function (e) {
          alert('文件读取失败,仅支持xlsx类型');
        });

      },
      //选择文件
      selectExcel(){
        upload_file.click();
      },
      changeExcel(){
        var excelFile = document.getElementById("upload_file").files[0];
        this.fileName = document.getElementById("upload_file").files[0].name;
        this.upExcel();
      },
      //保存EXCEL
      saveExcel(){
        //生成时间戳
        var fileName = new Date().getTime() + '.xlsx';
        var json = this.spread.toJSON();
        this.excelIo.save(json, (blob) => {
          saveAs(blob, fileName);
        }, function (e) {
          console.log(e);
        });
      },
      //保存模板
      saveTemplate(){
        //生成时间戳
        var fileName = new Date().getTime() + '.json';
        var json = this.spread.toJSON();
        var jsontext = JSON.stringify(json);
        //前台保存
        saveAs(new Blob([jsontext], {type: "text/plain;charset=utf-8"}), fileName);
        //保存后台
        let formData = new FormData();
        var thefile = document.getElementById("upload_file").files[0];
        formData.append("file", thefile);
        formData.append("fileName", thefile.name);
        axios.post('/admin/excelup/doImport', formData).then(function (response) {
          alert("模板保存成功");
        }).catch(function (error) {
          alert("上传失败");
          console.log(error);
        });

      }
    }
  }
</script>

若需要显示请求得到的excel而非从本地打开

<template ><!--xmlns="http://www.w3.org/1999/html" xmlns:v-on="http://www.w3.org/1999/xhtml"-->
    <div class="app-container pull-auto">
        <div :style='{height: winHeight}'>            
            <div style=" 250px; margin: 10px 0">
                <el-button type="primary" @click='reload'>刷新数据</el-button>

             </div>
            <div ref="excelView" id="excelView" :style="spreadStyle"></div>

        </div>
    </div>

</template>
<script>
  export default {
    data () {
      return {
        fileName: "",
        excelIo: {},
        spread: {},
        spreadStyle: {
           '98%',
          height: '100%'
        },
        signInfo: '',
        winHeight: '750',

      };
    },
    created(){
      const windiwHight = document.documentElement.clientHeight || document.body.clientHeight // 可视屏幕高度
      this.winHeight = windiwHight - 120 +'px'
    },
    mounted () {
      var defaultStyle = new GC.Spread.Sheets.Style();

    // 在mounted中声明 spread 和excelIo ,避免了改变某个参数后再次请求数据渲染界面的问题,若在 openExcel方法中声明会在该excel下方再生成一个excel
const spread = new GC.Spread.Sheets.Workbook(document.getElementById("excelView")); const excelIo = new GC.Spread.Excel.IO(); this.spread = spread this.excelIo = excelIo this.openExcel() }, methods: { reload(){ window.location.reload() }, openExcel(){ let spread = this.spread let excelIo = this.excelIo const excelFilePath = "需要请求的地址......"; const xhr = new XMLHttpRequest(); xhr.open('GET', excelFilePath, true); xhr.responseType = 'blob'; xhr.onload = function(e) { if (this.status == 200) { const blob = this.response; excelIo.open(blob, function (json) { spread.fromJSON(json); }, function (e) { alert(e.errorMessage); }, {}); } }; xhr.send(); }, } </script>

免责声明:文章转载自《vue项目中引用spreadjs方法》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇JavaScript如何隐藏网页滚动条?剖析 ERC721——了解非同质以太坊代币下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

cocos2D-X从的源代码的分析cocos2D-X学习OpenGL(1)----cocos2D-X渲染架构

        个人原创。欢迎转载,转载请注明原文地址http://blog.csdn.net/bill_man         从本篇文章開始,将分析cocos2D-X 3.0源码,第一部分是从cocos2D-X学习OpenGL,也就是分析cocos2D-X 3.0的渲染代码,本篇首先介绍cocos2D-X 3.0的渲染结构。使用的是3.0正式版。...

Oracle中索引的使用 索引性能优化调整

索引是由Oracle维护的可选结构,为数据提供快速的访问。准确地判断在什么地方需要使用索引是困难的,使用索引有利于调节检索速度。 当建立一个索引时,必须指定用于跟踪的表名以及一个或多个表列。一旦建立了索引,在用户表中建立、更改和删除数据库时, Oracle就自动地维护索引。创建索引时,下列准则将帮助用户做出决定:        1) 索引应该在SQL语句的...

IHttpHandler接口

IHttpHandler接口:定义Asp.net为了使用自定义Http处理程序同步处理Http Web请求而实现的协定。 说明:一旦定义的自己的HttpHandler,对系统的HttpHandler将是覆盖关系。 命名空间:System.Web 程序集:System.Web 语法:public interface IHttpHandler IHttpH...

tinyhttpd源码分析

  我们经常使用网页,作为开发人员我们也部署过httpd服务器,比如开源的apache,也开发过httpd后台服务,比如fastcgi程序,不过对于httpd服务器内部的运行机制,却不是非常了解,前几天看见tinyhttpd,只有短短500行左右的代码,就实现了一个简单的httpd服务器的基本功能,这种tiny程序,其实对于我们了解基本核心有一定的帮助,可...

echarts markLine 辅助线非直线设置

效果图: 用例option: option = { title: { text: '未来一周气温变化', subtext: '纯属虚构' }, tooltip: { trigger: 'axis' }, legend: { data:...

在linux系统中安装LANMP

1.安装LANMP步骤 root@kali:~# wget http://dl.wdlinux.cn/files/lanmp_v3.tar.gz #下载 root@kali:~# tar xzvf lanmp_v3.tar.gz #解压 root@kali:~# sh lanmp.sh #运行报错,原因:系统的dash兼容性不好,而编译常用的就是d...