0

当我从 docx 文件的路径打开 WordProcessingDocument 时,会创建功能区。但是,当我从 docx 文件的字节内容中打开它时,它不会被创建。以下内容未显示 docx 文件中的功能区:-

public void AddRibbonToTemplate(byte[] templateContent)
        {
                string sanitizedRibbonXml = GetRibbonXML().ToString().Replace("xmlns=\"\"", "");
                MemoryStream stream = new MemoryStream();
                stream.Write(templateContent, 0, templateContent.Length);
                using (WordprocessingDocument myDoc = WordprocessingDocument.Open(stream, true))
                {
                    MainDocumentPart mainPart = myDoc.MainDocumentPart;

                    if (myDoc.GetPartsCountOfType<RibbonExtensibilityPart>() > 0)
                        myDoc.DeletePart(myDoc.GetPartsOfType<RibbonExtensibilityPart>().First());

                    RibbonExtensibilityPart ribbonExtensibilityPart = myDoc.AddNewPart<RibbonExtensibilityPart>();
                    ribbonExtensibilityPart.CustomUI = new DocumentFormat.OpenXml.Office.CustomUI.CustomUI(sanitizedRibbonXml);
                    myDoc.CreateRelationshipToPart(ribbonExtensibilityPart);

                }
                stream.Close();
         }

下面显示了功能区:-

public void AddRibbonToTemplate(string documentFileName)
        {
            string sanitizedRibbonXml = GetRibbonXML().ToString().Replace("xmlns=\"\"", "");
            using (WordprocessingDocument myDoc = WordprocessingDocument.Open(documentFileName, true))
            {
                MainDocumentPart mainPart = myDoc.MainDocumentPart;

                if (myDoc.GetPartsCountOfType<RibbonExtensibilityPart>() > 0)
                    myDoc.DeletePart(myDoc.GetPartsOfType<RibbonExtensibilityPart>().First());

                RibbonExtensibilityPart ribbonExtensibilityPart = myDoc.AddNewPart<RibbonExtensibilityPart>();
                ribbonExtensibilityPart.CustomUI = new DocumentFormat.OpenXml.Office.CustomUI.CustomUI(sanitizedRibbonXml);
                myDoc.CreateRelationshipToPart(ribbonExtensibilityPart);
            }
        }

GetRibbonXml() 获取功能区的 XML。我正在删除添加到功能区元素中的 xmlns=""(来自这里的问题)。

有人可以提出一些想法,为什么没有添加功能区。甚至 customUI 文件夹也不会添加到 word zip 文件中。任何人都可以帮忙。

4

1 回答 1

0

我应该通过将流写入文件来验证流。以下是代码:-

public void AddRibbonToTemplate(byte[] templateContent)
        {
                string sanitizedRibbonXml = GetRibbonXML().ToString().Replace("xmlns=\"\"", "");
                using (MemoryStream stream = new MemoryStream())
                {
                    stream.Write(templateContent, 0, (int)templateContent.Length);
                    using (WordprocessingDocument myDoc = WordprocessingDocument.Open(stream, true))
                    {
                        MainDocumentPart mainPart = myDoc.MainDocumentPart;

                        if (myDoc.GetPartsCountOfType<RibbonExtensibilityPart>() > 0)
                            myDoc.DeletePart(myDoc.GetPartsOfType<RibbonExtensibilityPart>().First());

                        RibbonExtensibilityPart ribbonExtensibilityPart = myDoc.AddNewPart<RibbonExtensibilityPart>();
                        ribbonExtensibilityPart.CustomUI = new DocumentFormat.OpenXml.Office.CustomUI.CustomUI(sanitizedRibbonXml);
                        myDoc.CreateRelationshipToPart(ribbonExtensibilityPart);

                    }
                    using (FileStream fileStream = new FileStream("Test20.docx",
                    System.IO.FileMode.CreateNew))
                    {
                        stream.WriteTo(fileStream);
                    }


                }

         }
于 2010-12-28T07:59:17.407 回答