1. 程式人生 > >[轉載]Java集成PageOffice在線打開編輯word文件 - Spring Boot

[轉載]Java集成PageOffice在線打開編輯word文件 - Spring Boot

ati ppi leaf oid oot url ava () sqlit

開發環境: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="width: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

[轉載]Java集成PageOffice在線打開編輯word文件 - Spring Boot