在Powerpoint中使用OpenXML创建表

adityaswami89

我有一个场景,其中在我的Silverlight应用程序中Datagrid,并且希望将数据导出到Power Point。

我研究并发现仅链接,即可将图像(屏幕截图)导出到PowerPoint。在我的情况下,屏幕截图也无法正常工作,因为我有20列的滚动条,并且在上述解决方案中没有显示。

在这种情况下的任何变通办法。

PS:我不打算使用任何第三方控件。

编辑 :

我现在尝试使用OpenXML,但出现以下错误: 在此处输入图片说明

我的代码如下:

         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;
    }

我是否使用正确的图形进行转换?根据我,导致问题的行是:

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

好像我在评论他的话我没有得到错误,但是我也没有得到该表:o /有帮助吗?

adityaswami89

最终,我能够使用Open XML生产力工具(此处解决问题我突出显示的行是错误。我需要添加以下代码:

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

因此,我能够获得没有任何错误的输出:)

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何在 PowerPoint 中使用 openxml 为单元格添加边框?

来自分类Dev

在C#中使用OpenXML SDK创建XLSX文件

来自分类Dev

使用OpenXML创建合并单元

来自分类Dev

使用OpenXML创建合并单元

来自分类Dev

使用 OpenXML 创建单词插件

来自分类Dev

在C#中使用OpenXml在Excel中创建日期单元格中的问题

来自分类Dev

在Powershell中使用HTML创建表

来自分类Dev

在表创建脚本中使用变量

来自分类Dev

在SQL中使用内部联接创建表

来自分类Dev

在R中使用Kable创建表

来自分类Dev

在python中使用嵌套循环创建表

来自分类Dev

在R中使用rollapply()创建频率表

来自分类Dev

在SQL中使用内部联接创建表

来自分类Dev

在iOS中使用列创建可视表

来自分类Dev

在RDS中使用ansible创建mysql表

来自分类Dev

在Teradata中使用存储过程创建表

来自分类Dev

在SAS中使用循环创建表

来自分类Dev

在R中使用变量标签创建表

来自分类Dev

在 SQL 中使用 CTE 创建表

来自分类Dev

使用OpenXML将PowerPoint演示文稿另存为PDF

来自分类Dev

如何使用OpenXML在PowerPoint幻灯片中插入形状

来自分类Dev

在Powerpoint中使用InputBox替换

来自分类Dev

Powerpoint中使用的字体数

来自分类Dev

在powerpoint中使用变形过渡

来自分类Dev

使用VBA创建PowerPoint形状

来自分类Dev

使用动画从PowerPoint创建PDF

来自分类Dev

在PowerPoint 2010中使用A),B)等创建自定义列表标签

来自分类Dev

在SQL Server 2012中使用openXML

来自分类Dev

使用jQuery在JavaScript中使用任何字段从JSON创建表