首页 > 其他分享 >PageOffice调用本地office实现多人在线同时编辑Word文档

PageOffice调用本地office实现多人在线同时编辑Word文档

时间:2023-04-21 12:11:49浏览次数:44  
标签:userName Word office doc pageoffice 文档 import PageOffice

说明:多人同时在线编辑文件大多数会出现文档相互覆盖的问题,后保存的文档会覆盖先保存的文档。pageoffice的这个功能可以用程序控制不同用户打开文件后,编辑Word文档中属于自己的区域,并且不会互相影响。

1、环境:

前端:vue

后端:springboot、pageoffice5.4.0.3版本

vue+springboot集成pageoffice的具体步骤参考:Vue+springboot集成PageOffice实现在线编辑Word、excel文档

2、前端

在index.vue页面定义不同用户打开文件的按钮,调用pageoffice的POBrowser打开文件

<template>
	<div class="Word">
		<div style="text-align: center;">
            <br/><br/>
            <h1 style="color: rgb(114, 43, 207);">不同的用户在文档中可以编辑的区域不同</h1>
            <input type="button" @click="openWord('zhangsan')" value="张三打开文件"/><br/><br/>
            <input type="button" @click="openWord('lisi')" value="李四打开文件"/><br/>
        </div>
	</div>
</template>
 
<script>
	const axios=require('axios');
	  export default{
	    name: 'Word',
	    data(){
	      return {
	      }
	    },
	    methods:{
		  openWord(userId){
            POBrowser.openWindowModeless('word?userId='+userId , 'width=1200px;height=800px;');
		  }
	    }
	}
</script>

image

Word.vue页面通过axios请求后台获取pageoffice控件在页面解析

<template>
	<div class="Word">
		<div class="flow4">
			<input type="button" @click="exit()" value="退出" />
			<strong>当前用户:</strong>
			<span style="color: Red;">{{user}}</span>
		</div>
		<div style="height: 800px; width: auto" v-html="poHtmlCode"/>
	</div>
</template>
 
<script>
	const axios=require('axios');
	  export default{
	    name: 'Word',
	    data(){
	      return {
			poHtmlCode:'',
			user:'',
	      }
	    },
		created: function(){
			this.user = this.$route.query.userId;
			axios.post("/api/SetDrByUserWord2/Word?user="+this.user).then((response) => {
				this.poHtmlCode = response.data;
			  }).catch(function (err) {
				console.log(err)
			  })
	    },
	    methods:{
		  exit(){
			window.external.close();
		  },
		  Save() {
		  	document.getElementById("PageOfficeCtrl1").WebSave();
		  },
		  //全屏/还原
		  IsFullScreen() {
		  	document.getElementById("PageOfficeCtrl1").FullScreen = !document.getElementById("PageOfficeCtrl1").FullScreen;
		  }
	    },
	    mounted: function(){
	      // 将vue中的方法赋值给window
		  window.Save = this.Save;
		  window.IsFullScreen = this.IsFullScreen;
	    }
	}
</script>

3、后端

后端打开test.doc文件,这个文件相当于一个模板,张三和李四两个用户分别编辑的区域是两个单独的文件,在打开文件的时候将content这两个文件插入到test.doc中。当任何一个用户保存时,只单独保存他编辑的文件,其他用户的区域不动。这样就达到了同时编辑的效果,并且互不影响。主要是用了pageoffice的拆分文档和合并文档的功能

package com.zhuozhengsoft.samplesspringbootback.controller;
 
import com.zhuozhengsoft.pageoffice.OpenModeType;
import com.zhuozhengsoft.pageoffice.PageOfficeCtrl;
import com.zhuozhengsoft.pageoffice.wordwriter.DataRegion;
import com.zhuozhengsoft.pageoffice.wordwriter.WordDocument;
import com.zhuozhengsoft.samplesspringbootback.util.GetDirPathUtil;
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.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
 
@RestController
@RequestMapping(value = "/SetDrByUserWord2/")
public class SetDrByUserWord2Controller {
 
    //获取doc目录的磁盘路径
    private String dir = GetDirPathUtil.getDirPath() + "static/doc/";
 
