一、需求描述。
需要将客户端电脑的 office 办公文件上传到服务器
二:方案
总体方案用 pageoffice 组件操作 word 或者 excel 文件
1:在线打开文档 用pageoffice 在线打开编辑保存的功能
2:上传本地文件用 pageffice 提供的导入文件的js 方法
document.getElementById("PageOfficeCtrl1").ExcelImportDialog(); //导入 excel 文件
document.getElementById("PageOfficeCtrl1").WordImportDialog();//导入word文件
三、具体的实现过程
1:配置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 -->
2:实现代码(以word为例)
(1)导入本地文件
v<%@ page language="java" import="java.util.*,java.text.SimpleDateFormat" pageEncoding="utf-8"%>
<%@ page
import="com.zhuozhengsoft.pageoffice.*,com.zhuozhengsoft.pageoffice.wordwriter.*"%>
<%
PageOfficeCtrl poCtrl = new PageOfficeCtrl(request);
//设置授权程序servlet
poCtrl.setServerPage(request.getContextPath()+"/poserver.zz");
//添加自定义按钮(第二个参数对应页面中js 方法名)
poCtrl.addCustomToolButton("保存","Save",1);
poCtrl.addCustomToolButton("导入文件", "importData()", 15);
poCtrl.addCustomToolButton("提交数据", "submitData()", 1);
//设置保存页面
poCtrl.setSaveFilePage("SaveFile.jsp");
WordDocument doc = new WordDocument();
poCtrl.setWriter(doc);
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>导入文件</title>
</head>
<body>
<!-- ***************************************PageOffice组件的使用****************************************** -->
<script type="text/javascript">
function importData() {
document.getElementById("PageOfficeCtrl1").WordImportDialog();
}
function Save() {
document.getElementById("PageOfficeCtrl1").WebSave();
}
</script>
<div style="width: auto; height: 600px;">
<%=poCtrl.getHtmlCode("PageOfficeCtrl1")%>
</div>
<!-- ***************************************PageOffice组件的使用****************************************** -->
</body>
</html>
(2)保存上传到服务器
<%@ page language="java" import="java.util.*,com.zhuozhengsoft.pageoffice.*" pageEncoding="utf-8"%>
<%
FileSaver fs=new FileSaver(request,response);
fs.saveToFile("d:\\aa.doc");
fs.close();
%>
如果要导入excel文件
1:
WordDocument doc = new WordDocument();
poCtrl.setWriter(doc);
改为
Workbook wb = new Workbook();
Sheet sheet = wb.openSheet("Sheet1");
poCtrl.setWriter(wb);
2:
function importData() {
document.getElementById("PageOfficeCtrl1").WordImportDialog();
}
改为
function importData() {
document.getElementById("PageOfficeCtrl1").ExcelImportDialog();
}
3:保存代码
fs.saveToFile("d:\\aa.doc");
改为
fs.saveToFile("d:\\aa.xls");
四:最终的效果(和本地office的打开功能一样)
访问页面.此时会提示安装插件,点击安装成功后提示注册,填写相关信息,填写注册码Q37LN-W8NI-KFSQ-LEY3Y就可以打开文档.
注意:如果需要更丰富的功能,大家可以去pageoffice官网下载示例代码直接将samples4文件夹扔到Tomcat的webapps下,启动Tomcat,浏览器访问http://localhost:8080/Samples4/index.html,查看示例中的下面一个链接,直接看samples4文件夹下WordTable文件夹里面的代码.
刚开始接触pageoffice的话,也可以看视频快速上手http://www.zhuozhengsoft.com/Technical/