[轉載]Java整合PageOffice線上開啟編輯word檔案 - Spring Boot

來源:互聯網
上載者:User

標籤:eclipse   根目錄   word   按鈕   browser   cat   too   app   dex   

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.