简体   繁体   中英

Create table using OpenXML in powerpoint

I have a scenario where I have Datagrid in my Silverlight application and I want the data to be exported to Power Point.

I researched and found only links, to export image(screen captured) to powerpoint. In my scenario , a screen capture also wont work as I have scroll bar with 20 columns and it is not getting displayed with the above solution.

Any work around for this scenario.

PS : I do not intend to use any 3rd party controls.

Edit :

I have now tried to use OpenXML , but i am getting an error as below : 在此处输入图片说明

My code is as below :

         using A = DocumentFormat.OpenXml.Drawing;
         using P14 = DocumentFormat.OpenXml.Office2010.PowerPoint;
         using System;
         using System.Collections.Generic;
         using System.Linq;
         using System.Text;
         using DocumentFormat.OpenXml;

         using DocumentFormat.OpenXml.Packaging;
         using DocumentFormat.OpenXml.Presentation;
         using P = DocumentFormat.OpenXml.Presentation;
         using D = DocumentFormat.OpenXml.Drawing;
         using DocumentFormat.OpenXml.Drawing;

        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();

    }


    private static A.Table GenerateTable()
    {
        string[,] tableSources = new string[,] { { "name", "age" }, { "Tom", "25" } };

        // Declare and instantiate table 
        A.Table table = new A.Table();

        // Specify the required table properties for the table
        A.TableProperties tableProperties = new A.TableProperties() { FirstRow = true, BandRow = true };
        A.TableStyleId tableStyleId = new A.TableStyleId();
        tableStyleId.Text = "{5C22544A-7EE6-4342-B048-85BDC9FD1C3A}";

        tableProperties.Append(tableStyleId);

        // Declare and instantiate tablegrid and colums
        A.TableGrid tableGrid1 = new A.TableGrid();
        A.GridColumn gridColumn1 = new A.GridColumn() { Width = 3048000L };
        A.GridColumn gridColumn2 = new A.GridColumn() { Width = 3048000L };

        tableGrid1.Append(gridColumn1);
        tableGrid1.Append(gridColumn2);
        table.Append(tableProperties);
        table.Append(tableGrid1);
        for (int row = 0; row < tableSources.GetLength(0); row++)
        {
            // Instantiate the table row
            A.TableRow tableRow = new A.TableRow() { Height = 370840L };
            for (int column = 0; column < tableSources.GetLength(1); column++)
            {
                tableRow.Append(CreateTextCell(tableSources.GetValue(row, column).ToString()));
            }

            table.Append(tableRow);
        }

        return table;
    }

Am i using the correct graphics and transform ?? The line which is causing the problem according to me is :

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

As if i comment his line i dont get the error, but neither do i get the table :o/ Any help ?

Finally i was able to solve the problem using the Open XML productivity tool ( here ) . The line i highlighted was the error. I needed to add the below code :

 List<OpenXmlElement> elements = new List<OpenXmlElement>();
        elements.Add(new P.NonVisualGraphicFrameProperties
            (new P.NonVisualDrawingProperties() { Id = 1, Name = "xyz" }, new P.NonVisualGraphicFrameDrawingProperties(),new ApplicationNonVisualDrawingProperties()));



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

Thus i was able to get output without any errors :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM