PageOffice最简集成代码(Springboot)

本文描述了PageOffice产品在Springboot项目中如何集成调用。(本示例使用了Thymeleaf模板引擎)

  1. 新建Springboot项目:pageoffice6-springboot2-simple

  2. 在您项目的pom.xml中通过下面的代码引入PageOffice依赖。pageoffice.jar已发布到Maven中央仓库 (opens new window),建议使用最新版本。

    如果使用Springboot3,或Tomcat10及以上版本,使用下面的pom.xml配置

xml 复制代码
<dependency>
  <groupId>com.zhuozhengsoft</groupId>   
  <artifactId>pageoffice</artifactId>   
  <version>6.6.1.1</version>
</dependency>
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-websocket</artifactId>
</dependency>

如果使用Springboot2,或Tomcat9及以下的版本,使用下面的pom.xml配置

xml 复制代码
<dependency>
  <groupId>com.zhuozhengsoft</groupId>   
  <artifactId>pageoffice</artifactId>   
  <version>6.6.1.1-javax</version>
</dependency>
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-websocket</artifactId>
</dependency>
  1. 新建一个pageoffice文件夹,用来存放PageOffice的系统文件(如license.lic、客户端安装包等),比如windows环境下创建:D:/pageoffice,linux环境下创建:/root/pageoffice

  2. 拷贝pageoffice客户端安装程序到上一步创建的pageoffice文件夹下。

    • 客户端是windows环境:拷贝posetup_6.6.1.1.exe到pageoffice文件夹下;
    • 客户端是国产操作系统环境:拷贝对应芯片的PageOffice客户端deb安装包到pageoffice文件夹下;

PageOffice客户端安装程序下载地址:www.zhuozhengsoft.com/dowm/(opens...

  1. 打开springboot项目的配置文件application.properties,添加一个posyspath变量,值为上一步创建的pageoffice文件夹的路径
ini 复制代码
posyspath=D:/pageoffice
  1. 在您项目的启动类Application类中添加一项@Bean配置,此为PageOffice服务器端的必要配置,代码如下:
typescript 复制代码
@Value("${posyspath}")
private String poSysPath;

@Bean
public ServletRegistrationBean pageofficeRegistrationBean()  {
  com.zhuozhengsoft.pageoffice.poserver.Server poserver 
      						= new com.zhuozhengsoft.pageoffice.poserver.Server();
  poserver.setSysPath(poSysPath);//设置PageOffice注册成功后,license.lic文件存放的目录
    
  ServletRegistrationBean srb = new ServletRegistrationBean(poserver);
  srb.addUrlMappings("/poserver.zz");
  srb.addUrlMappings("/poclient");
  srb.addUrlMappings("/pageoffice.js");
  srb.addUrlMappings("/sealsetup.exe");
  return srb;
}

// PageOffice V6.6及以上版本新增以下代码,V6.5及之前版本无需此代码
@Bean
public ServerEndpointExporter serverEndpointExporter() {
    ServerEndpointExporter exporter = new ServerEndpointExporter();
    exporter.setAnnotatedEndpointClasses(
            com.zhuozhengsoft.pageoffice.poserver.WServer.class
    );
    return exporter;
}
@Bean
public ServletListenerRegistrationBean powContextListener() {
    return new ServletListenerRegistrationBean<>(new POWContextListener());
}
@Bean
public ServletContextInitializer pageofficeContextParams() {
    /*
         * powserver跨域安全配置:
         * 1. 生产环境不推荐使用"*",建议明确指定允许的域名/IP
         * 2. 格式:多个地址用逗号分隔,如"域名1,域名2,IP"。注意:本地开发环境地址(localhost,127.0.0.1)也必须在此配置
         * 3. 示例:
         *    - 单体多入口:"域名,ip"
         *      (如"www.oa.com,192.168.1.100")
         */
    return servletContext ->
        servletContext.setInitParameter("powserver-allowedOrigins", "*");
}
  1. 在D盘根目录下准备一个有内容的test.docx文件,新建Controller并调用PageOffice在线打开此文件,例如SimpleWordController代码如下:
typescript 复制代码
@Controller
@RequestMapping(value = "/simpleWord")
public class SimpleWordController {  
    
  @RequestMapping(value="/openFile")
  public String openFile(HttpServletRequest request) {
      PageOfficeCtrl poCtrl = new PageOfficeCtrl(request);
      //webOpen的第一个参数支持能够输出下载文件的Url相对地址或者文件在服务器上的磁盘路径两种方式
      //查看详细,请在本站搜索"PageOffice属性或方法中涉及到的URL路径或磁盘路径的说明"
      poCtrl.webOpen("D:\test.docx", OpenModeType.docNormalEdit, "张三");
      request.setAttribute("poHtmlCode", poCtrl.getHtml());
      return  "simpleWord";
  }
    
  @RequestMapping("/saveFile")
  public void saveFile(HttpServletRequest request, HttpServletResponse response) {
      FileSaver fs = new FileSaver(request, response);
      fs.saveToFile("D:\" + fs.getFileName());
      fs.close();
  }
    
}
  1. 为上一步代码中的openFile方法准备Thymeleaf模板:simpleWord.html,代码如下:
xml 复制代码
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>最简单的打开保存文件</title>
</head>
<body>
<div style="width:100%; height:800px;"  th:utext="${poHtmlCode}"></div>
<script>
    function Save() {
        //使用SaveFilePage属性设置后端保存方法的Controller路由地址,这个地址必须从"/"开始
        pageofficectrl.SaveFilePage = "/simpleWord/saveFile";
        pageofficectrl.WebSave();
    }
    function OnPageOfficeCtrlInit() {
        pageofficectrl.AddCustomToolButton("保存", "Save", 1);
    }
</script>
</body>
</html>
  1. 在需要点击超链接实现在线打开文件的页面(比如:index.html)中添加pageoffice.js文件的引用。
xml 复制代码
<script type="text/javascript" src="/pageoffice.js"></script>
  1. 然后在页面中添加一个超链接,点击超链接调用POBrowser对象的openWindow方法,弹出新浏览器窗口访问simpleWord/openFile在线打开文件,代码如下:
css 复制代码
<a href="javascript:POBrowser.openWindow('simpleWord/openFile','width=1150px;height=900px;');">
    在线打开文档
</a>
  1. 启动项目,点击"在线打开文档"超链接,查看在线打开编辑保存Office文件的效果。

参考链接:PageOffice最简集成代码(Springboot) | PageOffice 开发者中心

相关推荐
马卡巴卡1 天前
线上Nginx频繁502,排查3小时发现是这个配置的问题
后端
superman超哥1 天前
Rust `‘static` 生命周期:从字面意义到深层语义
开发语言·后端·rust·生命周期·编程语言·rust static·深层语义
猹斯1 天前
kubeadm 部署问题排查
后端
JOEH601 天前
🚀 数据库插入 1000 万数据?别再傻傻用 for 循环了!实测 5 种方式效率对比
数据库·后端
技术小泽1 天前
MQTT从入门到实战
java·后端·kafka·消息队列·嵌入式
半夏知半秋1 天前
rust学习-Option与Result
开发语言·笔记·后端·学习·rust
独自破碎E1 天前
Spring Boot支持哪些嵌入Web容器?
前端·spring boot·后端
疯狂成瘾者1 天前
后端Spring Boot 核心知识点
java·spring boot·后端
IT 行者1 天前
Spring Boot 4.x 安全监控新篇章:基于 ObservationFilterChainDecorator 的可观测性实践
java·spring boot·后端