找回密码
 立即注册

QQ登录

只需一步,快速开始

dexteryao 讲师达人认证 悬赏达人认证 SpreadJS 开发认证

超级版主

123

主题

8927

帖子

1万

积分

超级版主

Rank: 8Rank: 8

积分
13536

讲师达人悬赏达人元老葡萄SpreadJS 认证SpreadJS 高级认证微信认证勋章

dexteryao 讲师达人认证 悬赏达人认证 SpreadJS 开发认证
超级版主   /  发表于:2020-8-31 23:23  /   查看:2711  /  回复:0
本帖最后由 dexteryao 于 2020-8-31 23:23 编辑

前两期
SpreadJS 全家桶实践 - 在线Excel报表系统(一)
SpreadJS 全家桶实践 - 在线Excel报表系统(二)
实现了报表设计和数据绑定,本期将继续实现页面设置和导出PDF功能

为了导出PDF分页符合预期,进行简单的页面设置
新建ribbon.print.js添加页面配置config,并且在designer.js中 initDesigner方法里配置

  1.     customerRibbon.buttonGroups.push(ribbonPrintConfig)
复制代码


ribbon.print.js内容

  1. var ribbonPrintConfig = {
  2.     "label": "页面布局",
  3.     "thumbnailClass": "ribbon-thumbnail-spreadsettings",
  4.     "commandGroup": {
  5.         "children": [
  6.             {
  7.                 "direction": "vertical",
  8.                 "commands": [
  9.                     {
  10.                         text: "打印分页预览线",
  11.                         type: "checkbox",
  12.                         commandName: "show_hide_print_line",
  13.                         execute: async (context) => {
  14.                             setTimeout(() => {
  15.                                 let sheet = context.Spread.getActiveSheet();
  16.                                 var isVisible = sheet.isPrintLineVisible();
  17.                                 sheet.isPrintLineVisible(!isVisible);
  18.                             }, 0)
  19.                         },
  20.                         getState: (context) => {
  21.                             let sheet = context.Spread.getActiveSheet();
  22.                             return sheet.isPrintLineVisible();
  23.                         }
  24.                     }
  25.                 ]
  26.             },
  27.             {
  28.                 "type": "separator"
  29.             },
  30.             {
  31.                 "direction": "vertical",
  32.                 "commands": [
  33.                     {
  34.                         iconClass: "ribbon-button-namemanager",
  35.                         text: "插入分页符",
  36.                         commandName: "insertPageBreak",
  37.                         execute: async (context) => {
  38.                             let sheet = context.Spread.getActiveSheet();
  39.                             sheet.setColumnPageBreak(sheet.getActiveColumnIndex(), true)
  40.                             sheet.setRowPageBreak(sheet.getActiveRowIndex(), true)
  41.                         }
  42.                     },
  43.                     {
  44.                         iconClass: "ribbon-button-namemanager",
  45.                         text: "删除分页符",
  46.                         commandName: "deletePageBreak",
  47.                         execute: async (context) => {
  48.                             let sheet = context.Spread.getActiveSheet();
  49.                             sheet.setColumnPageBreak(sheet.getActiveColumnIndex(), false)
  50.                             sheet.setRowPageBreak(sheet.getActiveRowIndex(), false)
  51.                         }
  52.                     },
  53.                     {
  54.                         iconClass: "ribbon-button-namemanager",
  55.                         text: "删除所有分页符",
  56.                         commandName: "deleteAllPageBreak",
  57.                         execute: async (context) => {
  58.                             let sheet = context.Spread.getActiveSheet();
  59.                             sheet.suspendPaint()
  60.                             for(var row = 0; row < sheet.getRowCount(); row++){
  61.                                 sheet.setRowPageBreak(row, false)
  62.                             }
  63.                             for(var col = 0; col < sheet.getColumnCount(); col++){
  64.                                 sheet.setColumnPageBreak(col, false)
  65.                             }
  66.                             sheet.resumePaint();
  67.                         }
  68.                     },
  69.                 ]
  70.             },
  71.             {
  72.                 "type": "separator"
  73.             },
  74.             {
  75.                 "direction": "vertical",
  76.                 "commands": [
  77.                     {
  78.                         iconClass: "ribbon-button-namemanager",
  79.                         text: "宽度",
  80.                     },
  81.                     {
  82.                         iconClass: "ribbon-button-namemanager",
  83.                         text: "高度",
  84.                     }
  85.                 ]
  86.             },
  87.             {
  88.                 "direction": "vertical",
  89.                 "commands": [
  90.                     {
  91.                         text: "自动",
  92.                         type: "combobox",
  93.                         commandName: "setFitPagesWide",
  94.                         dropdownList: [
  95.                             {
  96.                                 text: "自动",
  97.                                 value: "-1"
  98.                             },
  99.                             {
  100.                                 text: "1",
  101.                                 value: "1"
  102.                             },
  103.                             {
  104.                                 text: "2",
  105.                                 value: "2"
  106.                             }
  107.                         ],
  108.                         execute: async (context, selectValue) => {
  109.                             if (selectValue != null && selectValue != undefined) {
  110.                                 setTimeout(() => {
  111.                                     let sheet = context.Spread.getActiveSheet();
  112.                                     var printInfo = sheet.printInfo();
  113.                                     printInfo.fitPagesWide(parseInt(selectValue));
  114.                                     sheet.printInfo(printInfo)
  115.                                 }, 0);
  116.                             }
  117.                         },
  118.                         getState: (context) => {
  119.                             let sheet = context.Spread.getActiveSheet();
  120.                             var printInfo = sheet.printInfo()
  121.                             var value = printInfo.fitPagesWide();
  122.                             return value.toString();
  123.                         }

  124.                     },
  125.                     {
  126.                         text: "高度",
  127.                         type: "combobox",
  128.                         commandName: "setFitPagesTall",
  129.                         dropdownList: [
  130.                             {
  131.                                 text: "自动",
  132.                                 value: "-1"
  133.                             },
  134.                             {
  135.                                 text: "1",
  136.                                 value: "1"
  137.                             },
  138.                             {
  139.                                 text: "2",
  140.                                 value: "2"
  141.                             }
  142.                         ],
  143.                         execute: async (context, selectValue) => {
  144.                             if (selectValue != null && selectValue != undefined) {
  145.                                 setTimeout(() => {
  146.                                     let sheet = context.Spread.getActiveSheet();
  147.                                     var printInfo = sheet.printInfo();
  148.                                     printInfo.fitPagesTall(parseInt(selectValue));
  149.                                     sheet.printInfo(printInfo)
  150.                                 }, 0);
  151.                             }
  152.                         },
  153.                         getState: (context) => {
  154.                             let sheet = context.Spread.getActiveSheet();
  155.                             var printInfo = sheet.printInfo()
  156.                             var value = printInfo.fitPagesTall();
  157.                             return value.toString();
  158.                         }

  159.                     }
  160.                 ]
  161.             }
  162.         ]
  163.     }
  164. };
