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 formatsHtmlToOpenXml: 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
HtmlToOpenXmllibrary 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
usingstatement ensures the document is properly disposed, preventing file locks or memory leaks—critical for server-side code.
Edge Cases to Consider
- 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)))); - 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’sAttachmentscollection (whereIsInline = true), save them temporarily, update the HTML’ssrcattributes, and then let the converter embed them.
内容的提问来源于stack exchange,提问作者Vineeta
相关产品推荐
相关产品推荐

