2012-08-10 33 views
0

我正在嘗試使用普通C#和XLinq而不是Ooxml SDK更改程序包(Word或Excel文檔)的自定義屬性的值。但是,它會破壞文件並且不會反映包中的更改。如何在不使用Ooxml SDK的情況下更新Word文檔的自定義屬性?

有人可以請建議什麼是不正確的嗎?

 Package package = null; 
     try 
     { 
      package = Package.Open("NewCustomProp.docx", FileMode.OpenOrCreate, FileAccess.ReadWrite); 
      foreach (var packagePart in package.GetParts()) 
      { 
       if (packagePart.ContentType == "application/vnd.openxmlformats-officedocument.custom-properties+xml") 
       { 
        var packageStream = packagePart.GetStream(FileMode.OpenOrCreate, FileAccess.ReadWrite); 
        using (StreamReader streamReader = new StreamReader(packageStream)) 
        { 
         try 
         { 
          string ns = "http://schemas.openxmlformats.org/officeDocument/2006/custom-properties"; 
          XDocument xDocument = XDocument.Parse(streamReader.ReadToEnd()); 
          var properties = xDocument.Descendants(XName.Get("property", ns)).Where(x => x.Attribute(XName.Get("name")).Value == "NewCustomProp").ToList(); 
          if (properties.Count > 0) 
          { 
           foreach (var currentProperty in properties) 
           { 
            var valueNode = currentProperty.Descendants().First(); 
            valueNode.Value = "This is new value of Custom Property"; 
           } 

           StringBuilder innerXmlSB = new StringBuilder(); 
           xDocument.Nodes().ToList().ForEach(node => innerXmlSB.Append(node.ToString())); 
           string innerXml = innerXmlSB.ToString(); 
           byte[] buffer = Encoding.UTF8.GetBytes(innerXml); 
           packageStream.Write(buffer, 0, buffer.Length); 

           //tried this as well 
           //xDocument.Save(packageStream); 
          } 
         } 
         catch (Exception ex) 
         { 
          Debug.WriteLine(ex.ToString()); 
         } 
        } 
       } 
      } 
      package.Flush(); 
     } 
     finally 
     { 
      package.Close(); 
     } 

回答

1

對不起,不知道很多關於C#,但這是它是如何在VB.NET(即要求沒有SDK)進行使用LINQ,XML文本和IO.Packaging。

Imports System.IO 
Imports System.IO.Packaging 
Imports <xmlns:vt="http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes"> 
Imports System.Xml 

Module Module1 
    Sub Main() 
     Dim docFilePath = "C:\Users\you\Documents\Doc2.docx" 
     Using presentationPackage As Package = Package.Open(docFilePath, FileMode.OpenOrCreate, FileAccess.ReadWrite) 
      Dim uriCustom = New Uri("/docProps/custom.xml", UriKind.Relative) 
      Dim customPart = presentationPackage.GetPart(uriCustom) 
      Dim customStream = New StreamReader(customPart.GetStream) 

      Dim custom = XDocument.Load(customStream) 
      Dim customProperties = custom.Root.Elements.<vt:lpwstr> 

      For Each prop In customProperties 
       prop.Value = "My New Custom Value" 
      Next 

      Using xw As XmlWriter = XmlWriter.Create(customPart.GetStream(FileMode.Create, FileAccess.Write)) 
       custom.Save(xw) 
      End Using 
     End Using 
    End Sub 

End Module 

注意這一點 - <vt:lpwstr> -is爲 「文本」 類型的自定義屬性。它只是直接改變數值。如果你想查詢確切的客戶屬性或改變自定義屬性的名稱或使用不同類型的東西,你必須在下面的代碼中改變一些東西。

相關問題