1. 程式人生 > >(轉)使用PageOffice實現文件(word,excel,pdf)線上預覽編輯

(轉)使用PageOffice實現文件(word,excel,pdf)線上預覽編輯

首先自己需要安裝PageOffice控制元件 目前最新版本是4.2 。下載地址是:http://www.zhuozhengsoft.com/dowm/ 其中包括例項程式碼。

1、安裝完後需要給自己的專案新增pageoffice4.jar 和java 新增jar包沒有區別。

2、啟動的時候第一次使用試用版會彈出一個框,需要新增一些資訊,有一個序列號在最後我寫了。剩下 那個就是隨便填寫就可以。同時他會生成一個檔案就是license.lic。他會在tomcat 專案部署的lib下。假如每次啟動都需要新增的話。你就需要把tomcat 部署專案lib 裡的license.lic 複製到你的本地專案中。

3、預覽後臺程式碼:

  1. public String openbar(HttpServletRequest request, HttpServletResponse response, @PathVariable int id,

  2. @PathVariable int flag) {

  3. // 設定PageOffice伺服器元件

  4. PageOfficeCtrl poCtrl1 = new PageOfficeCtrl(request);

  5. request.setAttribute("poCtrl1", poCtrl1);

  6. poCtrl1.setServerPage(request.getContextPath() + "/poserver.zz"); // 此行必須

  7. poCtrl1.setTitlebar(false); // 隱藏標題欄(pageoffice的標題)

  8. poCtrl1.setMenubar(false); // 隱藏選單欄(檔案一個設定)

  9. poCtrl1.setOfficeToolbars(false);// 隱藏Office工具條(word 的編輯按鈕)

  10. // poCtrl1.setCustomToolbar(false);// 隱藏自定義工具欄(儲存 關閉 全屏)

  11. poCtrl1.addCustomToolButton("列印", "ShowPrintDlg()", 6);

  12. poCtrl1.addCustomToolButton("全屏切換", "SwitchFullScreen()", 4);

  13. poCtrl1.addCustomToolButton("關閉", "close", 21);

  14. List<FileMeta> entity = fileMetaService.getList(getEntity(flag, id));

  15. String path = ZipService.getPath(entity.get(0));

  16. String realpath = path.replace("/", "\\\\");

  17. String suffix = path.substring(path.lastIndexOf(".") + 1);

  18. if ("doc".equals(suffix) || "docx".equals(suffix)) {

  19. poCtrl1.webOpen(realpath, OpenModeType.docReadOnly, "張三");

  20. } else if ("xls".equals(suffix) || "xlsx".equals(suffix)) {

  21. poCtrl1.webOpen(realpath, OpenModeType.xlsReadOnly, "張三");

  22. }

  23. // 開啟檔案

  24. return "OpenWord";

  25. }


4'、線上編輯:

  1. /**

  2. * 線上編輯

  3. *

  4. * @param request

  5. * @param response

  6. * @param id

  7. * @param flag

  8. * @return

  9. */

  10. @RequestMapping("openword/{flag}/{id}")

  11. public String openword(HttpServletRequest request, HttpServletResponse response, @PathVariable int id,

  12. @PathVariable int flag) {

  13. PageOfficeCtrl poCtrl = new PageOfficeCtrl(request);

  14. request.setAttribute("poCtrl", poCtrl);

  15. poCtrl.setTitlebar(false);

  16. // 設定服務頁面

  17. poCtrl.setServerPage(request.getContextPath() + "/poserver.zz");

  18. // 新增儲存按鈕

  19. poCtrl.addCustomToolButton("儲存並關閉", "Save", 1);

  20. poCtrl.addCustomToolButton("列印", "ShowPrintDlg()", 6);

  21. poCtrl.addCustomToolButton("全屏切換", "SwitchFullScreen()", 4);

  22. // 設定儲存的action

  23. poCtrl.setSaveFilePage(request.getContextPath() + "/wep/savefile/" + flag);

  24. // 開啟word

  25. List<FileMeta> entity = fileMetaService.getList(getEntity(flag, id));

  26. String path = ZipService.getPath(entity.get(0));

  27. String realpath = path.replace("/", "\\\\");

  28. String suffix = path.substring(path.lastIndexOf(".") + 1);

  29. if ("doc".equals(suffix) || "docx".equals(suffix)) {

  30. poCtrl.webOpen(realpath, OpenModeType.docNormalEdit, "張三");

  31. } else if ("xls".equals(suffix) || "xlsx".equals(suffix)) {

  32. poCtrl.webOpen(realpath, OpenModeType.xlsNormalEdit, "張三");

  33. }

  34. return "Word";

  35. }

