2010-12-13 8 views
4

開いているxml word docファイルのいくつかの組み込みプロパティ(作成者、最終更新日など)にアクセスしたいと思います。私はこの目的のためにオープンxmlのsdk2.0を使用したいと思います。だから、もし私はこれらの組み込みプロパティにプログラムでアクセスできる任意のクラスや方法があるのだろうかと思います。open xml worddocファイルの組み込みプロパティにプログラムでアクセスする方法

+0

[https://searchcode.com/codesearch/view/10033886/] (https://searchcode.com/codesearch/view/10033886/)のヒントについては、このコードをご覧ください。 – AnorZaken

答えて

8

次の方法の説明はhereを見つけることができますが、かなり多くのあなたは、このメソッドにcore.xmlファイルから取得したいプロパティに渡す必要があり、それが値を返します:

をパワーポイントのような他のオープンXML形式のためにも働く

using System.IO.Packaging.Package; 

[...] 

using (var package = Package.Open(path)) 
{ 
    package.PackageProperties.Creator = Environment.UserName; 
    package.PackageProperties.LastModifiedBy = Environment.UserName; 
} 

public static string WDRetrieveCoreProperty(string docName, string propertyName) 
{ 
    // Given a document name and a core property, retrieve the value of the property. 
    // Note that because this code uses the SelectSingleNode method, 
    // the search is case sensitive. That is, looking for "Author" is not 
    // the same as looking for "author". 

    const string corePropertiesSchema = "http://schemas.openxmlformats.org/package/2006/metadata/core-properties"; 
    const string dcPropertiesSchema = "http://purl.org/dc/elements/1.1/"; 
    const string dcTermsPropertiesSchema = "http://purl.org/dc/terms/"; 

    string propertyValue = string.Empty; 

    using (WordprocessingDocument wdPackage = WordprocessingDocument.Open(docName, true)) 
    { 
     // Get the core properties part (core.xml). 
     CoreFilePropertiesPart corePropertiesPart = wdPackage.CoreFilePropertiesPart; 

     // Manage namespaces to perform XML XPath queries. 
     NameTable nt = new NameTable(); 
     XmlNamespaceManager nsManager = new XmlNamespaceManager(nt); 
     nsManager.AddNamespace("cp", corePropertiesSchema); 
     nsManager.AddNamespace("dc", dcPropertiesSchema); 
     nsManager.AddNamespace("dcterms", dcTermsPropertiesSchema); 

     // Get the properties from the package. 
     XmlDocument xdoc = new XmlDocument(nt); 

     // Load the XML in the part into an XmlDocument instance. 
     xdoc.Load(corePropertiesPart.GetStream()); 

     string searchString = string.Format("//cp:coreProperties/{0}", propertyName); 

     XmlNode xNode = xdoc.SelectSingleNode(searchString, nsManager); 
     if (!(xNode == null)) 
     { 
     propertyValue = xNode.InnerText; 
     } 
    } 

    return propertyValue; 
} 
+0

ありがとうたくさんのアマチュア。 – stazera

1

また、パッケージングAPIを使用することができます。

+0

罰金!私はxlsxのために使用されます – elle0087

関連する問題