SpringBoot PageOffice 在线编辑 (完整版、有源码)

简介

此文档讲的是SpringBoot和PageOffice整合,在线编辑office的文件,在网上找了很多资料
第一个看到的是微软的“office online server”,这个太复杂了,需要安装中间件,安装还对系统有要求,必须是“windows server 2012 r2”或“Windows Server 2016”,但是这款是不收费的

第二个看到的是卓正软件的PageOffice,这个是收费的,但是可以试用,下下来后demo也多,demo基本都是基于jsp页面的,我不想用jsp,我写的html页面

第三个看到的是点聚的WebOffice,这个也是免费的,但是嘛,下下来的demo…不想说话,网页打开没有office客户端编辑那种界面,很不方便,也很不好操作,看了一眼直接放弃

最后我选择使用PageOffice

实例环境准备

技术版本
Jdk1.8
SpringBoot2.1.5
PageOffice4.5.0.9
Tomcat8.0.32

去官网下载PageOffice包,下载地址:
http://www.zhuozhengsoft.com/download/PageOffice_4.5.0.9_Java.zip

解压后里面会有:
说明解压后的目录文件结构

生成license.lic文件

把Samples4文件夹复制到Tomcat的webapp目录下,在启动tomcat访:http://localhost:8080/Samples4
访问后页面会提示输入公司名称等信息,然后输入序列号,序列号如上图文件目录下有个序列号.txt复制进去填入即可,我点击的是在线注册,注册成功后会在tomcat的webapp下的Samples4下的WEB-INF/lib目录下生成一个license.lic文件
文件展示
你也可以访问看demo,有很多实例,可以实现哪些功能:
展示
这时候tomcat已经没什么用了,可以关掉了

把jar安装到maven使项目能够使用maven引入

lib目录展示
进入集成文件夹里面的lib目录,复制目录地址,打开命令窗口,使用cd进入复制的地址目录,执行:

mvn install:install-file -DgroupId=com.zhuozhengsoft -DartifactId=pageoffice -Dversion=4.5.0.9  -Dpackaging=jar  -Dfile=pageoffice4.5.0.9.jar

执行后结果展示

pom.xml 配置

<!-- 添加PageOffice依赖(必须) -->
	<dependency>
		<groupId>com.zhuozhengsoft</groupId>
		<artifactId>pageoffice</artifactId>
		<version>4.5.0.9</version>
	</dependency>
	<!-- 页面配置 -->
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-thymeleaf</artifactId>
	</dependency>

application.properties配置

# 注册PageOffice后license.lic存放的路径,访问poserver.zz的时候需要license.lic文件(我这里就是resources目录下的file目录,我写的是绝对路径)
posyspath=G:/ideaProject/springboot-demo/online-office/src/main/resources/file/

项目结构、代码介绍

项目结构:

项目结构介绍

BeanLinitConf类说明,PageOffice注入

package com.onlineoffice.conf;

import com.zhuozhengsoft.pageoffice.poserver.Server;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Description :初始化Bean
 * ---------------------------------
 * @Author : SG.Y
 * @Date : 2019/5/9 15:40
 */
@Configuration
@ConfigurationProperties
public class BeanInitConf {

    // PageOffice配置
    private String posyspath;

    /***
     * PageOffice 注册
     * @return
     */
    @Bean
    public ServletRegistrationBean servletRegistrationBean() {
        Server poserver = new Server();
        //设置PageOffice注册成功后,license.lic文件存放的目录
        poserver.setSysPath(posyspath);
        ServletRegistrationBean srb = new ServletRegistrationBean(poserver);
        // 下面是把资源文件暴露出来,必须配置,否则页面访问不了
        srb.addUrlMappings("/poserver.zz", "/posetup.exe", "/pageoffice.js", "/jquery.min.js", "/pobstyle.css", "/sealsetup.exe");
        return srb;
    }

    public void setPosyspath(String posyspath) {
        this.posyspath = posyspath;
    }

}

OfficeOnlineApi类介绍

package com.onlineoffice.api;

import com.zhuozhengsoft.pageoffice.FileSaver;
import com.zhuozhengsoft.pageoffice.OpenModeType;
import com.zhuozhengsoft.pageoffice.PageOfficeCtrl;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * @Description :Office在线编辑、预览API
 * ---------------------------------
 * @Author : SG.Y
 * @Date : 2019/5/14 16:36
 */
@Controller
public class OfficeOnlineApi {

    @Value("${posyspath}")
    private String filePath;

