You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C# MVVM .NET项目:替换Interop DLL为Open XML以解决RDS服务器访问问题

Replacing Word Interop with Open XML for Your C# .NET MVVM Project

Great question—using Word Interop on RDS servers is notoriously tricky because it requires Microsoft Office to be installed, introduces security risks, and isn’t designed for server-side execution. Open XML is the ideal replacement here: it works directly with the .docx file format without relying on Office, making it perfect for RDS environments.

Here’s how to refactor your code step by step:

Step 1: Install Required NuGet Packages

First, add these packages to your project (via NuGet Package Manager or CLI):

  • DocumentFormat.OpenXml: Microsoft’s official SDK for working with Open XML formats
  • HtmlToOpenXml: A reliable helper library to convert HTML content to Open XML elements (since the MailItem’s formatted content is easiest to access as HTML)

Step 2: Replace Interop Code with Open XML

Your original code uses the Word Editor to save the mail content as a .docx. We’ll instead pull the mail’s HTML body, convert it to an Open XML document, and save it directly.

Original Interop Code

Microsoft.Office.Interop.Word.Document olDoc = _item.MailItem.GetInspector.WordEditor as Microsoft.Office.Interop.Word.Document;
tempAppFileName = tempFilePath + @"""" + _item.emailIdentifier.ToString() + ".docx";
olDoc.SaveAs2(tempAppFileName);

Refactored Open XML Code

using System.IO;
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using HtmlToOpenXml;

// Get the formatted HTML content from the MailItem
string mailHtmlContent = _item.MailItem.HTMLBody;

// Clean up the file path (avoid extra quotes and use proper path combining)
tempAppFileName = Path.Combine(tempFilePath, $"{_item.emailIdentifier}.docx");

// Create and populate the .docx file using Open XML
using (WordprocessingDocument wordDocument = WordprocessingDocument.Create(tempAppFileName, WordprocessingDocumentType.Document))
{
    // Initialize the main document structure
    MainDocumentPart mainPart = wordDocument.AddMainDocumentPart();
    mainPart.Document = new Document();
    Body documentBody = mainPart.Document.AppendChild(new Body());

    // Convert HTML to Open XML elements and add to the document
    HtmlConverter converter = new HtmlConverter(mainPart);
    converter.ParseHtml(mailHtmlContent);

    // Save and close the document
    mainPart.Document.Save();
}

Key Explanations

  • No Office Dependency: Open XML works directly with the .docx file structure, so you don’t need Word installed on your RDS server.
  • HTML to Open XML Conversion: The HtmlToOpenXml library handles translating HTML tags (like <p>, <b>, <i>) into their corresponding Open XML elements, preserving most formatting from the original mail.
  • Proper Resource Management: The using statement ensures the document is properly disposed, preventing file locks or memory leaks—critical for server-side code.

Edge Cases to Consider

  1. Plain Text Mails: If your MailItem uses plain text instead of HTML, replace the HTML conversion step with a simple paragraph:
    documentBody.AppendChild(new Paragraph(new Run(new Text(_item.MailItem.Body))));
    
  2. Inline Images: The HTML body uses cid: URLs for inline images, which the converter won’t automatically resolve. To include inline images, you’ll need to extract them from the MailItem’s Attachments collection (where IsInline = true), save them temporarily, update the HTML’s src attributes, and then let the converter embed them.

内容的提问来源于stack exchange,提问作者Vineeta

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.09 14:38:13