一、前言

  1. PageOffice官网: http://www.zhuozhengsoft.com/
  2. PageOffice集成说明: https://www.kancloud.cn/pageoffice_course_group/pageoffice_course/652260
准备集成环境

下载jar依赖:http://www.zhuozhengsoft.com/dowm/

二、SpringBoot整合PageOffice实现在线编辑Word和Excel

本文基于springboot2.3.1.RELEASE版本集成pageoffice

1、pom.xml中新增相关依赖

将下载的jar依赖放进项目的lib包下 ( 注:这里也可以自定义存放位置,然后修改本地引入位置${project.basedir}/lib/pageoffice4.6.0.4.jar即可 )

<!-- =================================== ↓↓↓↓↓↓ PageOffice所需依赖 start ↓↓↓↓↓↓ ==================================== -->
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency><!-- 添加Sqlite依赖(可选:如果不需要使用印章功能的话,不需要添加此依赖)-->
<dependency><groupId>org.xerial</groupId><artifactId>sqlite-jdbc</artifactId><version>3.7.2</version>
</dependency>
<!-- 添加PageOffice依赖(必须) -->
<dependency><groupId>com.zhuozheng</groupId><artifactId>pageoffice</artifactId><version>4.6.0.4</version><scope>system</scope><systemPath>${project.basedir}/lib/pageoffice4.6.0.4.jar</systemPath>
</dependency>
<!-- =================================== ↑↑↑↑↑↑ PageOffice所需依赖 end ↑↑↑↑↑↑ ==================================== -->

本地jar包引入需再新增如下,可参考文末源码demo

2、application.yml中新增pageoffice配置

spring:# THYMELEAF (ThymeleafAutoConfiguration)thymeleaf:prefix: classpath:/templates/suffix: .htmlcache: false# ======================== ↓↓↓↓↓↓ PageOffice相关配置 ↓↓↓↓↓↓ ===============================
pageoffice:# 本地文件所在磁盘位置docpath: /pageoffice/doc/# 设置PageOffice自带印章管理程序的登录密码popassword: 123456# 指定一个磁盘目录用来存放PageOffice注册成功之后生成的license.lic文件posyspath: /pageoffice/lic/

3、resources/templates下新增如下3个文件

① Excel.html
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
<head><title>打开Excel文件</title><script type="text/javascript">function Save() {document.getElementById("PageOfficeCtrl1").WebSave();}</script><script type="text/javascript">function AddSeal() {try {document.getElementById("PageOfficeCtrl1").ZoomSeal.AddSeal();} catch (e) {}}</script></head>
<body>
<div style="width:auto;height:700px;" th:utext="${pageoffice}"></div>
</body>
</html>
② Word.html
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
<head><title>打开Word文件</title><script type="text/javascript">function Save() {document.getElementById("PageOfficeCtrl1").WebSave();}</script><script type="text/javascript">function AddSeal() {try {document.getElementById("PageOfficeCtrl1").ZoomSeal.AddSeal();} catch (e) {}}</script></head>
<body>
<div style="width:auto;height:700px;" th:utext="${pageoffice}"></div>
</body>
</html>
③ index.html
<!DOCTYPE html>
<html>
<head><meta charset="utf-8"><title>HelloWorld</title><style>#main {width: 15%;text-align: center;}ol {text-align: left;border: solid 1px gray;"}ol li {text-align: left;border-bottom: dotted 1px gray;height: 30px;margin-top: 10px;}</style><!-- 引用后端项目中的pageoffice.js文件 --><script type="text/javascript" src="http://localhost:8080/pageoffice.js"></script>
</head>
<body>
<div id="main"><h3>HelloWorld</h3><ol><li><!-- openWindowModeless用法参考:http://www.zhuozhengsoft.com/help/js3/pobrowser/function/openwindowmodeless.htm --><a href="javascript:POBrowser.openWindowModeless('http://localhost:8080/api/word','width=1200px;height=800px;');">打开Word文档</a></li><li><a href="javascript:POBrowser.openWindowModeless('http://localhost:8080/api/excel','width=1200px;height=800px;');">打开Excel文档</a></li></ol>
</div>
</body>
</html>

4、全局常用变量