复制代码

添加导出PDF按钮和下拉选项,按钮样式为自定义图标
  1. {
  2.                 "commands": [
  3.                     {
  4.                         iconClass: "ribbon-button-pdf-font",
  5.                         bigButton: true,
  6.                         text: "PDF",
  7.                         commandName: "pdf0",
  8.                         execute: exportPDF,
  9.                     },
  10.                     {
  11.                         title: "PDF_List",
  12.                         type: "dropdown",
  13.                         commandName: "pdf_sub",
  14.                         subCommands: [
  15.                             {
  16.                                 text: "绑定数据导出",
  17.                                 iconClass: "ribbon-button-pdf-small",
  18.                                 commandName: "pdf2",
  19.                                 execute: async (context, selectValue) => {
  20.                                     await exportPDF(context, true);
  21.                                 }
  22.                             },
  23.                             {
  24.                                 text: "导出当前Sheet",
  25.                                 iconClass: "ribbon-button-pdf-small",
  26.                                 commandName: "pdf1",
  27.                                 execute: async (context, selectValue) => {
  28.                                     await exportPDF(context, false, true);
  29.                                 }
  30.                             },
  31.                             {
  32.                                 text: "绑定导出当前Sheet",
  33.                                 iconClass: "ribbon-button-pdf-small",
  34.                                 commandName: "pdf3",
  35.                                 execute: async (context, selectValue) => {
  36.                                     await exportPDF(context, true, true);
  37.                                 }
  38.                             }
  39.                         ]
  40.                     }
  41.                 ]
  42.             },
