如何在.NET Core Web应用程序中打开Word文档,编辑并保存回服务器?

3
我正在寻找现代示例,演示如何在ASP/C# (.NET Core) Web应用程序中创建类似SharePoint的与Microsoft Word的集成。换句话说,我的目标是从我的网页上单击一个Word文档(文件存储在我的本地服务器上),在Word(桌面应用程序)中打开它,进行更改,并保存回服务器。当我说SharePoint-like集成时,我的意思是,打开和保存都会自动处理而不需要用户费心将文件本地保存并手动上传回服务器。
我发现其他人也在问同样的问题,但没有具体的答案,大多数都已经接近十年了。以下是我发现的一些文章,但对我没有帮助: 看起来 Office Add-in 平台可能有用(https://learn.microsoft.com/en-us/office/dev/add-ins/overview/office-add-ins#components-of-an-office-add-in),但我不一定想要将一个 Word Add-in 添加到功能区。所以不确定这是否真的是我想要的。
WebDAV( https://www.webdavsystem.com/ajaxfilebrowser/programming/opening-docs/open_save_docs_directly_to_server/) 可能是我需要的,但不确定...而且这篇文章谈到了 Office 2007,似乎有些过时了。
任何帮助引导我示例、文章或论坛,讨论当前解决此问题的方法,都将非常感激。
更多信息:客户端将使用 Edge 或 Chrome 浏览器,在 Windows 10 上运行,使用 Office 2016 (或更高版本)。

2
这可能对于一个单一的问题来说有点宽泛。你不想使用Add-On路线的特定原因是什么?或直接使用Sharepoint/O365? - DSMTurboAWD
@DSMTurboAWD,该应用程序将无法访问我们企业的O365环境,我们计划利用SharePoint之外的文件存储库,因为我们预计会有大量(且较大的)文件。我们的开发人员不喜欢SharePoint将文件存储在SQL Server中的方式。 - Tooky
说实话,我会使用类似 Aspose.Words 的工具(https://products.aspose.com/words/net/)。它几乎拥有你所需要的所有功能。这是一个 NuGet 包,可以让你控制文档的渲染方式、保存方式,甚至可以将其保存回数据库文件存储中。 - Gwasshoppa
1个回答

0

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

  1. 一个可以配置与Web服务通信的插件
  2. 一个从插件消费数据的API
  3. 插件应该能够自动保存

以下是一些相关文档,尽管其中一些是针对Excel的,但可能可以为Word重新定制:

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

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

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

https://learn.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));
    }
});

非常感谢您抽出时间回答我的问题。这为我提供了一个很好的起点,让我能够继续阅读和研究! - Tooky

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接