    /**
     * 在线word文档编辑
     *
     * @param request
     * @param map
     * @return
     */
    @GetMapping("/word")
    public String showWord(HttpServletRequest request, Map<String, Object> map) {
        PageOfficeCtrl poCtrl = new PageOfficeCtrl(request);
        //设置授权程序servlet
        poCtrl.setServerPage("/poserver.zz");
        //添加自定义按钮
        poCtrl.addCustomToolButton("保存", "Save", 1);
        //保存按钮接口地址
        poCtrl.setSaveFilePage("/save");
        //打开文件(打开的文件类型由OpenModeType决定,docAdmin是一个word,并且是管理员权限,如果是xls文件,则使用openModeType.xls开头的,其他的office格式同等),最后一个参数是作者
        // TODO 这里有个坑,这里打开的文件是本地的,地址如果写成/结构的路径,页面就会找不到文件,会从http://xxxxx/G/id...去找,写成\\就是从本地找
        poCtrl.webOpen("G:\\ideaProject\\springboot-demo\\online-office\\src\\main\\resources\\file\\test.docx", OpenModeType.docAdmin, "光哥");
        //pageoffice 是文件的变量,前端页面通过这个变量加载出文件
        map.put("pageoffice", poCtrl.getHtmlCode("PageOfficeCtrl1"));
        //跳转到word.html
        return "word";
    }

    /**
     * 在线ppt文档编辑
     *
     * @param request
     * @param map
     * @return
     */
    @GetMapping("/ppt")
    public String showPPT(HttpServletRequest request, Map<String, Object> map) {
        PageOfficeCtrl poCtrl = new PageOfficeCtrl(request);
        //设置授权程序servlet
        poCtrl.setServerPage("/poserver.zz");
        //添加自定义按钮
        poCtrl.addCustomToolButton("保存", "Save", 1);
        //保存接口地址
        poCtrl.setSaveFilePage("/save");
        //打开文件(打开的文件类型由OpenModeType决定,docAdmin是一个word,并且是管理员权限,如果是xls文件,则使用openModeType.xls开头的,其他的office格式同等),最后一个参数是作者
        // TODO 这里有个坑,这里打开的文件是本地的,地址如果写成/结构的路径,页面就会找不到文件,会从http://xxxxx/G/id...去找,写成\\就是从本地找
        poCtrl.webOpen("G:\\ideaProject\\springboot-demo\\online-office\\src\\main\\resources\\file\\test.pptx", OpenModeType.pptNormalEdit, "光哥");
        //pageoffice 是文件的变量,前端页面通过这个变量加载出文件
        map.put("pageoffice", poCtrl.getHtmlCode("PageOfficeCtrl1"));
        //跳转到word.html
        return "ppt";
    }

    /**
     * 保存文件接口
     *
     * @param request
     * @param response
     */
    @RequestMapping("/save")
    public void saveFile(HttpServletRequest request, HttpServletResponse response) {
        // 保存修改后的文件
        FileSaver fs = new FileSaver(request, response);
        fs.saveToFile("G:\\test\\在线编辑\\" + fs.getFileName());
        fs.close();
    }

}

word.html 页面介绍

这里只展示word.html,ppt.html和word基本一样的

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="UTF-8">
    <title>在线编辑Office文件</title>
    <script type="text/javascript" src="jquery.min.js"></script>
    <script type="text/javascript" src="pageoffice.js" id="po_js_main"></script>
</head>
<body>
<!--POBrowser打开一个窗口用来编辑文件-->
<a id="open_a" style="display: none;" onclick="javascript:POBrowser.openWindowModeless('/word','width=1200px;height=800px;');" href="#">打开文件</a>
<!-- 页面布局 -->
<div style="width:100%;height:900px; align-content: center" th:utext="${pageoffice}"></div>
<script type="text/javascript">
    function Save() {
        document.getElementById("PageOfficeCtrl1").WebSave();
    }
</script>
<script>
    $(function () {
        var userAgent = navigator.userAgent; //取得浏览器的userAgent字符串
        var isOpera = userAgent.indexOf("Opera") > -1;
		
		// 获取浏览器类型
        function getType() {
            //判断是否chorme浏览器
            if (userAgent.indexOf("Chrome") > -1) {
                return "Chrome";
            }
            //判断是否IE浏览器
            if (userAgent.indexOf("compatible") > -1 && userAgent.indexOf("MSIE") > -1 && !isOpera) {
                return "IE";
            }

            //判断是否Edge浏览器
            if (userAgent.indexOf("Trident") > -1) {
                return "Edge";
            }
        }

        // POBrowser打开一个窗口用来编辑文件
        function open() {
            $("#open_a").click();
        }

        // pageOffice有很多浏览器都不支持,不支持则使用POBrowser打开一个窗口用来编辑文件
        if (getType() != 'Edge') {
            // 延迟一秒在打开,是因为如果即时打开,可能有些文件请求还没加载好,会检测出客户端未安装pageOffice控件
            setTimeout(open, 1000)
        }

    });
</script>
</body>
</html>

启动后访问:

在线编辑Word:http://localhost:8080/word
在线编辑PPT:http://localhost:8080/ppt

在这里插入图片描述
在这里插入图片描述

源码下载

下载地址:https://github.com/y15211669315/online-office.git

  • 12
    点赞
  • 107
    收藏
    觉得还不错? 一键收藏
  • 13
    评论
评论 13
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值