(轉)使用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、預覽後臺程式碼:
-
public String openbar(HttpServletRequest request, HttpServletResponse response, @PathVariable int id,
-
@PathVariable int flag) {
-
// 設定PageOffice伺服器元件
-
PageOfficeCtrl poCtrl1 = new PageOfficeCtrl(request);
-
request.setAttribute("poCtrl1", poCtrl1);
-
poCtrl1.setServerPage(request.getContextPath() + "/poserver.zz"); // 此行必須
-
poCtrl1.setTitlebar(false); // 隱藏標題欄(pageoffice的標題)
-
poCtrl1.setMenubar(false); // 隱藏選單欄(檔案一個設定)
-
poCtrl1.setOfficeToolbars(false);// 隱藏Office工具條(word 的編輯按鈕)
-
// poCtrl1.setCustomToolbar(false);// 隱藏自定義工具欄(儲存 關閉 全屏)
-
poCtrl1.addCustomToolButton("列印", "ShowPrintDlg()", 6);
-
poCtrl1.addCustomToolButton("全屏切換", "SwitchFullScreen()", 4);
-
poCtrl1.addCustomToolButton("關閉", "close", 21);
-
List<FileMeta> entity = fileMetaService.getList(getEntity(flag, id));
-
String path = ZipService.getPath(entity.get(0));
-
String realpath = path.replace("/", "\\\\");
-
String suffix = path.substring(path.lastIndexOf(".") + 1);
-
if ("doc".equals(suffix) || "docx".equals(suffix)) {
-
poCtrl1.webOpen(realpath, OpenModeType.docReadOnly, "張三");
-
} else if ("xls".equals(suffix) || "xlsx".equals(suffix)) {
-
poCtrl1.webOpen(realpath, OpenModeType.xlsReadOnly, "張三");
-
}
-
// 開啟檔案
-
return "OpenWord";
-
}
4'、線上編輯:
-
/**
-
* 線上編輯
-
*
-
* @param request
-
* @param response
-
* @param id
-
* @param flag
-
* @return
-
*/
-
@RequestMapping("openword/{flag}/{id}")
-
public String openword(HttpServletRequest request, HttpServletResponse response, @PathVariable int id,
-
@PathVariable int flag) {
-
PageOfficeCtrl poCtrl = new PageOfficeCtrl(request);
-
request.setAttribute("poCtrl", poCtrl);
-
poCtrl.setTitlebar(false);
-
// 設定服務頁面
-
poCtrl.setServerPage(request.getContextPath() + "/poserver.zz");
-
// 新增儲存按鈕
-
poCtrl.addCustomToolButton("儲存並關閉", "Save", 1);
-
poCtrl.addCustomToolButton("列印", "ShowPrintDlg()", 6);
-
poCtrl.addCustomToolButton("全屏切換", "SwitchFullScreen()", 4);
-
// 設定儲存的action
-
poCtrl.setSaveFilePage(request.getContextPath() + "/wep/savefile/" + flag);
-
// 開啟word
-
List<FileMeta> entity = fileMetaService.getList(getEntity(flag, id));
-
String path = ZipService.getPath(entity.get(0));
-
String realpath = path.replace("/", "\\\\");
-
String suffix = path.substring(path.lastIndexOf(".") + 1);
-
if ("doc".equals(suffix) || "docx".equals(suffix)) {
-
poCtrl.webOpen(realpath, OpenModeType.docNormalEdit, "張三");
-
} else if ("xls".equals(suffix) || "xlsx".equals(suffix)) {
-
poCtrl.webOpen(realpath, OpenModeType.xlsNormalEdit, "張三");
-
}
-
return "Word";
-
}
5、線上編輯需要儲存,所以有線上儲存:
-
/**
-
* 儲存
-
*
-
* @param request
-
* @param response
-
* @param flag
-
*/
-
@RequestMapping("savefile/{flag}")
-
public void savefile(HttpServletRequest request, HttpServletResponse response, @PathVariable int flag) {
-
FileSaver fs = new FileSaver(request, response);
-
if (flag == 1) {// hse 儲存路徑
-
fs.saveToFile("D:\\Demo\\conhse\\" + fs.getFileName());
-
} else if (flag == 2) {
-
fs.saveToFile("D:\\Demo\\doc\\" + fs.getFileName());
-
} else if (flag == 3) {
-
fs.saveToFile("D:\\Demo\\hsequa\\" + fs.getFileName());
-
} else if (flag == 4) {
-
fs.saveToFile("D:\\Demo\\eva\\" + fs.getFileName());
-
} else if (flag == 5) {
-
fs.saveToFile("D:\\Demo\\leg\\" + fs.getFileName());
-
} else if (flag == 6) {
-
fs.saveToFile("D:\\Demo\\plan\\" + fs.getFileName());
-
}
-
fs.close();
-
}
6、web-xml配置
-
<!-- PageOffice Begin -->
-
<servlet>
-
<servlet-name>poserver</servlet-name>
-
<servlet-class>com.zhuozhengsoft.pageoffice.poserver.Server</servlet-class>
-
</servlet>
-
<servlet-mapping>
-
<servlet-name>poserver</servlet-name>
-
<url-pattern>/poserver.zz</url-pattern>
-
</servlet-mapping>
-
<servlet-mapping>
-
<servlet-name>poserver</servlet-name>
-
<url-pattern>/sealsetup.exe</url-pattern>
-
</servlet-mapping>
-
<servlet-mapping>
-
<servlet-name>poserver</servlet-name>
-
<url-pattern>/posetup.exe</url-pattern>
-
</servlet-mapping>
-
<servlet-mapping>
-
<servlet-name>poserver</servlet-name>
-
<url-pattern>/pageoffice.js</url-pattern>
-
</servlet-mapping>
-
<servlet-mapping>
-
<servlet-name>poserver</servlet-name>
-
<url-pattern>/jquery.min.js</url-pattern>
-
</servlet-mapping>
-
<servlet-mapping>
-
<servlet-name>poserver</servlet-name>
-
<url-pattern>/pobstyle.css</url-pattern>
-
</servlet-mapping>
-
<servlet>
-
<servlet-name>adminseal</servlet-name>
-
<servlet-class>com.zhuozhengsoft.pageoffice.poserver.AdminSeal</servlet-class>
-
</servlet>
-
<servlet-mapping>
-
<servlet-name>adminseal</servlet-name>
-
<url-pattern>/adminseal.zz</url-pattern>
-
</servlet-mapping>
-
<servlet-mapping>
-
<servlet-name>adminseal</servlet-name>
-
<url-pattern>/loginseal.zz</url-pattern>
-
</servlet-mapping>
-
<servlet-mapping>
-
<servlet-name>adminseal</servlet-name>
-
<url-pattern>/sealimage.zz</url-pattern>
-
</servlet-mapping>
-
<mime-mapping>
-
<extension>mht</extension>
-
<mime-type>message/rfc822</mime-type>
-
</mime-mapping>
-
<context-param>
-
<param-name>adminseal-password</param-name>
-
<param-value>111111</param-value>
-
</context-param>
-
<!-- PageOffice End -->
7、jsp配置
-
<%@ page language="java"
-
import="java.util.*,com.zhuozhengsoft.pageoffice.*"
-
pageEncoding="UTF-8"%>
-
<%
-
PageOfficeCtrl poCtrl = (PageOfficeCtrl) request.getAttribute("poCtrl");
-
%>
-
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
-
<html>
-
<head>
-
<title>最簡單的開啟儲存Word檔案</title>
-
<!-- <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> -->
-
<script type="text/javascript" src="../../../jquery.min.js"></script>
-
<script type="text/javascript" src="../../../pageoffice.js" id="po_js_main"></script>
-
</head>
-
<body>
-
<div style="width: 100%; height: 100%;">
-
<%=poCtrl.getHtmlCode("PageOfficeCtrl1")%>
-
</div>
-
<script type="text/javascript">
-
function Save() {
-
document.getElementById("PageOfficeCtrl1").WebSave();
-
POBrowser.closeWindow();//關閉POBrowser視窗
-
}
-
function ShowPrintDlg() {
-
document.getElementById("PageOfficeCtrl1").ShowDialog(4); //列印對話方塊
-
}
-
function SwitchFullScreen() {//全屏
-
document.getElementById("PageOfficeCtrl1").FullScreen = !document
-
.getElementById("PageOfficeCtrl1").FullScreen;
-
}
-
</script>
-
</body>
-
</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