首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何不使用Ooxml更新Word文档的自定义属性?

如何不使用Ooxml更新Word文档的自定义属性?
EN

Stack Overflow用户
提问于 2012-08-10 03:01:38
回答 1查看 3K关注 0票数 0

我试图使用普通的和XLinq来更改包 (Word或Excel文档)的自定义属性的值,而不是使用Ooxml。但是,它破坏了文件,并且没有反映包中的更改。

有人能给我建议一下什么不对吗?

代码语言:javascript
复制
        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();
        }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-08-15 22:23:03

对不起,对C#不太了解,但这就是在VB.NET中使用Linq、XML和IO.Packaging (即不需要SDK )所做的事情。

代码语言:javascript
复制
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

请注意,这是自定义属性的"Text“类型的<vt:lpwstr> -is。它只是直接改变了值。如果您想要查询确切的客户属性或更改诸如自定义属性的名称之类的内容,或者使用不同的类型,则必须在下面的代码中更改一些内容。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11894892

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档