复制代码
根据点击不同按钮,exportPDF方法传递不同参数
  1. async function exportPDF(designer, isBinding, isActiveSheet) {
  2.     console.log(arguments)
  3.     var spread = designer.getWorkbook();
  4.     var spreadJSON = JSON.stringify(spread.toJSON());
  5.     var uploadData = gzipString(spreadJSON);
  6.     fetch("spread/getpdf", {
  7.         body: "data=" + encodeURIComponent(uploadData) + "&isBinding=" + !!isBinding + "&isActiveSheet=" + !!isActiveSheet,
  8.         headers: {
  9.             "content-type": "application/x-www-form-urlencoded"
  10.         },
  11.         method: "POST"
  12.     }).then(resp => resp.blob())
  13.         .then(blob => {
  14.             const url = window.URL.createObjectURL(blob);
  15.             window.open(url, "_blank")
  16.         })
  17. }
复制代码

服务端使用GCExcel 绑定数据,返回PDF,前端直接新tab 打开

  1.     /* 导出PDF,可选择单sheet导出或者绑定模拟数据后导出*/
  2.     @RequestMapping(value = "/getpdf", method = RequestMethod.POST)
  3.     public ResponseEntity<byte[]> getPDF(@RequestParam(value = "data", required = true) String data,
  4.                                          @RequestParam(value = "isBinding", required = false) boolean isBinding,
  5.                                          @RequestParam(value = "isActiveSheet", required = false) boolean isActiveSheet) {

  6.         String json = uncompress(data);
  7.         Workbook workbook = new Workbook();
  8.         List<com.grapecity.documents.excel.JsonError> errors = workbook.fromJson(json);
  9.         System.out.println(errors);

  10.         if (isBinding) {
  11.             bindingDataToWrokbook(workbook);
  12.         }

  13.         System.out.println(workbook.getActiveSheet().getUsedRange(EnumSet.of(UsedRangeType.Data, UsedRangeType.DataValidation)));

  14.         ByteArrayOutputStream out = new ByteArrayOutputStream();

  15.         if (isActiveSheet) {
  16.             IWorksheet sheet = workbook.getActiveSheet();
  17.             System.out.println(sheet.getVPageBreaks().getCount());
  18.             sheet.save(out, SaveFileFormat.Pdf);
  19.         } else {
  20.             workbook.save(out, SaveFileFormat.Pdf);
  21.         }
  22. //        System.out.print(workbook.toJson());

  23.         byte[] contents = out.toByteArray();

  24.         HttpHeaders headers = new HttpHeaders();
  25.         headers.setContentType(MediaType.APPLICATION_PDF);
  26.         // Here you have to set the actual filename of your pdf
  27.         String filename = "output.pdf";
  28.         headers.setContentDispositionFormData(filename, filename);
  29.         headers.setCacheControl("must-revalidate, post-check=0, pre-check=0");
  30.         ResponseEntity<byte[]> response = new ResponseEntity<>(contents, headers, HttpStatus.OK);
  31.         return response;
  32.     }
复制代码

更多内容请关注葡萄城技术社区


0 个回复

您需要登录后才可以回帖 登录 | 立即注册
返回顶部