    @RequestMapping(value = "Word")
    public String showWord(HttpServletRequest request) {
        String userName = request.getParameter("user");
        //***************************卓正PageOffice组件的使用********************************
        WordDocument doc = new WordDocument();
        //打开数据区域
        DataRegion d1 = doc.openDataRegion("PO_com1");
        DataRegion d2 = doc.openDataRegion("PO_com2");
 
        //给数据区域赋值
        d1.setValue("[word]/api/doc/SetDrByUserWord2/content1.doc[/word]");
        d2.setValue("[word]/api/doc/SetDrByUserWord2/content2.doc[/word]");
 
        //根据登录用户名设置数据区域可编辑性
        //甲客户:zhangsan登录后
        if (userName.equals("zhangsan")) {
            d1.setEditing(true);
            //若要将数据区域内容存入文件中,则必须设置属性“setSubmitAsFile”值为true
            d1.setSubmitAsFile(true);
            d2.setEditing(false);
        }
        //乙客户:lisi登录后
        else if(userName.equals("lisi")) {
            d2.setEditing(true);
            d2.setSubmitAsFile(true);
            d1.setEditing(false);
        }
 
        PageOfficeCtrl poCtrl = new PageOfficeCtrl(request);
        poCtrl.setWriter(doc);
        //添加自定义按钮
        poCtrl.addCustomToolButton("保存", "Save", 1);
        poCtrl.addCustomToolButton("全屏/还原", "IsFullScreen", 4);
        //设置服务器页面
        poCtrl.setServerPage("/api/poserver.zz"); //此行必须
        //设置保存方法
        poCtrl.setSaveDataPage("/api/SetDrByUserWord2/save?userName=" + userName);
        //打开Word文档,文档地址可以是磁盘路径也可以是url
        poCtrl.webOpen("/api/doc/SetDrByUserWord2/test.doc", OpenModeType.docSubmitForm, userName);
        return poCtrl.getHtmlCode("PageOfficeCtrl1");
    }
 
 
    @RequestMapping("save")
    public void save(HttpServletRequest request, HttpServletResponse response) throws IOException {
        com.zhuozhengsoft.pageoffice.wordreader.WordDocument doc = new com.zhuozhengsoft.pageoffice.wordreader.WordDocument(request, response);
        byte[] bytes = null;
        String filePath = "";
        if (request.getParameter("userName") != null && request.getParameter("userName").trim().equalsIgnoreCase("zhangsan")) {
            bytes = doc.openDataRegion("PO_com1").getFileBytes();
            filePath = "content1.doc";
        } else if(request.getParameter("userName") != null && request.getParameter("userName").trim().equalsIgnoreCase("lisi")) {
            bytes = doc.openDataRegion("PO_com2").getFileBytes();
            filePath = "content2.doc";
        }
        doc.close();

        filePath = dir + "SetDrByUserWord2/" + filePath;
        FileOutputStream outputStream = new FileOutputStream(filePath);
        outputStream.write(bytes);
        outputStream.flush();
        outputStream.close();
    }

}

4、最后效果

image

转载 https://blog.csdn.net/qq_44306545/article/details/128237354

标签:userName,Word,office,doc,pageoffice,文档,import,PageOffice
From: https://www.cnblogs.com/qq742655/p/17339910.html

相关文章

  • SiteFactory 实现ctrl+v粘贴图片并上传、word粘贴带图片
    ​ 自动导入Word图片,或者粘贴Word内容时自动上传所有的图片,并且最终保留Word样式,这应该是Web编辑器里面最基本的一个需求功能了。一般情况下我们将Word内容粘贴到Web编辑器(富文本编辑器)中时,编辑器都无法自动上传图片。需要用户手动一张张上传Word图片。如果只有一张图片还能够接......
  • 手把手教你用Python操纵Word自动编写离职报告
    今日鸡汤不堪玄鬓影,来对白头吟。前言大家好,我是吴老板,今天给大家分享一篇Python自动化文章。Python是自动化处理的一把好手,比如自动编写离职申请!!!安装pipinstallpython-docx-ihttps://pypi.douban.com/simple/基本用法创建一个docx文档fromdocximportDocumentfromdocx.enu......
  • Linux环境aspose插件word转pdf中文乱码解决方案
    本地没出现这个问题,到了linux环境出现了这个问题。第一想到的是字体。 解决方案1:环境解决安装字库,将win机器的c:\windows\fonts目录下的全部文件拷贝到生产服务器字体安装目录下,然后执行以下命令更新字体缓存。查看linux目前的所有字体fc-list查看Linux目前的所有中文......
  • 根据EXCEL数据自动生成WORD文档
    如何通过excel批量生成word文件?大家应该都有批量生成word文件的工作经历。相信有很多小伙伴都遇到过,一个excel表格里有一批明细数据,然后需要一次性把这些明细数据生成对应的word文件,这项工作如果手动做,一个一个的创建word文件,那工作效率会非常的低。有没有高效快捷的办法批量生成......
  • 如何将带格式的代码复制到Word文档中
    step1:使用UE(文本编辑器软件)打开你的代码,并在右下方的查看方式,选好代码的类型格式。step2:选中需要copy的代码(建议使用列模式来选中,copy时可以背景颜色也copy过去),在主页下点击复制旁边的倒三角,选择特殊复制,选择复制为HTML文档。 step3:粘贴到word文档中,结果如下图。step4:在wor......
  • gotenberg+ chromiumly + pdf.js 进行office 文档转换以及预览处理
    日常中office预览是一个比较常见的问题,基于微软的officeonline是一个选择,但是移动端效果不是很好就有pdf以及一些基于生成图片的方案也是不错的,以下是基于gotenberg+chromiumly的一个尝试简单说明gotenberg是基于golang开发的包装了Chromium以及LibreOffice的基于api......
  • 华为防火墙 修改密码 过期时间的命令 manager-user password valid-days 0
    ......
  • 【MathType】word2016数学公式编号
    问题毕业论文排版中,对数学公式需要类似(3-1)的格式。解决技巧在写论文初稿的时候,先不要于公式的编号,先给它编一个号,比如(3)(2)(4)的。最后写完了以后,再再添加section,同意修改编号格式为(章-公式序号)可以先修改mathtype章节的样式为不隐藏,这样方便添加。添加完math......
  • 博客 实现ctrl+v粘贴图片并上传、word粘贴带图片
    ​ 图片的复制无非有两种方法,一种是图片直接上传到服务器,另外一种转换成二进制流的base64码目前限chrome浏览器使用首先以um-editor的二进制流保存为例:打开umeditor.js,找到UM.plugins['autoupload'],然后找到autoUploadHandler方法,注释掉其中的代码。加入下面的代码://判断剪......
  • Blog 实现ctrl+v粘贴图片并上传、word粘贴带图片
    ​ 这种方法是servlet,编写好在web.xml里配置servlet-class和servlet-mapping即可使用后台(服务端)java服务代码:(上传至ROOT/lqxcPics文件夹下)<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%><%@     page contentType="text/html;cha......