5、線上編輯需要儲存,所以有線上儲存:

  1. /**

  2. * 儲存

  3. *

  4. * @param request

  5. * @param response

  6. * @param flag

  7. */

  8. @RequestMapping("savefile/{flag}")

  9. public void savefile(HttpServletRequest request, HttpServletResponse response, @PathVariable int flag) {

  10. FileSaver fs = new FileSaver(request, response);

  11. if (flag == 1) {// hse 儲存路徑

  12. fs.saveToFile("D:\\Demo\\conhse\\" + fs.getFileName());

  13. } else if (flag == 2) {

  14. fs.saveToFile("D:\\Demo\\doc\\" + fs.getFileName());

  15. } else if (flag == 3) {

  16. fs.saveToFile("D:\\Demo\\hsequa\\" + fs.getFileName());

  17. } else if (flag == 4) {

  18. fs.saveToFile("D:\\Demo\\eva\\" + fs.getFileName());

  19. } else if (flag == 5) {

  20. fs.saveToFile("D:\\Demo\\leg\\" + fs.getFileName());

  21. } else if (flag == 6) {

  22. fs.saveToFile("D:\\Demo\\plan\\" + fs.getFileName());

  23. }

  24. fs.close();

  25. }

6、web-xml配置

  1. <!-- PageOffice Begin -->

  2. <servlet>

  3. <servlet-name>poserver</servlet-name>

  4. <servlet-class>com.zhuozhengsoft.pageoffice.poserver.Server</servlet-class>

  5. </servlet>

  6. <servlet-mapping>

  7. <servlet-name>poserver</servlet-name>

  8. <url-pattern>/poserver.zz</url-pattern>

  9. </servlet-mapping>

  10. <servlet-mapping>

  11. <servlet-name>poserver</servlet-name>

  12. <url-pattern>/sealsetup.exe</url-pattern>

  13. </servlet-mapping>

  14. <servlet-mapping>

  15. <servlet-name>poserver</servlet-name>

  16. <url-pattern>/posetup.exe</url-pattern>

  17. </servlet-mapping>

  18. <servlet-mapping>

  19. <servlet-name>poserver</servlet-name>

  20. <url-pattern>/pageoffice.js</url-pattern>

  21. </servlet-mapping>

  22. <servlet-mapping>

  23. <servlet-name>poserver</servlet-name>

  24. <url-pattern>/jquery.min.js</url-pattern>

  25. </servlet-mapping>

  26. <servlet-mapping>

  27. <servlet-name>poserver</servlet-name>

  28. <url-pattern>/pobstyle.css</url-pattern>

  29. </servlet-mapping>

  30. <servlet>

  31. <servlet-name>adminseal</servlet-name>

  32. <servlet-class>com.zhuozhengsoft.pageoffice.poserver.AdminSeal</servlet-class>

  33. </servlet>

  34. <servlet-mapping>

  35. <servlet-name>adminseal</servlet-name>

  36. <url-pattern>/adminseal.zz</url-pattern>

  37. </servlet-mapping>

  38. <servlet-mapping>

  39. <servlet-name>adminseal</servlet-name>

  40. <url-pattern>/loginseal.zz</url-pattern>

  41. </servlet-mapping>

  42. <servlet-mapping>

  43. <servlet-name>adminseal</servlet-name>

  44. <url-pattern>/sealimage.zz</url-pattern>

  45. </servlet-mapping>

  46. <mime-mapping>

  47. <extension>mht</extension>

  48. <mime-type>message/rfc822</mime-type>

  49. </mime-mapping>

  50. <context-param>

  51. <param-name>adminseal-password</param-name>

  52. <param-value>111111</param-value>

  53. </context-param>

  54. <!-- PageOffice End -->

7、jsp配置

  1. <%@ page language="java"

  2. import="java.util.*,com.zhuozhengsoft.pageoffice.*"

  3. pageEncoding="UTF-8"%>

  4. <%

  5. PageOfficeCtrl poCtrl = (PageOfficeCtrl) request.getAttribute("poCtrl");

  6. %>

  7. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

  8. <html>

  9. <head>

  10. <title>最簡單的開啟儲存Word檔案</title>

  11. <!-- <script type="text/javascript" >

  12. function getPageOfficeJsUrl(){var d=document.URL;var c=d.substring(0,find(d,"/",3)+1);var a=d.substring(0,find(d,"/",4)+1);var f=new Array();f[0]=c;f[1]=a;for(var b=0;b<f.length;b++){var e="pageoffice.js";ajax({url:f[b]+"/pageoffice.js",type:"GET",data:{name:"super"},dataType:"json",success:function(h,k){if(d.indexOf("?")>0){d=d.substring(0,d.indexOf("?"))}var g=countChar(d.substring(f[b].length),"/");for(var i=0;i<g;i++){e="../"+e}alert('src="'+e+'"')},fail:function(g){}})}}function countChar(d,e){var b=0;for(var a=0;a<d.length;a++){if(d.charAt(a)==e){b++}}return b}function find(e,b,c){var a=0;for(var d=0;d<c;d++){a=e.indexOf(b,a+1)}return a}getPageOfficeJsUrl();function ajax(a){a=a||{};a.type=(a.type||"GET").toUpperCase();a.dataType=a.dataType||"json";var c=formatParams(a.data);if(window.XMLHttpRequest){var b=new XMLHttpRequest()}else{var b=new ActiveXObject("Microsoft.XMLHTTP")}b.onreadystatechange=function(){if(b.readyState==4){var d=b.status;if(d>=200&&d<300){a.success&&a.success(b.responseText,b.responseXML)}else{a.fail&&a.fail(d)}}};if(a.type=="GET"){b.open("GET",a.url+"?"+c,false);b.send(null)}else{if(a.type=="POST"){b.open("POST",a.url,false);b.setRequestHeader("Content-Type","application/x-www-form-urlencoded");b.send(c)}}}function formatParams(c){var a=[];for(var b in c){a.push(encodeURIComponent(b)+"="+encodeURIComponent(c[b]))}a.push(("v="+Math.random()).replace(".",""));return a.join("&")};

  13. </script> -->

  14. <script type="text/javascript" src="../../../jquery.min.js"></script>

  15. <script type="text/javascript" src="../../../pageoffice.js" id="po_js_main"></script>

  16. </head>

  17. <body>

  18. <div style="width: 100%; height: 100%;">

  19. <%=poCtrl.getHtmlCode("PageOfficeCtrl1")%>

  20. </div>

  21. <script type="text/javascript">

  22. function Save() {

  23. document.getElementById("PageOfficeCtrl1").WebSave();

  24. POBrowser.closeWindow();//關閉POBrowser視窗

  25. }

  26. function ShowPrintDlg() {

  27. document.getElementById("PageOfficeCtrl1").ShowDialog(4); //列印對話方塊

  28. }

  29. function SwitchFullScreen() {//全屏

  30. document.getElementById("PageOfficeCtrl1").FullScreen = !document

  31. .getElementById("PageOfficeCtrl1").FullScreen;

  32. }

  33. </script>

  34. </body>

  35. </html>


注意:有時候jsp 和他的js 不在同一個目錄, 這時候在jsp頁面 使用<script type="text/javascript" >
function getPageOfficeJsUrl(){var d=document.URL;var c=d.substring(0,find(d,"/",3)+1);var a=d.substring(0,find(d,"/",4)+1);var f=new Array();f[0]=c;f[1]=a;for(var b=0;b<f.length;b++){var e="pageoffice.js";ajax({url:f[b]+"/pageoffice.js",type:"GET",data:{name:"super"},dataType:"json",success:function(h,k){if(d.indexOf("?")>0){d=d.substring(0,d.indexOf("?"))}var g=countChar(d.substring(f[b].length),"/");for(var i=0;i<g;i++){e="../"+e}alert('src="'+e+'"')},fail:function(g){}})}}function countChar(d,e){var b=0;for(var a=0;a<d.length;a++){if(d.charAt(a)==e){b++}}return b}function find(e,b,c){var a=0;for(var d=0;d<c;d++){a=e.indexOf(b,a+1)}return a}getPageOfficeJsUrl();function ajax(a){a=a||{};a.type=(a.type||"GET").toUpperCase();a.dataType=a.dataType||"json";var c=formatParams(a.data);if(window.XMLHttpRequest){var b=new XMLHttpRequest()}else{var b=new ActiveXObject("Microsoft.XMLHTTP")}b.onreadystatechange=function(){if(b.readyState==4){var d=b.status;if(d>=200&&d<300){a.success&&a.success(b.responseText,b.responseXML)}else{a.fail&&a.fail(d)}}};if(a.type=="GET"){b.open("GET",a.url+"?"+c,false);b.send(null)}else{if(a.type=="POST"){b.open("POST",a.url,false);b.setRequestHeader("Content-Type","application/x-www-form-urlencoded");b.send(c)}}}function formatParams(c){var a=[];for(var b in c){a.push(encodeURIComponent(b)+"="+encodeURIComponent(c[b]))}a.push(("v="+Math.random()).replace(".",""));return a.join("&")};
</script>

就可以輸出你本頁面pageoffice.js具體位置。

這就是簡單的使用更詳細具體需要去官網API看看。還有一些可以參考使用的,還有一些圖示對應使用。

PageOffice V4.0 標準版試用序列號:5LB6J-1JIL-XE8X-AYY44
PageOffice V4.0 專業版試用序列號:I1I92-4M46-R6B2-E15TX
PageOffice V4.0 企業版試用序列號:I7TGD-71VV-FYD8-4NMYP