public class Constants {/*** 项目根目录*/public static final String PROJECT_ROOT_DIRECTORY = System.getProperty("user.dir");/*** word文件名*/public static final String FILE_NAME_WORD = "test.doc";/*** excel文件名*/public static final String FILE_NAME_EXCEL = "test.xls";
}

5、编写测试Controller

@Slf4j
@RestController
@RequestMapping("/api")
public class DemoController {@Value("${pageoffice.posyspath}")private String poSysPath;@Value("${pageoffice.popassword}")private String poPassWord;@Value("${pageoffice.docpath}")private String docPath;/*** 被`@PostConstruct`修饰的方法会在服务器加载Servlet的时候运行,并且只会被服务器执行一次。PostConstruct在构造函数之后执行,init()方法之前执行。*/@PostConstructpublic void init() {poSysPath = Constants.PROJECT_ROOT_DIRECTORY + poSysPath;docPath = Constants.PROJECT_ROOT_DIRECTORY + docPath;}@RequestMapping("/hello")public String hello() {log.info("hello ...");return "HelloWorld~";}@RequestMapping(value = "/word", method = RequestMethod.GET)public ModelAndView showWord(HttpServletRequest request, Map<String, Object> map) {log.info("编辑word ...");PageOfficeCtrl poCtrl = new PageOfficeCtrl(request);// 设置服务页面poCtrl.setServerPage("/poserver.zz");// 添加自定义保存按钮poCtrl.addCustomToolButton("保存", "Save", 1);// 添加自定义盖章按钮poCtrl.addCustomToolButton("盖章", "AddSeal", 2);// 拿到请求前缀做拼接保存文件方法String requestApiPrefix = request.getServletPath().replace("/word", "");// 设置处理文件保存的请求方法poCtrl.setSaveFilePage(requestApiPrefix + "/save");// 打开wordpoCtrl.webOpen("file://" + docPath + Constants.FILE_NAME_WORD, OpenModeType.docAdmin, "张三");map.put("pageoffice", poCtrl.getHtmlCode("PageOfficeCtrl1"));ModelAndView mv = new ModelAndView("Word");return mv;}@RequestMapping(value = "/excel", method = RequestMethod.GET)public ModelAndView showExcel(HttpServletRequest request, Map<String, Object> map) {log.info("编辑excel ...");PageOfficeCtrl poCtrl = new PageOfficeCtrl(request);// 设置服务页面poCtrl.setServerPage("/poserver.zz");// 添加自定义保存按钮poCtrl.addCustomToolButton("保存", "Save", 1);// 添加自定义盖章按钮poCtrl.addCustomToolButton("盖章", "AddSeal", 2);// 拿到请求前缀做拼接保存文件方法String requestApiPrefix = request.getServletPath().replace("/excel", "");// 设置处理文件保存的请求方法poCtrl.setSaveFilePage(requestApiPrefix + "/save");// 打开wordpoCtrl.webOpen("file://" + docPath + Constants.FILE_NAME_EXCEL, OpenModeType.xlsNormalEdit, "张三");map.put("pageoffice", poCtrl.getHtmlCode("PageOfficeCtrl1"));ModelAndView mv = new ModelAndView("Excel");return mv;}@RequestMapping("/save")public void saveFile(HttpServletRequest request, HttpServletResponse response) {log.info("保存文件 ...");FileSaver fs = new FileSaver(request, response);fs.saveToFile(docPath + fs.getFileName());fs.close();}/*** 添加PageOffice的服务器端授权程序Servlet(必须)** @return*/@Beanpublic ServletRegistrationBean servletRegistrationBean() {com.zhuozhengsoft.pageoffice.poserver.Server poserver = new com.zhuozhengsoft.pageoffice.poserver.Server();// 设置PageOffice注册成功后,license.lic文件存放的目录poserver.setSysPath(poSysPath);ServletRegistrationBean srb = new ServletRegistrationBean(poserver);srb.addUrlMappings("/poserver.zz");srb.addUrlMappings("/posetup.exe");srb.addUrlMappings("/pageoffice.js");srb.addUrlMappings("/jquery.min.js");srb.addUrlMappings("/pobstyle.css");srb.addUrlMappings("/sealsetup.exe");return srb;}/*** 添加印章管理程序Servlet(可选)*/@Beanpublic ServletRegistrationBean servletRegistrationBean2() {com.zhuozhengsoft.pageoffice.poserver.AdminSeal adminSeal =new com.zhuozhengsoft.pageoffice.poserver.AdminSeal();// 设置印章管理员admin的登录密码adminSeal.setAdminPassword(poPassWord);// 设置印章数据库文件poseal.db存放的目录adminSeal.setSysPath(poSysPath);ServletRegistrationBean srb = new ServletRegistrationBean(adminSeal);srb.addUrlMappings("/adminseal.zz");srb.addUrlMappings("/sealimage.zz");srb.addUrlMappings("/loginseal.zz");return srb;}
}

6、准备测试word和excel文件

注意将测试word和excel文件(文件不能为0字节)放在项目根目录/pageoffice/doc目录下,以及新建lic文件夹…

三、测试

访问http://localhost:8080/,点击打开Word文档

第一次使用的时候会提示安装PageOffice,直接下一步安装即可~

可参考PageOffice客户端安装步骤:https://www.kancloud.cn/pageoffice_course_group/pageoffice_course/654031




安装完成之后回到页面再次点击打开Word文档,这时候需要填写注册信息,序列号在之前下载的包里面可以找到


注册成功后,正常打开Word文件,之后就可以进行自己的神操作了…

打开Excel文档如下:

四、Vue页面集成PageOffice

1、项目的index.html中引入pageoffice.js

<!-- 引用后端项目中的pageoffice.js文件 -->
<script type="text/javascript" src="http://localhost:8080/pageoffice.js"></script>

2、页面

<template><div class="app-container"><el-button><a href="javascript:POBrowser.openWindowModeless('http://localhost:8080/api/word','width=1200px;height=800px;');">打开Word文档</a></el-button><el-button><a href="javascript:POBrowser.openWindowModeless('http://localhost:8080/api/excel','width=1200px;height=800px;');">打开Excel文档</a></el-button></div>
</template>
<style lang="scss" scoped></style>

这里页面很简单就2个按钮…

主要通过openWindowModeless 使用非模态框的形式打开文件

用法可参考:http://www.zhuozhengsoft.com/help/js3/pobrowser/function/openwindowmodeless.htm

3、后端解决前后端分离情况下跨域问题

@Configuration
public class CorsConfig {private CorsConfiguration config() {CorsConfiguration corsConfiguration = new CorsConfiguration();corsConfiguration.setAllowCredentials(true);// ① 设置你要允许的网站域名,如果全允许则设为 *corsConfiguration.addAllowedOrigin("*");// corsConfiguration.addAllowedOrigin("http://www.zhengqingya.com");// ② 如果要限制 HEADER 或 METHOD 请自行更改corsConfiguration.addAllowedHeader("*");corsConfiguration.addAllowedMethod("*");return corsConfiguration;}@Beanpublic CorsFilter corsFilter() {UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();source.registerCorsConfiguration("/**", config());return new CorsFilter(source);}}

五、问题 -> 使用Pageoffice打开Word时报0x80040154错误

问题出现可能原因:安装WPS时关联了.doc.xls.ppt等文件,导致Office无法自动关联

解决:

