简体   繁体   English

Office Web Apps文字编辑

[英]Office Web Apps Word Editing

The idea is to build a proprietary Java back end document system using Office Web Apps. 这个想法是使用Office Web Apps构建一个专有的Java后端文档系统。

We have created the WOPI client which allows us to view/edit PowerPoint and Excel web app documents but we can only view Word Documents. 我们已经创建了WOPI客户端,它允许我们查看/编辑PowerPoint和Excel Web应用程序文档,但是我们只能查看Word文档。

In order to edit Word Web App documents you need to implement MS-FSSHTTP. 为了编辑Word Web App文档,您需要实现MS-FSSHTTP。

It appears there is no information about how to actually do this in code. 似乎没有有关如何在代码中实际执行此操作的信息。 Has anyone performed this or would know how? 有没有人执行过此操作或知道如何执行此操作?

recently my team and I have implemented a WOPI-Host that supports viewing and editing of Word, PPT and Excel documents. 最近,我和我的团队实现了一个WOPI-Host,它支持查看和编辑Word,PPT和Excel文档。 You can take a look at https://github.com/marx-yu/WopiHost which is a command prompt project that listens on the 8080 port and enables editing and viewing of word documents though the Microsoft Office Web Apps. 您可以查看https://github.com/marx-yu/WopiHost ,它是一个命令提示符项目,它侦听8080端口并允许通过Microsoft Office Web Apps编辑和查看Word文档。

We have implemented this solution in a webApi and it works great. 我们已经在webApi中实现了该解决方案,并且效果很好。 Hope this sample project will help you out. 希望这个示例项目可以为您提供帮助。

After requested, I will try and add code samples to clarify the way to implement it based on my webApi implementation, but their is a lot of code to implement to actually make it work properly. 在请求之后,我将尝试添加代码示例,以阐明基于我的webApi实现的实现方式,但是要实现它们才能正常工作,需要大量代码。

First things first, to enabled editing you will need to capture Http Posts in a FilesController. 首先,要启用编辑,您将需要在FilesController中捕获Http Post。 Each posts that concern the actual editing will have the header X-WOPI-Override equal to COBALT . 每个涉及实际编辑的帖子都将具有等于COBALT的标题X-WOPI-Override In these post you will find out that the InputStream is and Atom type. 在这些文章中,您将发现InputStream是and Atom类型。 Based on the MS-WOPI documentation, in your response you will need to include the following headers X-WOPI-CorrelationID and request-id . 根据MS-WOPI文档,您需要在响应中包括以下标头X-WOPI-CorrelationIDrequest-id

Here is the code of my webApi post method (it is not complete since I'm still implementing that WOPI protocol). 这是我的webApi post方法的代码(由于我仍在实现该WOPI协议,因此尚不完整)。

string wopiOverride = Request.Headers.GetValues("X-WOPI-Override").First();
if (wopiOverride.Equals("COBALT"))
{
   string filename = name;
   EditSession editSession = CobaltSessionManager.Instance.GetSession(filename);
   var filePath = HostingEnvironment.MapPath("~/App_Data/");
   if (editSession == null){
      var fileExt = filename.Substring(filename.LastIndexOf('.') + 1);
      if (fileExt.ToLower().Equals(@"xlsx"))
         editSession = new FileSession(filename, filePath + "/" + filename, @"yonggui.yu", @"yuyg", @"yonggui.yu@emacle.com", false);
      else
         editSession = new CobaltSession(filename, filePath + "/" + filename, @"patrick.racicot", @"Patrick Racicot", @"patrick.racicot@hospitalis.com", false);
         CobaltSessionManager.Instance.AddSession(editSession);
      }

      //cobalt, for docx and pptx
      var ms = new MemoryStream();

      HttpContext.Current.Request.InputStream.CopyTo(ms);
      AtomFromByteArray atomRequest = new AtomFromByteArray(ms.ToArray());
      RequestBatch requestBatch = new RequestBatch();

      Object ctx;
      ProtocolVersion protocolVersion;

      requestBatch.DeserializeInputFromProtocol(atomRequest, out ctx, out protocolVersion);
      editSession.ExecuteRequestBatch(requestBatch);


      foreach (Request request in requestBatch.Requests)
      {
         if (request.GetType() == typeof(PutChangesRequest) && request.PartitionId == FilePartitionId.Content)
         {
             //upload file to hdfs
             editSession.Save();
         }
      }
      var responseContent = requestBatch.SerializeOutputToProtocol(protocolVersion);
      var host = Request.Headers.GetValues("Host");
      var correlationID = Request.Headers.GetValues("X-WOPI-CorrelationID").First();

      response.Headers.Add("X-WOPI-CorrelationID", correlationID);
      response.Headers.Add("request-id", correlationID);
      MemoryStream memoryStream = new MemoryStream();

      var streamContent = new PushStreamContent((outputStream, httpContext, transportContent) =>
      {
         responseContent.CopyTo(outputStream);
         outputStream.Close();
      });

      response.Content = streamContent;
      response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
      response.Content.Headers.ContentLength = responseContent.Length;
}

As you can see in this method I make use of CobaltSessionManager and CobaltSession which are used to create and manage editing sessions on the Cobalt protocol. 如您所见,我利用CobaltSessionManagerCobaltSession来创建和管理基于Cobalt协议的编辑会话。 You will also need a what I call CobaltHostLockingStore which is used to handle the different requests when communicating with the Office Web App server in the edition initialization. 您还将需要一个我称为CobaltHostLockingStore的文件,该文件用于在版本初始化中与Office Web App服务器进行通信时处理不同的请求。

I won't be posting the code for these 3 classes since they are already coded in the sample github project I posted and that they are fairly simple to understand even though they are big. 我不会发布这3个类的代码,因为它们已经在我发布的示例github项目中进行了编码,即使它们很大,它们也很容易理解。

If you have more questions or if it's not clear enough don't hesitate to comment and I will update my post accordingly. 如果您还有其他问题或不清楚,请随时发表评论,我将相应地更新我的帖子。

Patrick Racicot, provided great answer. 帕特里克·拉西科特(Patrick Racicot)提供了很好的答案。 But i had problem saving docx(exception in CobaltCore.dll), and i even started using dotPeak reflector trying to figure it out. 但是我在保存docx时遇到了问题(CobaltCore.dll中的例外),我什至开始使用dotPeak反射器试图弄清楚它。

But after i locked editSession variable in my WebApi method everything started working like magic. 但是,当我在WebApi方法中锁定了editSession变量后,一切开始如魔术般工作。 It seems that OWA is sending requests that should be handled as a chain, not in parallel as usually controller method acts. 似乎OWA正在发送应作为链处理的请求,而不是像通常的控制器方法那样并行处理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM