大家好,又见面了,我是你们的朋友全栈君。
最近发现一款不错的插件的PageOffice,地址是:http://www.zhuozhengsoft.com/Technical/ 他可以实现word,excel、pdf在线预览以及在线编辑。虽然商用的话需要收费,但是有免费的试用版,在实现自己毕业设计或是做样品的时候 是一个不错的选择。他同时支持java\c#\php.一旦有了正真的项目花钱再买也可以。同时自己也可以熟悉如何使用。接着下面实现在线预览。
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";
}
/**
* 保存
*
* @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 V4.0 专业版试用序列号:I1I92-4M46-R6B2-E15TX
PageOffice V4.0 企业版试用序列号:I7TGD-71VV-FYD8-4NMYP
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/135036.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...