简体   繁体   English

如何使用 Office.js 将后端返回的字节数组响应转换为 word 文档?

[英]How to convert byte array response returned from backend to word document using Office.js?

I am using compareDocuments function to compare two documents and returning compared document as a byte array as response to front end, I don't know how to convert this byte array and open it as word document on my desktop word after calling my API on taskpane Add-in without downloading the file to my system.我正在使用 compareDocuments function 来比较两个文档并将比较的文档作为字节数组返回作为对前端的响应,我不知道如何转换这个字节数组并在我的桌面 word 上调用我的 API 后将其作为 word 文档在任务面板上打开在不将文件下载到我的系统的情况下加载项。 I want two show the compared doc on word Desktop .我想要两个在 word Desktop上显示比较文档。

Below is my C# code for the API which I created: -下面是我创建的 API 的 C# 代码:-

[HttpPost]
        [Route("compare")]
        public async Task<object> compare(string original, string revised)
        {
            Word.Application wordApp = new Word.Application();
            wordApp.Visible = false;
            object wordTrue = (object)true;
            object wordFalse = (object)false;
            object fileToOpen = @original;
            object missing = Type.Missing;
            Word.Document doc1 = wordApp.Documents.Open(ref fileToOpen,
                   ref missing, ref wordFalse, ref wordFalse, ref missing,
                   ref missing, ref missing, ref missing, ref missing,
                   ref missing, ref missing, ref wordTrue, ref missing,
                   ref missing, ref missing, ref missing);

            object fileToOpen1 = @revised;
            Word.Document doc2 = wordApp.Documents.Open(ref fileToOpen1,
                   ref missing, ref wordFalse, ref wordFalse, ref missing,
                   ref missing, ref missing, ref missing, ref missing,
                   ref missing, ref missing, ref missing, ref missing,
            ref missing, ref missing, ref missing);

            Word.Document doc = wordApp.CompareDocuments(doc1, doc2, Word.WdCompareDestination.wdCompareDestinationNew,
                                Word.WdGranularity.wdGranularityWordLevel,
                                true, true, true, true, true, true, true, true, true, true, "", true);

            doc1.Close(ref missing, ref missing, ref missing);
            doc2.Close(ref missing, ref missing, ref missing);

            // Hides both original and revised documents
            wordApp.ActiveWindow.ShowSourceDocuments = WdShowSourceDocuments.wdShowSourceDocumentsNone;

            wordApp.Visible = false;
            //doc.Activate();
            object filePath = @"C:\Users\yatin\OneDrive\Documents\compared_document.docx";
            doc.SaveAs(ref filePath);
            doc.Close();
            wordApp.Quit();

            byte[] byteArray;
            using (FileStream fs = new FileStream(filePath.ToString(), FileMode.Open, FileAccess.Read))
            {
                byteArray = new byte[fs.Length];
                fs.Read(byteArray, 0, (int)fs.Length);
            }

            
            // Return the byte array to the frontend
            return byteArray;

        }

I want to use this API with my frontend ADD-IN and show the compared document on the frontend device without downloading it, everytime I run my code the compared document opens up on server side,so I tried converting the compared doc to byte array and send it as a response to frontend, but I cannot open the word doc on desktop in frontend.我想将这个 API 与我的前端 ADD-IN 一起使用,并在不下载的情况下在前端设备上显示比较文档,每次我运行我的代码时,比较文档都会在服务器端打开,所以我尝试将比较文档转换为字节数组并将其作为对前端的响应发送,但我无法在前端的桌面上打开 word doc。 Is there any way we can solve this issue?有什么办法可以解决这个问题吗?

First of all, automating MS Word from any web server is not really a good idea.首先,从任何 web 服务器自动化 MS Word 并不是一个好主意。

Microsoft does not currently recommend, and does not support, Automation of Microsoft Office applications from any unattended, non-interactive client application or component (including ASP, ASP.NET, DCOM, and NT Services), because Office may exhibit unstable behavior and/or deadlock when Office is run in this environment. Microsoft 目前不建议也不支持来自任何无人值守、非交互式客户端应用程序或组件(包括 ASP、ASP.NET、DCOM 和 NT 服务)的 Microsoft Office 应用程序自动化,因为 Office 可能表现出不稳定的行为和/或死锁当 Office 在此环境中运行时。

If you are building a solution that runs in a server-side context, you should try to use components that have been made safe for unattended execution.如果您正在构建一个在服务器端上下文中运行的解决方案,您应该尝试使用已被安全用于无人值守执行的组件。 Or, you should try to find alternatives that allow at least part of the code to run client-side.或者,您应该尝试找到至少允许部分代码在客户端运行的替代方案。 If you use an Office application from a server-side solution, the application will lack many of the necessary capabilities to run successfully.如果您使用来自服务器端解决方案的 Office 应用程序,该应用程序将缺少许多成功运行所必需的功能。 Additionally, you will be taking risks with the stability of your overall solution.此外,您将承担整体解决方案稳定性的风险。 Read more about that in the Considerations for server-side Automation of Office article.服务器端自动化 Office 文章的注意事项中阅读更多相关信息。

It seems you need to received the byte array and then save it as a file on the client side, see How to load Word document from byte array for more information.看来您需要接收字节数组,然后将其保存为客户端的文件,请参阅如何从字节数组加载 Word 文档以获取更多信息。 But I don't think any JS code can access the local filesystem for security reasons.但出于安全原因,我认为任何 JS 代码都不能访问本地文件系统。 The best what your application could do is to ask users to download the generated file.您的应用程序最多只能要求用户下载生成的文件。 Or just open the file in Office365 editor prior uploading the generated file to the OneDrive server.或者在将生成的文件上传到 OneDrive 服务器之前,只需在 Office365 编辑器中打开文件。

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

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