java 在线打开 word 或者 excel文件,并上传到服务器

一、需求描述。 

 需要将客户端电脑的 office 办公文件上传到服务器

二:方案

总体方案用 pageoffice 组件操作 word 或者 excel 文件

1:在线打开文档  用pageoffice 在线打开编辑保存的功能

2:上传本地文件用 pageffice 提供的导入文件的js 方法

 document.getElementById("PageOfficeCtrl1").ExcelImportDialog(); //导入 excel 文件
   
   
   
   
 document.getElementById("PageOfficeCtrl1").WordImportDialog();//导入word文件
   
   
   
   

三、具体的实现过程

1:配置web.xml


   
   
   
   
  1. <servlet>
  2. <servlet-name>poserver servlet-name>
  3. <servlet-class>com.zhuozhengsoft.pageoffice.poserver.Server servlet-class>
  4. servlet>
  5. <servlet-mapping>
  6. <servlet-name>poserver servlet-name>
  7. <url-pattern>/poserver.zz url-pattern>
  8. servlet-mapping>
  9. <servlet-mapping>
  10. <servlet-name>poserver servlet-name>
  11. <url-pattern>/sealsetup.exe url-pattern>
  12. servlet-mapping>
  13. <servlet-mapping>
  14. <servlet-name>poserver servlet-name>
  15. <url-pattern>/posetup.exe url-pattern>
  16. servlet-mapping>
  17. <servlet-mapping>
  18. <servlet-name>poserver servlet-name>
  19. <url-pattern>/pageoffice.js url-pattern>
  20. servlet-mapping>
  21. <servlet-mapping>
  22. <servlet-name>poserver servlet-name>
  23. <url-pattern>/jquery.min.js url-pattern>
  24. servlet-mapping>
  25. <servlet-mapping>
  26. <servlet-name>poserver servlet-name>
  27. <url-pattern>/pobstyle.css url-pattern>
  28. servlet-mapping>
  29. <servlet>
  30. <servlet-name>adminseal servlet-name>
  31. <servlet-class>com.zhuozhengsoft.pageoffice.poserver.AdminSeal servlet-class>
  32. servlet>
  33. <servlet-mapping>
  34. <servlet-name>adminseal servlet-name>
  35. <url-pattern>/adminseal.zz url-pattern>
  36. servlet-mapping>
  37. <servlet-mapping>
  38. <servlet-name>adminseal servlet-name>
  39. <url-pattern>/loginseal.zz url-pattern>
  40. servlet-mapping>
  41. <servlet-mapping>
  42. <servlet-name>adminseal servlet-name>
  43. <url-pattern>/sealimage.zz url-pattern>
  44. servlet-mapping>
  45. <mime-mapping>
  46. <extension>mht extension>
  47. <mime-type>message/rfc822 mime-type>
  48. mime-mapping>
  49. <context-param>
  50. <param-name>adminseal-password param-name>
  51. <param-value>111111 param-value>
  52. context-param>

2:实现代码(以word为例)

(1)导入本地文件


   
   
   
   
  1. v <%@ page language="java" import="java.util.*,java.text.SimpleDateFormat" pageEncoding="utf-8"%>
  2. <%@ page
  3. import= "com.zhuozhengsoft.pageoffice.*,com.zhuozhengsoft.pageoffice.wordwriter.*"%>
  4. <%
  5. PageOfficeCtrl poCtrl = new PageOfficeCtrl( request);
  6. //设置授权程序servlet
  7. poCtrl.setServerPage( request.getContextPath()+ "/poserver.zz");
  8. //添加自定义按钮(第二个参数对应页面中js 方法名)
  9. poCtrl.addCustomToolButton( "保存", "Save", 1);
  10. poCtrl.addCustomToolButton( "导入文件", "importData()", 15);
  11. poCtrl.addCustomToolButton( "提交数据", "submitData()", 1);
  12. //设置保存页面
  13. poCtrl.setSaveFilePage( "SaveFile.jsp");
  14. WordDocument doc = new WordDocument();
  15. poCtrl.setWriter(doc);
  16. %>
  17. <html>
  18. <head>
  19. <title>导入文件 title>
  20. head>
  21. <body>
  22. <script type="text/javascript">
  23. function importData() {
  24. document.getElementById( "PageOfficeCtrl1").WordImportDialog();
  25. }
  26. function Save() {
  27. document.getElementById( "PageOfficeCtrl1").WebSave();
  28. }
  29. script>
  30. <div style="width: auto; height: 600px;">
  31. <%=poCtrl.getHtmlCode("PageOfficeCtrl1")%>
  32. div>
  33. body>
  34. html>

(2)保存上传到服务器


   
   
   
   
  1. <%@ page language="java" import="java.util.*,com.zhuozhengsoft.pageoffice.*" pageEncoding="utf-8"%>
  2. <%
  3. FileSaver fs= new FileSaver( request, response);
  4. fs.saveToFile( "d:\\aa.doc");
  5. fs.close();
  6. %>

 

如果要导入excel文件

1:


   
   
   
   
  1. WordDocument doc = new WordDocument();
  2. poCtrl.setWriter(doc);

改为


   
   
   
   
  1. Workbook wb = new Workbook();
  2. Sheet sheet = wb.openSheet( "Sheet1");
  3. poCtrl.setWriter(wb);

2:


   
   
   
   
  1. function importData() {
  2. document.getElementById( "PageOfficeCtrl1").WordImportDialog();
  3. }

改为


   
   
   
   
  1. function importData() {
  2. document.getElementById( "PageOfficeCtrl1").ExcelImportDialog();
  3. }

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/

 

你可能感兴趣的:(word)