  1. 想办法去掉默认关联
  2. 直接卸载WPS
  3. 自行谷歌

本文案例demo源码

https://gitee.com/zhengqingya/java-workspace

SpringBoot(30) 整合PageOffice实现在线编辑Word和Excel相关推荐

  1. ①. SpringBoot整合PageOffice实现在线编辑Word和Excel

    ①. SpringBoot整合PageOffice实现在线编辑Word和Excel PageOffice官网: http://www.zhuozhengsoft.com/ PageOffice集成说明 ...

  2. Vue+springboot集成PageOffice实现在线编辑Word、excel文档

    说明: PageOffice是一款在线的office编辑软件,帮助Web应用系统或Web网站实现用户在线编辑Word.Excel.PowerPoint文档.可以完美实现在线公文流转,领导批阅,盖章.可 ...

  3. Vue+SpringBoot 集成 PageOffice 实现在线编辑Word、excel文档

    说明: PageOffice是一款在线的office编辑软件,帮助Web应用系统或Web网站实现用户在线编辑Word.Excel.PowerPoint文档.可以完美实现在线公文流转,领导批阅,盖章.可 ...

  4. office文档管理服务器编辑,_卓正软件 - PageOffice官方网站 - 在线编辑Word、Excel的Office文档控件...

    Office 组件 在线显示.编辑.保存Word文档 √ √ √ 在线显示.编辑.保存Excel文档 √ √ √ 在线显示.编辑.保存PowerPoint文档 √ √ √ 在线播放PowerPoint ...

