1. 需引入的依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!-- 添加Sqlite依赖(可选:如果不需要使用印章功能的话,不需要添加此依赖 )-->
<dependency>
<groupId>org.xerial</groupId>
<artifactId>sqlite-jdbc</artifactId>
<version>3.7.2</version>
</dependency>
<!-- 添加PageOffice依赖(必须) -->
<dependency>
<groupId>com.zhuozhengsoft</groupId>
<artifactId>pageoffice</artifactId>
<version>5.4.0.3</version>
</dependency>
<!--注意版本保持一致 poi poi-ooxml poi-scratchpad-->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.1.2</version>
</dependency>
<!-- 操作doc ppt xls -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-scratchpad</artifactId>
<version>4.1.2</version>
</dependency>
<!-- 操作docx pptx xlsx -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.1.2</version>
</dependency>
</dependencies>
2. 项目结构
3. 后端代码
package com.test.controller;
import com.zhuozhengsoft.pageoffice.FileSaver;
import com.zhuozhengsoft.pageoffice.OpenModeType;
import com.zhuozhengsoft.pageoffice.PageOfficeCtrl;
import com.zhuozhengsoft.pageoffice.excelwriter.Workbook;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;
/**
* @author Administrator
*/
@RestController
public class DemoController {
@Value("${posyspath}")
private String poSysPath;
@Value("${popassword}")
private String poPassWord;
@RequestMapping("/hello")
public String test() {
System.out.println("hello run");
return "Hello";
}
@RequestMapping(value = "/index", method = RequestMethod.GET)
public ModelAndView showIndex() {
ModelAndView mv = new ModelAndView("Index");
return mv;
}
@RequestMapping(value = "/word", method = RequestMethod.GET)
public ModelAndView showWord(HttpServletRequest request, Map<String, Object> map) {
PageOfficeCtrl poCtrl = new PageOfficeCtrl(request);
poCtrl.setServerPage("/poserver.zz");//设置服务页面
poCtrl.addCustomToolButton("保存", "Save", 1);//添加自定义保存按钮
poCtrl.setSaveFilePage("/save");//设置处理文件保存的请求方法
Workbook wordDoc = new Workbook();
poCtrl.setWriter(wordDoc);
//打开word
poCtrl.webOpen("D:\\备份\\531回归缺陷-费用字段.xlsx", OpenModeType.xlsNormalEdit, "admin");
map.put("pageoffice", poCtrl.getHtmlCode("PageOfficeCtrl1"));
ModelAndView mv = new ModelAndView("Word");
return mv;
}
@RequestMapping("/save")
public void saveFile(HttpServletRequest request, HttpServletResponse response) {
FileSaver fs = new FileSaver(request, response);
fs.saveToFile("d:\\lic\\关于xxxx初步设计的内审意见模板.docx");
fs.close();
}
@RequestMapping("/saveData")
public void saveData(HttpServletRequest request, HttpServletResponse response) {
//---- 关键代码开始 ----
com.zhuozhengsoft.pageoffice.wordreader.WordDocument doc = new com.zhuozhengsoft.pageoffice.wordreader.WordDocument(request, response);
doc.close();
//---- 关键代码结束 ----
}
/**
* 添加PageOffice的服务器端授权程序Servlet(必须)
*
* @return
*/
@Bean
public ServletRegistrationBean servletRegistrationBean() {
com.zhuozhengsoft.pageoffice.poserver.Server poserver = new com.zhuozhengsoft.pageoffice.poserver.Server();
poserver.setSysPath(poSysPath);//设置PageOffice注册成功后,license.lic文件存放的目录
ServletRegistrationBean srb = new ServletRegistrationBean(poserver);
srb.addUrlMappings("/poserver.zz");
srb.addUrlMappings("/posetup.exe");
srb.addUrlMappings("/pageoffice.js");
srb.addUrlMappings("/jquery.min.js");
srb.addUrlMappings("/pobstyle.css");
srb.addUrlMappings("/sealsetup.exe");
return srb;//
}
/**
* 添加印章管理程序Servlet(可选)
*
* @return
*/
@Bean
public ServletRegistrationBean servletRegistrationBean2() {
com.zhuozhengsoft.pageoffice.poserver.AdminSeal adminSeal = new com.zhuozhengsoft.pageoffice.poserver.AdminSeal();
adminSeal.setAdminPassword(poPassWord);//设置印章管理员admin的登录密码
adminSeal.setSysPath(poSysPath);//设置印章数据库文件poseal.db存放的目录
ServletRegistrationBean srb = new ServletRegistrationBean(adminSeal);
srb.addUrlMappings("/adminseal.zz");
srb.addUrlMappings("/sealimage.zz");
srb.addUrlMappings("/loginseal.zz");
return srb;//
}
}
4. 前端文件 ,需放在 resources 的templates 目录下
Index.html
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"
xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
<head>
<title>Index</title>
<script type="text/javascript" src="pageoffice.js"></script>
</head>
<body>
<h1 th:inline="text">PageOffice 集成效果演示</h1>
<a href="javascript:POBrowser.openWindowModeless('/word','width=1200px;height=800px;');">管理员 打开文件  </a>
 
<a href="javascript:POBrowser.openWindowModeless('/wordTwo','width=1200px;height=800px;');">李四 打开文件  </a>
 
<a href="javascript:POBrowser.openWindowModeless('/wordThree','width=1200px;height=800px;');">张三 打开文件  </a>
<a href="javascript:POBrowser.closeWindow();">关闭  </a>
</body>
</html>
Word.html
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"
xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
<head>
<title th:utext="${username}"></title>
<script type="text/javascript">
function Save() {
document.getElementById("PageOfficeCtrl1").WebSave();
}
</script>
<script type="text/javascript">
function AddSeal() {
try{
document.getElementById("PageOfficeCtrl1").ZoomSeal.AddSeal();
}catch (e){ };
}
</script>
</head>
<body>
<h1 th:inline="text" th:utext="${username}"> Hello.v.2</h1>
<div style="width:1000px;height:700px;" th:utext="${pageoffice}"></div>
</body>
</html>
效果展示,完全具备wps 所有功能
打开文件效果: