[转载]Java集成PageOffice在线打开编辑word文件

开发环境:JDK1.8、Eclipse、Sping Boot + Thymeleaf框架。

一. 构建Sping Boot + Thymeleaf框架的项目(不再详述):

  1. 新建一个maven project项目:demo。

  2. 修改pom.xml配置,把项目配置为Spring Boot项目;

  3. 配置Thymeleaf:添加Thymeleaf依赖,并在application.properties文件中添加Thymeleaf的配置;

  4. 新建DemoController,添加showWord、showIndex方法:

复制代码
@RequestMapping(value="/word", method=RequestMethod.GET)
public ModelAndView showWord(HttpServletRequest request, Map<String,Object> map){
    ModelAndView mv = new ModelAndView("Word");
    return mv;
}
@RequestMapping(value="/index", method=RequestMethod.GET)
public ModelAndView showIndex(){
    ModelAndView mv = new ModelAndView("Index");
    return mv;
}
复制代码

  5.  新建Thymeleaf模板页:Word.html、Index.html;

  6. 运行demo项目,并成功访问:http://localhost:8080/index

二、 集成PageOffice

  1.  在pom.xml中添加PageOffice的依赖:

复制代码
<!-- 添加Sqlite依赖(可选:如果不需要使用印章功能的话,不需要添加此依赖)-->
<dependency>
    <groupId>org.xerial</groupId>
    <artifactId>sqlite-jdbc</artifactId>
    <version>3.7.2</version>
</dependency>
<!-- 添加PageOffice依赖(必须) -->
<dependency>
    <groupId>com.zhuozhengsoft</groupId>
    <artifactId>pageoffice</artifactId>
    <version>4.3.0.2</version>
</dependency>
复制代码

  2.  在application.properties文件中添加两个自定义参数配置,posyspath:指定一个磁盘目录用来存放PageOffice注册成功之后生成的license.lic文件;popassword:设置PageOffice自带印章管理程序的登录密码;以备给PageOffice的服务器端Servlet程序使用:

########################################################
###PageOffice 
########################################################
posyspath=d:/lic/
popassword=111111

  3.  在DemoController中添加代码获取上一步在application.properties中定义的两个参数:

@Value("${posyspath}") 
private String poSysPath;
@Value("${popassword}") 
private String poPassWord;

  4. 在DemoController中添加PageOffice的Servlet的注册代码:

复制代码
     /**
     * 添加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;
        //
    }
复制代码

  5. 在DemoController的showWord方法中添加创建PageOfficeCtrl对象的代码,其中WebOpen方法的第一个参数是office文件在服务器端的磁盘路径,在此demo中暂时使用常量:d:\test.doc

  

复制代码
    @RequestMapping(value="/word", method=RequestMethod.GET)
    public ModelAndView showWord(HttpServletRequest request, Map<String,Object> map){
        //--- PageOffice的调用代码 开始 -----
        PageOfficeCtrl poCtrl=new PageOfficeCtrl(request);
        poCtrl.setServerPage("/poserver.zz");//设置授权程序servlet
        poCtrl.addCustomToolButton("保存","Save",1); //添加自定义按钮
        poCtrl.setSaveFilePage("/save");//设置保存的action
        poCtrl.webOpen("d:\test.doc",OpenModeType.docAdmin,"张三");
        map.put("pageoffice",poCtrl.getHtmlCode("PageOfficeCtrl1"));
        //--- PageOffice的调用代码 结束 -----
        ModelAndView mv = new ModelAndView("Word");
        return mv;
    }
复制代码

  6. 在Word.html中添加PageOffice客户端控件所在的div和js代码:

复制代码
<div style="1000px;height:700px;" th:utext="${pageoffice}"> </div>
<script type="text/javascript">
    function Save() {
        document.getElementById("PageOfficeCtrl1").WebSave();
    }
</script>
复制代码

  7. 在Word.html中添加pageoffice.js和jquery.min.js的引用,并添打开文件的超链接:

<script type="text/javascript" src="jquery.min.js"></script>
<script type="text/javascript" src="pageoffice.js" id="po_js_main"></script>
<a href="javascript:POBrowser.openWindowModeless('/word','width=1200px;height=800px;');">打开文件</a>

  8. 在DemoController添加saveFile方法,用来接收PageOffice客户端上传的文件流并保存到服务器指定磁盘目录,在此demo中暂时使用常量:d:\

复制代码
    @RequestMapping("/save")
    public void saveFile(HttpServletRequest request, HttpServletResponse response){
        FileSaver fs = new FileSaver(request, response);
        fs.saveToFile("d:\" + fs.getFileName());
        fs.close();
    }
复制代码

  9.  在d盘根目录下准备一个test.doc文件(不要用0字节的文件)以备测试;

  10. 运行demo项目,访问:http://localhost:8080/index点击“打开文件”的超链接即可在线打开、编辑和保存文件。

三、源码下载

  下载地址:http://www.zhuozhengsoft.com/download/PageOffice4.3.0.2ForSpringBoot.zip

原文地址:https://www.cnblogs.com/qq742655/p/9025784.html