2011-05-25 2 views
1

J'ai besoin de lire les détails du fichier, en particulier, Auteurs, Titre, Sujet, à partir de nouveaux fichiers Office (.docx, .xlsx). J'ai trouvé cet article de MS, qui a également quelques méthodes - http://msdn.microsoft.com/en-us/library/bb739835%28v=office.12%29.aspx Mais je peux sembler faire fonctionner cela. Méthode J'utilise est:xml sdk 2.0 pour lire les détails du fichier Office 10/propriétés

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; 
} 

Je suis d'appeler cette méthode comme:

WDRetrieveCoreProperty(textBox1.Text, "Authors"); 
// textBox1 has path to some .docx file 

Mais elle retourne toujours nulle. Alors qu'est-ce qui ne va pas avec ça?

Répondre

0

Je l'ai fait ...

using System.IO.Packaging; // Assembly WindowsBase.dll 
    : 
    static void Main(string[] args) 
    { 
     String path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData); 
     String file = Path.Combine(path, "Doc1.docx"); 

     Package docx = Package.Open(file, FileMode.Open, FileAccess.Read); 
     String subject = docx.PackageProperties.Subject; 
     String title = docx.PackageProperties.Title; 
     docx.Close(); 
    } 
2

Je sais que cette question est vieux, mais a couru à travers tout en recherchant la même question. L'exemple sur MSDN a l'exemple de code pour la méthode pour récupérer les propriétés de base, mais n'a pas un exemple utilisant la méthode.

Lors du passage de la propriété pour trouver vous devez inclure le préfixe d'espace de noms. Donc, l'accès à la propriété core lastModifiedBy à l'aide de la méthode OP ressemblerait à:

WDRetrieveCoreProperty(textBox1.Text, "cp:lastModifiedBy"); 
Questions connexes