【问题标题】:How To: Open a Word document from .NET Core web application, edit, and save back to server?如何:从 .NET Core Web 应用程序打开 Word 文档、编辑并保存回服务器?
【发布时间】:2021-08-03 17:05:14
【问题描述】:

我正在寻找有关如何在 ASP/C# (.NET Core) Web 应用程序中创建类似于 SharePoint 的 Microsoft Word 集成的现代示例。换句话说,我的目标是从我的网页(存储在我的本地服务器上的文件)单击 Word 文档,在 Word(桌面应用程序)中打开,进行更改,然后保存回服务器。当我说类似 SharePoint 的集成时,我的意思是,打开和保存是自动处理的

我发现其他人也问过同样的问题,但没有具体的回答,而且大多数人已经有近十年的历史了。以下是我找到但没有帮助的一些文章:

看起来 Office 加载项平台 (https://docs.microsoft.com/en-us/office/dev/add-ins/overview/office-add-ins#components-of-an-office-add-in) 可能有用,但我不一定希望将 Word 加载项添加到功能区。所以不确定这是否真的是我想要的。

WebDAV 可能是我在此处讨论的 (https://www.webdavsystem.com/ajaxfilebrowser/programming/opening-docs/open_save_docs_directly_to_server/) 所需要的,但不确定...而且这篇文章谈论的是 Office 2007,所以似乎有点过时了。

如果有任何帮助可以指导我参考讨论当前解决此问题的方法的示例、文章或论坛,我们将不胜感激。

更多信息:客户将在 Windows 10 机器上使用 Edge 或 Chrome 浏览器,以及 Office 2016(或更高版本)。

【问题讨论】:

  • 这对于单个问题可能有点宽泛。您是否有不想使用附加路由的特殊原因?还是直接使用Sharepoint/O365?
  • @DSMTurboAWD,该应用程序将无法访问我们企业的 O365 环境,我们的计划是利用 SharePoint 之外的文件存储库,因为我们预计会有大量(和大型)文件。我们的开发人员不喜欢 SharePoint 在 SQL Server 中存储文件的方式。
  • 老实说,我会使用 Aspose.Words products.aspose.com/words/net 之类的东西。它几乎有你想要的东西。它是一个 NuGet 包,可让您控制文档呈现、保存甚至保存回数据库文件存储的方式。

标签: c# asp.net-core ms-word webdav


【解决方案1】:

这个问题非常广泛。看起来有几个部分。

  1. 可配置为与 Web 服务通信的插件
  2. 使用插件数据的 Api
  3. 插件应该可以自动保存

这里是一些相关文档,虽然有些是 Excel 特有的,但可能可以重新用于 Word

另请注意,Office 插件开发是 JavaScript 和/或 TypeScript。

https://docs.microsoft.com/en-us/office/dev/add-ins/excel/custom-functions-web-reqs

https://docs.microsoft.com/en-us/office/dev/add-ins/excel/custom-functions-batching

https://docs.microsoft.com/en-us/javascript/api/word/word.document?view=word-js-preview#save__

这里没有任何代码,所以即使编写一个特定的函数也会做出几个假设,但是从文档中(带有几个插件):

    // Run a batch operation against the Word object model.
    Word.run(function (context) {
    
    // Create a proxy object for the document.
    var thisDocument = context.document;

    // Queue a command to load the document save state (on the saved property).
    context.load(thisDocument, 'saved');    
    
    // Synchronize the document state by executing the queued commands, 
    // and return a promise to indicate task completion.
    return context.sync().then(function () {
        
        if (thisDocument.saved === false) {
            // Queue a command to save this document.
            thisDocument.save();
            
            // Synchronize the document state by executing the queued commands, 
            // and return a promise to indicate task completion.
            return context.sync().then(function () {
                // 
                // CODE to send your data to your server(s) via
                // API (post request, or something similar)
                //
                console.log('Saved the document');
            });
        } else {
            console.log('The document has not changed since the last save.');
        }
    });  
})
.catch(function (error) {
    console.log("Error: " + JSON.stringify(error));
    if (error instanceof OfficeExtension.Error) {
        console.log("Debug info: " + JSON.stringify(error.debugInfo));
    }
});

【讨论】:

  • 感谢您抽出宝贵时间回答我的问题。这给了我一个很好的起点来继续我的阅读/研究!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-03-06
  • 1970-01-01
  • 2011-04-13
  • 1970-01-01
  • 1970-01-01
  • 2014-01-01
  • 1970-01-01
相关资源
最近更新 更多