随笔 - 26  文章 - 5  评论 - 4  阅读 - 23021

用pageOffice控件实现 office 文档在线编辑Word 打开文档后在页面里触发事件

OA办公中,业务需要编辑打开word文档后 执行一些js操作

怎么实现编辑打开word文档后 执行一些js操作呢?

2 实现方法

通过pageOffice实现简单的在线打开编辑word时,
通过设置
关键代码:
// 设置文件打开后执行的js function
poCtrl.setJsFunction_AfterDocumentOpened("AfterDocumentOpened()");

就可以实现编辑打开word文档后 执行一些js操作。

3 实现过程

以java的springboot框架为例

1 集成pageOffice

https://www.zhuozhengsoft.com/dowm/

image
从pageOffice官网
下载页面,找到springboot的集成示例,按照里面的集成明说,可以集成到自己的springboot项目中。

2 在线打开编辑word

image

可以按照这个示例首先实现最基本的打开word的方法。

3 通过代码打开word文档后 执行一些js操作

示例参考
image

java代码

点击查看代码
@RestController
@RequestMapping(value = "/AfterDocOpened")
public class AfterDocOpenedController {

    @RequestMapping(value = "/Word", method = RequestMethod.GET)
    public ModelAndView showWord(HttpServletRequest request, Map<String, Object> map) {
        PageOfficeCtrl poCtrl = new PageOfficeCtrl(request);
        poCtrl.setServerPage(request.getContextPath() + "/poserver.zz");//设置服务页面
        // 设置文件打开后执行的js function
        poCtrl.setJsFunction_AfterDocumentOpened("AfterDocumentOpened()");
        //打开Word文档
        poCtrl.webOpen("/doc/AfterDocOpened/test.doc", OpenModeType.docNormalEdit, "张三");
        map.put("pageoffice", poCtrl.getHtmlCode("PageOfficeCtrl1"));
        ModelAndView mv = new ModelAndView("AfterDocOpened/Word");
        return mv;
    }

}

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>文件打开后触发的事件</title>

</head>
<body>
<script type="text/javascript">
    function AfterDocumentOpened() {
        // 打开文件的时候,给word中当前光标位置赋值一个文本值
        document.getElementById("PageOfficeCtrl1").Document.Application.Selection.Range.Text = "文件打开了";
    }

</script>
<form id="form1">
    Word中的"<span style=" color:Red;"> 文件打开了</span>" 是在文档打开的事件中用程序添加进去的。<br/><br/>

    <div style=" width:auto; height:700px;" th:utext="${pageoffice}">
    </div>
</form>
</body>

</html>

通过以上代码,可以实现 office 文档在线编辑Word 打开之后在页面里触发的事件

4效果图

image

文档打开后,显示的 文件打开了 是通过AfterDocumentOpened的方法写到文档中的。

5总结

用pageOffice控件实现 office 文档在线编辑Word 打开之后在页面里触发的事件

posted on   Tony636  阅读(279)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示