VUE框架下搭建在线Excel应用

本项目实现功能: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功能注意事项

  • 安装相同版本的 pdf的包: "@grapecity/spread-sheets-pdf"
  • 在需要打印的页面引入该包: import "@grapecity/spread-sheets-pdf";
  • 引入该包需要注意引入顺序,先引入 @grapecity/spread-sheets和 grapecity/spread-sheets-print
  • 需引入第三方插件file-saver : import FaverSaver from 'file-saver'
  • 如下几行代码可实现导出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

  • 安装相关的包: "@grapecity/spread-excelio"、 "file-saver"
  • 在页面中引入: import ExcelIO from '@grapecity/spread-excelio'、import FaverSaver from 'file-saver'
  • 如下代码可实现导入导出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);
        });
    }

代码下载地址点这里

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 12,199评论 0 10
  • jHipster - 微服务搭建 CC_简书[https://www.jianshu.com/u/be0d56c4...
    quanjj阅读 4,306评论 0 2
  • ## 框架和库的区别?> 框架(framework):一套完整的软件设计架构和**解决方案**。> > 库(lib...
    Rui_bdad阅读 8,204评论 1 4
  • 把努力活成一种习惯。 前几天发布了一篇文章《想改变自己?试试提升这5种能力!》。收到了很多简友的来信,有咨询如何找...
    何以笙箫默1516阅读 3,122评论 0 0
  • 尊重长辈,尊重丈夫,尊重妻子,如果没有了尊重,好像人生就没有什么意义。 今天看综合第一套沙溢一家人,孩子问妈妈会不...
    喜欢厨房的人阅读 1,072评论 0 1

友情链接更多精彩内容