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

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

PageOffice官网: http://www.zhuozhengsoft.com/
PageOffice集成说明: https://www.kancloud.cn/pageoffice_course_group/pageoffice_course/652260

1. 下载依赖:

下载jar依赖:http://www.zhuozhengsoft.com/dowm/
在这里插入图片描述

2. 添加我们的jar包到项目中

在这里插入图片描述

在项目的根路径下面建立lib文件夹,然后放入jar包:
在这里插入图片描述

在pom.xml中导入依赖(导入现有的jar包):

<dependency>
    <groupId>com.zhuozheng</groupId>
    <artifactId>pageoffice</artifactId>
    <version>4.6.0.4</version>
    <scope>system</scope>
    <systemPath>${project.basedir}/lib/pageoffice5.1.0.1.jar</systemPath>
</dependency>
设置maven的打包属性,打包的时候把我们的jar包也进行一个打包;
<plugins>
  <plugin>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-maven-plugin</artifactId>
		<!--  打包的时候把本地的jar包也进行到一个导入-->
      <configuration>
          <includeSystemScope>true</includeSystemScope>
      </configuration>
  </plugin>
</plugins>

3. 配置我们的项

在项目的路径下建立如图所示的目录结构,doc就是在线编辑的文档数据。
在这里插入图片描述

application.yml:

spring:
  thymeleaf:
    cache: false
    prefix: classpath:/templates/
    check-template-location: true
    suffix: .html
    encoding: UTF-8
    content-type: text/html
    mode: HTML5
pageoffice:
  docpath: /MY-springboot-pageoffice/pageoffice/doc/
  popassword: 123456
  posyspath: /MY-springboot-pageoffice/pageoffice/lic/
server:
  port: 8082
logging:
  level:
    root: info

IndexController:

@Controller
public class IndexController {

    @RequestMapping("/index")
    public String getIndex(){
        System.out.println("进入方法");
        return "index";
    }

}
ApiController:
@RestController
@RequestMapping("/api")
public class AipController {


    @Value("${pageoffice.posyspath}")
    private String poSysPath;

    @Value("${pageoffice.popassword}")
    private String poPassWord;

    @Value("${pageoffice.docpath}")
    private String docPath;

    /**
     * 被`@PostConstruct`修饰的方法会在服务器加载Servlet的时候运行,并且只会被服务器执行一次。PostConstruct在构造函数之后执行,init()方法之前执行。
     */
    @PostConstruct
    public void init() {
        poSysPath = Constants.PROJECT_ROOT_DIRECTORY + poSysPath;
        docPath = Constants.PROJECT_ROOT_DIRECTORY + docPath;
    }

    @RequestMapping("/hello")
    public String hello() {
        return "HelloWorld~";
    }

    @RequestMapping(value = "/word", method = RequestMethod.GET)
    public ModelAndView showWord(HttpServletRequest request, Map<String, Object> map) {
        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");
        // 打开word
        poCtrl.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) {
        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");
        // 打开word
        poCtrl.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) {
        FileSaver fs = new FileSaver(request, response);
        fs.saveToFile(docPath + fs.getFileName());
        fs.close();
    }

    /**
     * 添加PageOffice的服务器端授权程序Servlet(必须)
     *
     * @return
     */
    @Bean
    public 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(可选)
     */
    @Bean
    public 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;
    }
}

启动的常量类: Constants:

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";

}

在templates下面建立三个文件,然后实现我们的功能;
index.html:

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<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:8082/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:8082/api/word','width=1200px;height=800px;');">打开Word文档</a>
        </li>
        <li>
            <a href="javascript:POBrowser.openWindowModeless('http://localhost:8082/api/excel','width=1200px;height=800px;');">打开Excel文档</a>
        </li>
    </ol>
</div>
</body>
</html>

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>

启动项目访问即可,第一次需要下载客户端,然后填入验证码(可以试用),然后就可以进行编辑,可以直接进行保存。

  • 4
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

吴涛_1618

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值