使用openxml从c#写入powerpoint pptx

时间:2016-03-24 10:56:46

标签: c# powerpoint openxml

我正在使用csharp中的openxml将数据写入pptx最后一张幻灯片,下面是保存ppt的代码

public static void CreateTableInLastSlide(PresentationDocument presentationDocument)
    {
        // Get the presentation Part of the presentation document
        PresentationPart presentationPart = presentationDocument.PresentationPart;

        // Get the Slide Id collection of the presentation document
        var slideIdList = presentationPart.Presentation.SlideIdList;

        if (slideIdList == null)
        {
            throw new NullReferenceException("The number of slide is empty, please select a ppt with a slide at least again");
        }

        // Get all Slide Part of the presentation document
        var list = slideIdList.ChildElements
                    .Cast<SlideId>()
                    .Select(x => presentationPart.GetPartById(x.RelationshipId))
                    .Cast<SlidePart>();

        // Get the last Slide Part of the presentation document
        var tableSlidePart = (SlidePart)list.Last();

        // Declare and instantiate the graphic Frame of the new slide

        P.GraphicFrame graphicFrame = tableSlidePart.Slide.CommonSlideData.ShapeTree.AppendChild(new P.GraphicFrame());

        ApplicationNonVisualDrawingPropertiesExtension applicationNonVisualDrawingPropertiesExtension = new ApplicationNonVisualDrawingPropertiesExtension();
        P14.ModificationId modificationId1 = new P14.ModificationId() { Val = 3229994563U };
        modificationId1.AddNamespaceDeclaration("p14", "http://schemas.microsoft.com/office/powerpoint/2010/main");
        applicationNonVisualDrawingPropertiesExtension.Append(modificationId1);
        graphicFrame.NonVisualGraphicFrameProperties = new DocumentFormat.OpenXml.Presentation.NonVisualGraphicFrameProperties
        (new A.NonVisualDrawingProperties() { Id = 5, Name = "table 1" },
        new A.NonVisualGraphicFrameDrawingProperties(new A.GraphicFrameLocks() { NoGrouping = true }),
        new ApplicationNonVisualDrawingProperties(new ApplicationNonVisualDrawingPropertiesExtensionList(applicationNonVisualDrawingPropertiesExtension)));

        graphicFrame.Transform = new Transform(new Offset() { X = 10, Y = 10 });
        graphicFrame.Graphic = new A.Graphic(new A.GraphicData(GenerateTable()) { Uri = "http://schemas.openxmlformats.org/drawingml/2006/table" });
        presentationPart.Presentation.Save();

    }

文件以所需数据保存但是 打开powerpoint时出现以下错误:

Powerpoint发现“Sample.pptx”中的内容存在问题。电源点可以尝试修复演示文稿。

修复后,内容显示正常。但由于文件在运行代码时已损坏,因此下次不会使用最新数据进行更新。 请帮我解决问题

试图在不同版本的办公室开放,但没有运气 - 从不同版本创建模板仍然会出现问题

0 个答案:

没有答案