(转)使⽤PageOffice实现⽂档(word,excel,pdf)在线预览
编辑
1、安装完后需要给⾃⼰的项⽬添加pageoffice4. 和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);
jquery在线库6. poCtrl1.ContextPath() + "/"); // 此⾏必须
7. poCtrl1.setTitlebar(false); // 隐藏标题栏(pageoffice的标题)
8. poCtrl1.setMenubar(false); // 隐藏菜单栏(⽂件⼀个设置)
9. poCtrl1.setOfficeToolbars(false);// 隐藏Office⼯具条(word 的编辑按钮)
0. // poCtrl1.setCustomToolbar(false);// 隐藏⾃定义⼯具栏(保存关闭全屏)
1. poCtrl1.addCustomToolButton("打印", "ShowPrintDlg()", 6);
2. poCtrl1.addCustomToolButton("全屏切换", "SwitchFullScreen()", 4);
3. poCtrl1.addCustomToolButton("关闭", "close", 21);
4. List<FileMeta> entity = List(getEntity(flag, id));
5. String path = (0));
6. String realpath = place("/", "\\\\");
7. String suffix = path.substring(path.lastIndexOf(".") + 1);
8. if ("doc".equals(suffix) || "docx".equals(suffix)) {
9. poCtrl1.webOpen(realpath, OpenModeType.docReadOnly, "张三");
0. } else if ("xls".equals(suffix) || "xlsx".equals(suffix)) {
1. poCtrl1.webOpen(realpath, OpenModeType.xlsReadOnly, "张三");
2. }
3. // 打开⽂件
4. return "OpenWord";
5. }
4'、在线编辑:
1. /**
2. * 在线编辑
3. *
4. * @param request
5. * @param response
6. * @param id
7. * @param flag
8. * @return
9. */
0. @RequestMapping("openword/{flag}/{id}")
1. public String openword(HttpServletRequest request, HttpServletResponse response, @PathVariable int id,
2. @PathVariable int flag) {
3. PageOfficeCtrl poCtrl = new PageOfficeCtrl(request);
4. request.setAttribute("poCtrl", poCtrl);
5. poCtrl.setTitlebar(false);
6. // 设置服务页⾯
7. poCtrl.ContextPath() + "/");
8. // 添加保存按钮
9. poCtrl.addCustomToolButton("保存并关闭", "Save", 1);
0. poCtrl.addCustomToolButton("打印", "ShowPrintDlg()", 6);
1. poCtrl.addCustomToolButton("全屏切换", "SwitchFullScreen()", 4);
2. // 设置保存的action
3. poCtrl.ContextPath() + "/wep/savefile/" + flag);
4. // 打开word
5. List<FileMeta> entity = List(getEntity(flag, id));
6. String path = (0));
7. String realpath = place("/", "\\\\");
8. String suffix = path.substring(path.lastIndexOf(".") + 1);
9. if ("doc".equals(suffix) || "docx".equals(suffix)) {
0. poCtrl.webOpen(realpath, OpenModeType.docNormalEdit, "张三");
1. } else if ("xls".equals(suffix) || "xlsx".equals(suffix)) {
2. poCtrl.webOpen(realpath, OpenModeType.xlsNormalEdit, "张三");
3. }
4. return "Word";
5. }
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) {
0. FileSaver fs = new FileSaver(request, response);
1. if (flag == 1) {// hse 保存路径
2. fs.saveToFile("D:\\Demo\\conhse\\" + fs.getFileName());
3. } else if (flag == 2) {
4. fs.saveToFile("D:\\Demo\\doc\\" + fs.getFileName());
5. } else if (flag == 3) {
6. fs.saveToFile("D:\\Demo\\hsequa\\" + fs.getFileName());
7. } else if (flag == 4) {
8. fs.saveToFile("D:\\Demo\\eva\\" + fs.getFileName());
9. } else if (flag == 5) {
0. fs.saveToFile("D:\\Demo\\leg\\" + fs.getFileName());
1. } else if (flag == 6) {
2. fs.saveToFile("D:\\Demo\\plan\\" + fs.getFileName());
3. }
4. fs.close();
5. }
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>/</url-pattern>
9. </servlet-mapping>
0. <servlet-mapping>
1. <servlet-name>poserver</servlet-name>
2. <url-pattern>/</url-pattern>
3. </servlet-mapping>
4. <servlet-mapping>
5. <servlet-name>poserver</servlet-name>
6. <url-pattern>/</url-pattern>
7. </servlet-mapping>
8. <servlet-mapping>
9. <servlet-name>poserver</servlet-name>
0. <url-pattern>/pageoffice.js</url-pattern>
1. </servlet-mapping>
2. <servlet-mapping>
3. <servlet-name>poserver</servlet-name>
4. <url-pattern>/jquery.min.js</url-pattern>
5. </servlet-mapping>
6. <servlet-mapping>
7. <servlet-name>poserver</servlet-name>
8. <url-pattern>/pobstyle.css</url-pattern>
9. </servlet-mapping>
0. <servlet>
1. <servlet-name>adminseal</servlet-name>
2. <servlet-class>com.zhuozhengsoft.pageoffice.poserver.AdminSeal</servlet-class>
3. </servlet>
4. <servlet-mapping>
5. <servlet-name>adminseal</servlet-name>
6. <url-pattern>/</url-pattern>
7. </servlet-mapping>
8. <servlet-mapping>
9. <servlet-name>adminseal</servlet-name>
0. <url-pattern>/</url-pattern>
1. </servlet-mapping>
2. <servlet-mapping>
3. <servlet-name>adminseal</servlet-name>
4. <url-pattern>/</url-pattern>
5. </servlet-mapping>
6. <mime-mapping>
7. <extension>mht</extension>
8. <mime-type>message/rfc822</mime-type>
9. </mime-mapping>
0. <context-param>
1. <param-name>adminseal-password</param-name>
2. <param-value>111111</param-value>
3. </context-param>
4. <!-- PageOffice End -->
7、jsp配置
1. <%@ page language="java"
2. import="java.util.*,com.zhuozhengsoft.pageoffice.*"
3. pageEncoding="UTF-8"%>
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论