问题描述
我一直在尝试用C#构建的Outlook 2010插件,使用Visual Studio 2008中,我能够获得 Microsoft.Office.Interop.Word.DocumentClass
从 Microsoft.Office.Interop.Outlook.Inspector
对象,它代表电子邮件对象当前正编辑。我明白,这的DocumentClass
其他各种环境中使用过多(一般为Microsoft Word扩展,毫不奇怪)。
I've been experimenting with building an Outlook 2010 addin in C#, using Visual Studio 2008. I am able to obtain a Microsoft.Office.Interop.Word.DocumentClass
object from a Microsoft.Office.Interop.Outlook.Inspector
object, which represents the e-mail being currently edited. I understand that this DocumentClass
is used in various other contexts too (usually for Microsoft Word extensions, unsurprisingly).
那类有益有一个叫做物业 WordOpenXML
,这似乎是构成所有文件的XML表示的 .DOCX
套餐,保存此Word文档另存为 .DOCX
。
That class helpfully has a property called WordOpenXML
, which seems to be an XML representation of all the files constituting the the .docx
package which would be saved to disk when saving this Word document as a .docx
.
一个该属性的方式时,可以保存到磁盘将是非常有用的,如果它可以转换成内存中的 System.IO.Packaging.Package
对象; IE浏览器。反序列化。有谁知道是否有一个简单的方法来做到这一点,或者我需要写一些XML解析代码做手工?
One way this property would be very useful is if it could be converted into a System.IO.Packaging.Package
object in memory; ie. deserialize it. Does anyone know whether there is an easy way to do this, or do I need to write some XML parsing code to do it manually?
推荐答案
我最终修改一些代码,我发现在网上创建一个 WordOpenXML
转换成一个包,这是保存到磁盘的方法:
I ended up modifying some code I found online to create a method which converts WordOpenXML
to a Package, which is saved to disk:
using System;
using System.Xml;
using System.Xml.XPath;
using System.Text;
using System.IO;
using System.IO.Packaging;
// [...]
// Call like this: createPackageFromWordOpenXML(wordEditor.WordOpenXML, @"C:\outputFiles\testOut.docx");
/// <summary>
/// Creates a ZIP package (ie. Word's .docx format) from a WordOpenXML string, which is saved to the file at the path specified.
/// </summary>
/// <param name="wordOpenXML">The WordOpenXML string to get the ZIP package data from.</param>
/// <param name="filePath">The path of the file to save the ZIP package to.</param>
private void createPackageFromWordOpenXML(string wordOpenXML, string filePath)
{
string packageXmlns = "http://schemas.microsoft.com/office/2006/xmlPackage";
Package newPkg = System.IO.Packaging.ZipPackage.Open(filePath, FileMode.Create);
try
{
XPathDocument xpDocument = new XPathDocument(new StringReader(wordOpenXML));
XPathNavigator xpNavigator = xpDocument.CreateNavigator();
XmlNamespaceManager nsManager = new XmlNamespaceManager(xpNavigator.NameTable);
nsManager.AddNamespace("pkg", packageXmlns);
XPathNodeIterator xpIterator = xpNavigator.Select("//pkg:part", nsManager);
while (xpIterator.MoveNext())
{
Uri partUri = new Uri(xpIterator.Current.GetAttribute("name", packageXmlns), UriKind.Relative);
PackagePart pkgPart = newPkg.CreatePart(partUri, xpIterator.Current.GetAttribute("contentType", packageXmlns));
// Set this package part's contents to this XML node's inner XML, sans its surrounding xmlData element.
string strInnerXml = xpIterator.Current.InnerXml
.Replace("<pkg:xmlData xmlns:pkg=\"" + packageXmlns + "\">", "")
.Replace("</pkg:xmlData>", "");
byte[] buffer = Encoding.UTF8.GetBytes(strInnerXml);
pkgPart.GetStream().Write(buffer, 0, buffer.Length);
}
newPkg.Flush();
}
finally
{
newPkg.Close();
}
}
这篇关于如何将WordOpenXML属性转换成System.IO.Packaging.Package?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!