  5. 点聚WebOffice在线编辑word、excel开发指南

    http://www.dianju.cn/forum/viewtopic.php?t=9249 WebOffice开发系列指南 WebOffice文档控件技术交流和版本发布 发表回复 17 篇帖子 • ...

  6. 前后端分离项目集成PageOffice——实现在线编辑Word文件的版本控制

    说明: PageOffice本身提供了SaveFilePage的js方法,但是由于该方法不支持代理且不能跨域导致在前后端分离项目中无法使用 功能:实现三个按钮分别保存不同版本的文件 1.PageOff ...

  7. 用pageOffice控件实现 office 文档在线编辑Word 打开文档后在页面里触发事件

    OA办公中,业务需要编辑打开word文档后 执行一些js操作 怎么实现编辑打开word文档后 执行一些js操作呢? 2 实现方法 通过pageOffice实现简单的在线打开编辑word时, 通过设置 ...

  8. web在线编辑word,excel,pdf插件-----WebOffice 文档控件API

    目    录 一.工作原理...5 1.1         开发流程...5 1.2         WEB页面调用控件:.6 二.接口说明...7 2.1         接口...7 2.1.1  ...

  9. LoadWebOffice实现在线编辑Word

    LoadWebOffice实现在线编辑Word 1.准备工作 (1)LoadWebOffice.js 提取码:iyk4 (2)weboffice.ocx 提取码:nde6 2.代码块 LoadWebO ...

  10. pageoffice 在线编辑 word 文档,保存之后返回自定义的值,并把值传到父页面

    1.    描述:最近项目有个在线编辑office 办公文档的功能, 采用的方案是用 pageoffice 在线编辑 office 文档. 因为高版本的谷歌和火狐不支持任何插件了.所以pageoffi ...

最新文章

  1. 卷积神经网络(CNN)代码实现(MNIST)解析
  2. 图解“红黑树”原理,一看就明白!
  3. 三分钟Docker-推送本地镜像到仓库
  4. 《狂人日记》金句摘抄(一)
  5. 英特尔拥抱开源,岂能没有杀手锏?
  6. Java学习笔记2.2.1 常量与变量 - 变量
  7. 【报告分享】bilibili 2020品牌营销手册.pdf(附下载链接)
  8. 探索式测试实践之路(国际大师James Bach题词推荐之探索式测试唯一本土著作)
  9. Pr 入门教程,如何倾斜移位效果?
  10. 【转】SIP 中的Dialog,call,session 和 transaction
  11. 传智播客Java引用和数值类型思考
  12. 天猫魔盒android开发者模式,【当贝市场】天猫魔盒M16S开启远程调试模式教程
  13. 一文读懂运放偏置电流和输入失调电流
  14. MYSQL 思考题5 参考答案
  15. 如何长时间高效学习?
  16. 群体遗传学--近交系数
  17. 中标麒麟桌面版7.0 u盘安装
  18. Attention Is All You Need论文笔记
  19. Spring Cloud的Ribbon-Hystrix-Feign
  20. 苹果xrid不支持服务器,iPhone XR登录不了Apple ID怎么办?iTunes无法登录Apple ID怎么解决? ... ......

热门文章

  1. Ubuntu 10.10 下安装spoonwep-wpa工具
  2. 面向对象实现气缸吹气类的PLC逻辑
  3. 新员工入职表_员工离职率过高,只要三步骤,就能轻松有效控制
  4. MFC 通用对话框之颜色对话框
  5. Linux基础之计算机网络
  6. android listview表格分页显示,android实现listview分页的方法
  7. Android 字符串的替换字符
  8. 最全的基于MFC的ActiveX控件开发教程
  9. 关于飞信的协议以及验证码
  10. screen linux卸载,Ubuntu常用软件安装(附截图软件、FTP、卸载命令)