找回密码
 立即注册

QQ登录

只需一步,快速开始

随机用户

金牌服务用户

2

主题

8

帖子

20

积分

金牌服务用户

积分
20
最新发帖
随机用户
金牌服务用户   /  发表于:2021-3-29 13:38  /   查看:1863  /  回复:4
本帖最后由 随机用户 于 2021-3-29 13:43 编辑
  1. <template>
  2. <div class="com-pannel-wrp">
  3.     <button @click="getSpread">获取spread对象</button>
  4.     <div class="sheet-area" style="text-align: left;width:80vw;height:100vh;" id="hostDiv">
  5.         <gc-spread-sheets-designer :styleInfo='styleInfo' :config='config' @designerInitialized='designerInitialized'>
  6.         </gc-spread-sheets-designer>
  7.     </div>
  8. </div>
  9. </template>
  10. <script>
  11. /* eslint-disable */
  12. import '@grapecity/spread-sheets/styles/gc.spread.sheets.excel2013white.css'
  13. import '@grapecity/spread-sheets-designer/styles/gc.spread.sheets.designer.min.css'
  14. import '@grapecity/spread-sheets-designer-resources-cn';
  15. import GC from '@grapecity/spread-sheets';
  16. import * as Excel from "@grapecity/spread-excelio";
  17. GC.Spread.Common.CultureManager.culture("zh-cn");
  18. import '@grapecity/spread-sheets-resources-zh';
  19. import { Designer } from '@grapecity/spread-sheets-designer-vue'
  20. import FaverSaver from "file-saver";
  21. GC.Spread.Sheets.Designer.LicenseKey = "";
  22. export default {
  23.   name: 'demo', // 落货纸页面
  24.   props: {
  25.     excelFile: {
  26.       type: String,
  27.       default: "'https://minio.cnbabylon.com/public/luckysheet/money-manager-2.xlsx'",
  28.     }
  29.   },
  30.   data(){
  31.     return {
  32.       styleInfo: { height: "98vh", width: '100%' },
  33.       config: null,
  34.       designer: null,
  35.       commonQuery: {
  36.         type: undefined,
  37.       },
  38.       typeOptions:[],
  39.       spread: null,
  40.     }
  41.   },
  42.   created(){
  43.     let self = this;
  44.     console.log('created挂载文件:',self.excelFile)
  45.   },
  46.   methods: {
  47.     getSpread(){
  48.       let self = this;
  49.       var designer = new GC.Spread.Sheets.Designer.Designer(document.getElementById("hostDiv"));
  50.       let excelIo = new Excel.IO();
  51.       var workbook = designer.getWorkbook();
  52.       var sheet = workbook.getActiveSheet();
  53.       let json = sheet.toJSON();
  54.       console.log(json,'sheet');
  55.       excelIo.save(json, function (blob) {console.log('blob',blob) }, function (e) {console.log(e);});
  56.     },
  57.     designerInitialized(designer) {
  58.       this.designer = designer;
  59.       window.designer = designer;
  60.       this.initWookbook(designer);
  61.     },
  62.     initWookbook(designer) {
  63.       let self = this;
  64.       let excelIo = new Excel.IO();
  65.       let excelFilePath = self.excelFile;
  66.       let xhr = new XMLHttpRequest();
  67.       xhr.open('GET', excelFilePath, true);
  68.       xhr.responseType = 'blob';
  69.       xhr.onload = function(e) {
  70.         if (this.status == 200) {
  71.           var blob = this.response;
  72.           console.log(this.response,'this.response')
  73.           excelIo.open(blob, function (json) {
  74.             var workbookObj = json;
  75.             self.spread = designer.getWorkbook().fromJSON(workbookObj);
  76.           }, function (e) {
  77.               console.log(e.errorMessage);
  78.           }, {});
  79.         }
  80.       };
  81.       xhr.send();
  82.     },
  83.     // 每列点击效果
  84.     btnEvent(method) {
  85.       const self = this;
  86.       self[method]();
  87.     },
  88.     // processFile (event) {
  89.     //   this.excelFile = event.target.files[0];
  90.     //   this.importExcel();
  91.     // },
  92.     exportExcel () {
  93.       var excelIO = new Excel.IO();
  94.       var json = this.spread.toJSON();
  95.       excelIO.save(
  96.         json,
  97.         function(blob) {
  98.           FaverSaver.saveAs(blob, "export.xlsx");
  99.         },
  100.         function(e) {
  101.           console.log(e);
  102.         }
  103.       );
  104.     },
  105.     printWorkbook (){
  106.       this.spread.print();
  107.     },
  108.     workbookInitialized(spread) {
  109.       this.spread = spread;
  110.       spread.refresh();
  111.     },
  112.    
  113.   }
  114. }
  115. </script>
  116. <style scoped>
  117. .spread-host {
  118.   width: 84vw;
  119.   height: 800px;
  120.   border: 1px solid black;
  121. }

  122. .btn-group {
  123.   position: fixed;
  124.   z-index: 4;
  125.   width: 84vw;
  126. }
  127. .search-row {
  128.   margin: 2px 0;
  129.   background-color: #fff;
  130. }
  131. .sheet-area {
  132.   margin-top: 78px;
  133. }
  134. .com-pannel-wrp {
  135.   background-color: #fff;
  136. }
  137. </style>
复制代码
image.png405873107.png

vue-demo.zip

102.82 KB, 下载次数: 25

4 个回复

倒序浏览
Derrick.Jiao讲师达人认证 悬赏达人认证 SpreadJS 开发认证
论坛元老   /  发表于:2021-3-29 14:05:36
沙发
您好,出现这个报错是因为save方法应该传的是workbook的序列化对象而不是sheet的序列化对象,也就是let json = workbook.toJSON();
image.png14441222.png
回复 使用道具 举报
随机用户
金牌服务用户   /  发表于:2021-3-29 14:15:19
板凳
DerrickJiao 发表于 2021-3-29 14:05
您好,出现这个报错是因为save方法应该传的是workbook的序列化对象而不是sheet的序列化对象,也就是let jso ...

导出一下是空白文件,这个如何解决?
回复 使用道具 举报
随机用户
金牌服务用户   /  发表于:2021-3-29 14:17:10
地板
DerrickJiao 发表于 2021-3-29 14:05
您好,出现这个报错是因为save方法应该传的是workbook的序列化对象而不是sheet的序列化对象,也就是let jso ...
  1. let json = workbook.toJSON();
  2.       console.log(json,'sheet');
  3.       excelIo.save(json, function (blob) {console.log('blob',blob);FaverSaver.saveAs(blob, "export.xlsx");}, function (e) {console.log(e);});
复制代码


导出的时候下载出来是空白的
回复 使用道具 举报
Derrick.Jiao讲师达人认证 悬赏达人认证 SpreadJS 开发认证
论坛元老   /  发表于:2021-3-29 14:39:43
5#
随机用户 发表于 2021-3-29 14:17
导出的时候下载出来是空白的

下载空白是因为spread对象没获取对,参考下面的代码 image.png186984252.png
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 立即注册
返回顶部