VUE框架下搭建在线Excel应用

2019-10-28  本文已影响0人  小FFF

本项目实现功能:VUE框架中 嵌入表格控件SpreadJS,实现导入Excel、导出Excel、导出PDF、打印表格等

初始化VUE项目

参考文章:3分钟创建 SpreadJS 的 Vue 项目
项目运行效果:
本地的一个Excel文件:

image.png
导入Excel在项目中:
image.png

文章中的SpreadJS 版本是V11 的,现在最新版本已经是V13版本,我的例子中应用了SpreadJSV 12.2.5的版本,package.json 中添加的引用如下:

"dependencies": {
    "@grapecity/spread-excelio": "12.2.5",
    "@grapecity/spread-sheets": "12.2.5",
    "@grapecity/spread-sheets-pdf": "^12.2.5",
    "@grapecity/spread-sheets-print": "12.2.5",
    "@grapecity/spread-sheets-resources-zh": "12.2.5",
    "@grapecity/spread-sheets-vue": "12.2.5",
    "@grapecity/spread-sheets-charts": "12.2.5" ,
    "file-saver": "2.0.2",
    "jquery": "2.2.1",
    "vue": "^2.5.2",
    "vue-router": "^3.0.1"
  },

执行npm install 命令安装SpreadJS

导出PDF功能注意事项

   savePdf(){
         let self = this;
        let jsonString = JSON.stringify(self.spread.toJSON());
        let printSpread = new GC.Spread.Sheets.Workbook();
        printSpread.fromJSON(JSON.parse(jsonString));
    
        printSpread.savePDF(function(blob) {    
                // window.open(URL.createObjectURL(blob))   
                FaverSaver.saveAs(blob,  'Hello.pdf')
                }, function(error) {
                console.log(error);
                }, {
                title: 'Print',
            });  
    }

导入导出Excel

exportXlsx () {
      let ex = new ExcelIO.IO()
      let json = this.spread.toJSON()
      ex.save(json, function (blob) {
        FaverSaver.saveAs(blob, 'export.xlsx')
      }, function (e) {
        console.log(e)
      })
    },
    importXlsx(){
       let self = this;
        var excelIO = new ExcelIO.IO();
        console.log(excelIO);
        const excelFile = document.getElementById("fileDemo").files[0];
      excelIO.open(excelFile, function (json) {
          let workbookObj = json;
          self.spread.fromJSON(workbookObj);
        }, function (e) {
            alert(e.errorMessage);
        });
    }

代码下载地址点这里

上一篇下一篇

猜你喜欢

热点阅读