Skip to content
  • 首页
  • 留言
  • 关于

Share the joys of programming and technology

使用OpenXML SDK往PPT中指定的幻灯片插入表格

使用OpenXML SDK往PPT中指定的幻灯片插入表格

2021年2月16日 liyanliang Comments 0 Comment
阅读次数: 154

本主题演示如何使用 Open XML SDK中的类以编程方式往演示文稿中指定的幻灯片插入表格。

       public static void AddTableToPresentation(string file, int slideIndex, int nRow, int nColumn, long width, long height)
        {
            using (PresentationDocument doc = PresentationDocument.Open(file, true))
            {
                // Get the slide part for the specified slide.
                SlidePart slidePart = GetSlidePartbyIndex(doc, slideIndex);

                CreateTable(slidePart, nRow, nColumn, width,  height);
            }
        }

        private static SlidePart GetSlidePartbyIndex(PresentationDocument doc, int slideIndex)
        {
            if (doc == null)
            {
                throw new ArgumentNullException("presentationDocument");
            }

            // Use the CountSlides sample to get the number of slides in the presentation.
            int slidesCount = CountSlides(doc);

            if (slideIndex < 0 || slideIndex >= slidesCount)
            {
                throw new ArgumentOutOfRangeException("slideIndex");
            }

            // Get the presentation part from the presentation document. 
            PresentationPart presentationPart = doc.PresentationPart;

            // Get the presentation from the presentation part.
            Presentation presentation = presentationPart.Presentation;

            // Get the list of slide IDs in the presentation.
            SlideIdList slideIdList = presentation.SlideIdList;

            // Get the slide ID of the specified slide
            SlideId slideId = slideIdList.ChildElements[slideIndex] as SlideId;

            // Get the relationship ID of the slide.
            string slideRelId = slideId.RelationshipId;

            // Save the modified presentation.
            presentation.Save();

            // Get the slide part for the specified slide.
            SlidePart slidePart = presentationPart.GetPartById(slideRelId) as SlidePart;
            return slidePart;
        }

        private static void CreateTable(SlidePart slidePart, int nRow, int nColumn, long width = 8127999L, long height = 370840L )
        {
            //列宽
            if (width == 8127999L)
            {
                width = 8127999L / nColumn;
            }

            Slide slide1 = new Slide();
            slide1.AddNamespaceDeclaration("a", "http://schemas.openxmlformats.org/drawingml/2006/main");
            slide1.AddNamespaceDeclaration("r", "http://schemas.openxmlformats.org/officeDocument/2006/relationships");
            slide1.AddNamespaceDeclaration("p", "http://schemas.openxmlformats.org/presentationml/2006/main");

            //
            CommonSlideData commonSlideData1 = new CommonSlideData();

            ShapeTree shapeTree1 = new ShapeTree();

            NonVisualGroupShapeProperties nonVisualGroupShapeProperties1 = new NonVisualGroupShapeProperties();
            NonVisualDrawingProperties nonVisualDrawingProperties1 = new NonVisualDrawingProperties() { Id = (UInt32Value)1U, Name = "" };
            NonVisualGroupShapeDrawingProperties nonVisualGroupShapeDrawingProperties1 = new NonVisualGroupShapeDrawingProperties();
            ApplicationNonVisualDrawingProperties applicationNonVisualDrawingProperties1 = new ApplicationNonVisualDrawingProperties();

            nonVisualGroupShapeProperties1.Append(nonVisualDrawingProperties1);
            nonVisualGroupShapeProperties1.Append(nonVisualGroupShapeDrawingProperties1);
            nonVisualGroupShapeProperties1.Append(applicationNonVisualDrawingProperties1);

            GroupShapeProperties groupShapeProperties1 = new GroupShapeProperties();

            A.TransformGroup transformGroup1 = new A.TransformGroup();
            A.Offset offset1 = new A.Offset() { X = 0L, Y = 0L };
            A.Extents extents1 = new A.Extents() { Cx = 0L, Cy = 0L };
            A.ChildOffset childOffset1 = new A.ChildOffset() { X = 0L, Y = 0L };
            A.ChildExtents childExtents1 = new A.ChildExtents() { Cx = 0L, Cy = 0L };

            transformGroup1.Append(offset1);
            transformGroup1.Append(extents1);
            transformGroup1.Append(childOffset1);
            transformGroup1.Append(childExtents1);

            groupShapeProperties1.Append(transformGroup1);


            //
            GraphicFrame graphicFrame1 = new GraphicFrame();

            NonVisualGraphicFrameProperties nonVisualGraphicFrameProperties1 = new NonVisualGraphicFrameProperties();
            NonVisualDrawingProperties nonVisualDrawingProperties4 = new NonVisualDrawingProperties() { Id = (UInt32Value)4U, Name = "表格 1" };

            NonVisualGraphicFrameDrawingProperties nonVisualGraphicFrameDrawingProperties1 = new NonVisualGraphicFrameDrawingProperties();
            A.GraphicFrameLocks graphicFrameLocks1 = new A.GraphicFrameLocks() { NoGrouping = true };

            nonVisualGraphicFrameDrawingProperties1.Append(graphicFrameLocks1);
            ApplicationNonVisualDrawingProperties applicationNonVisualDrawingProperties4 = new ApplicationNonVisualDrawingProperties();

            nonVisualGraphicFrameProperties1.Append(nonVisualDrawingProperties4);
            nonVisualGraphicFrameProperties1.Append(nonVisualGraphicFrameDrawingProperties1);
            nonVisualGraphicFrameProperties1.Append(applicationNonVisualDrawingProperties4);

            Transform transform1 = new Transform();
            A.Offset offset2 = new A.Offset() { X = 2032000L, Y = 719666L };
            A.Extents extents2 = new A.Extents() { Cx = 8127999L, Cy = 741680L };

            transform1.Append(offset2);
            transform1.Append(extents2);

            A.Graphic graphic1 = new A.Graphic();

            A.GraphicData graphicData1 = new A.GraphicData() { Uri = "http://schemas.openxmlformats.org/drawingml/2006/table" };

            A.Table table1 = new A.Table();

            A.TableProperties tableProperties1 = new A.TableProperties() { FirstRow = true, BandRow = true };
            A.TableStyleId tableStyleId1 = new A.TableStyleId();
            tableStyleId1.Text = "{5C22544A-7EE6-4342-B048-85BDC9FD1C3A}";

            tableProperties1.Append(tableStyleId1);

            A.TableGrid tableGrid1 = new A.TableGrid();

            for (int idx = 0; idx < nColumn; ++idx)
            {
                A.GridColumn gridColumn1 = new A.GridColumn() { Width = width };

                A.ExtensionList extensionList1 = new A.ExtensionList();

                A.Extension extension1 = new A.Extension() { Uri = "{9D8B030D-6E8A-4147-A177-3AD203B41FA5}" };

                OpenXmlUnknownElement openXmlUnknownElement1 = OpenXmlUnknownElement.CreateOpenXmlUnknownElement("<a16:colId xmlns:a16=\"http://schemas.microsoft.com/office/drawing/2014/main\" val=\"3310506284\" />");

                extension1.Append(openXmlUnknownElement1);

                extensionList1.Append(extension1);

                gridColumn1.Append(extensionList1);
                tableGrid1.Append(gridColumn1);  
            }

            table1.Append(tableProperties1);
            table1.Append(tableGrid1);

            for (int i = 0; i < nRow; ++i)
            {
                A.TableRow tableRow1 = new A.TableRow() { Height = height };

                for (int j = 0; j < nColumn; ++j)
                {
                    A.TableCell tableCell1 = new A.TableCell();

                    A.TextBody textBody3 = new A.TextBody();
                    A.BodyProperties bodyProperties3 = new A.BodyProperties();
                    A.ListStyle listStyle3 = new A.ListStyle();

                    A.Paragraph paragraph3 = new A.Paragraph();
                    A.EndParagraphRunProperties endParagraphRunProperties3 = new A.EndParagraphRunProperties() { Language = "zh-CN", AlternativeLanguage = "en-US", Dirty = false };

                    paragraph3.Append(endParagraphRunProperties3);

                    textBody3.Append(bodyProperties3);
                    textBody3.Append(listStyle3);
                    textBody3.Append(paragraph3);
                    A.TableCellProperties tableCellProperties1 = new A.TableCellProperties();

                    tableCell1.Append(textBody3);
                    tableCell1.Append(tableCellProperties1);


                    tableRow1.Append(tableCell1);
                }

                A.ExtensionList extensionList4 = new A.ExtensionList();

                A.Extension extension4 = new A.Extension() { Uri = "{0D108BD9-81ED-4DB2-BD59-A6C34878D82A}" };

                OpenXmlUnknownElement openXmlUnknownElement4 = OpenXmlUnknownElement.CreateOpenXmlUnknownElement("<a16:rowId xmlns:a16=\"http://schemas.microsoft.com/office/drawing/2014/main\" val=\"1915637097\" />");

                extension4.Append(openXmlUnknownElement4);

                extensionList4.Append(extension4);

                tableRow1.Append(extensionList4);

                table1.Append(tableRow1);
            }

            graphicData1.Append(table1);

            graphic1.Append(graphicData1);

            graphicFrame1.Append(nonVisualGraphicFrameProperties1);
            graphicFrame1.Append(transform1);
            graphicFrame1.Append(graphic1);

            //
            shapeTree1.Append(nonVisualGroupShapeProperties1);
            shapeTree1.Append(groupShapeProperties1);
            shapeTree1.Append(graphicFrame1);

            CommonSlideDataExtensionList commonSlideDataExtensionList1 = new CommonSlideDataExtensionList();

            CommonSlideDataExtension commonSlideDataExtension1 = new CommonSlideDataExtension() { Uri = "{BB962C8B-B14F-4D97-AF65-F5344CB8AC3E}" };

            P14.CreationId creationId1 = new P14.CreationId() { Val = (UInt32Value)3891535481U };
            creationId1.AddNamespaceDeclaration("p14", "http://schemas.microsoft.com/office/powerpoint/2010/main");

            commonSlideDataExtension1.Append(creationId1);

            commonSlideDataExtensionList1.Append(commonSlideDataExtension1);

            commonSlideData1.Append(shapeTree1);
            commonSlideData1.Append(commonSlideDataExtensionList1);

            ColorMapOverride colorMapOverride1 = new ColorMapOverride();
            A.MasterColorMapping masterColorMapping1 = new A.MasterColorMapping();

            colorMapOverride1.Append(masterColorMapping1);

            Timing timing1 = new Timing();

            TimeNodeList timeNodeList1 = new TimeNodeList();

            ParallelTimeNode parallelTimeNode1 = new ParallelTimeNode();
            CommonTimeNode commonTimeNode1 = new CommonTimeNode() { Id = (UInt32Value)1U, Duration = "indefinite", Restart = TimeNodeRestartValues.Never, NodeType = TimeNodeValues.TmingRoot };

            parallelTimeNode1.Append(commonTimeNode1);

            timeNodeList1.Append(parallelTimeNode1);

            timing1.Append(timeNodeList1);

            slide1.Append(commonSlideData1);
            slide1.Append(colorMapOverride1);
            slide1.Append(timing1);



            slidePart.Slide = slide1;

            // Save the modified slide.
            slidePart.Slide.Save();
        }

效果:

相关文章

  • 设计模式-抽象工厂模式设计模式-抽象工厂模式
  • Midas XD [错误] 右侧挡土墙的最下端深度必须小于地基的最下端深度Midas XD [错误] 右侧挡土墙的最下端深度必须小于地基的最下端深度
  • 给不规则的多边形闭合区域填充颜色给不规则的多边形闭合区域填充颜色
  • Modern OpenGL绘制圆柱体Modern OpenGL绘制圆柱体
  • C++实现一个简单的语言解释器C++实现一个简单的语言解释器
  • ODA的基本操作-平移、旋转、矩阵变换ODA的基本操作-平移、旋转、矩阵变换

C#
OpenXML

Post navigation

PREVIOUS
ObjectARX开发视频教程(C++)-创建模态对话框绘制直线
NEXT
C++编写的情人节小程序

发表回复 取消回复

您的邮箱地址不会被公开。 必填项已用 * 标注

近期文章

  • ANR崩溃日志查看方法
  • 通过数学方法来计算short类型的变量w的低八位x和高八位
  • 3dTiles数据解析
  • Games101和Games202脑图汇总
  • LearnOpenGL脑图汇总
  • IBL计算总结
  • C++实现一个简单的语言解释器
  • OpenGL-法线贴图(Normal Mapping)
  • OpenGL-卡通着色(Cartoon)
  • OpenGL几何着色器实现贝塞尔曲线
  • WinDbg检查内存泄漏
  • OpenGL雾化效果实现-每像素雾化
  • OpenGL实现billboard效果(CPU)
  • 算法:寻找异常数字
  • OpenGL 几何着色器的应用
  • Midas XD-构件详图开发
  • Midas XD-选筋助手开发
  • Civil Designer开发-检测规范自动生成控制截面
  • Civil Designer开发-公路桥梁承载能力检算评定
  • Midas W-满堂支架快速建模助手开发

全站热点

  • C++编写的情人节小程序 (2,080)
  • 提取最小封闭区域 (1,693)
  • Modern OpenGL绘制圆柱体 (1,606)
  • OpenGL开发环境搭建-GLFW与GLAD配置 超详细 (1,459)
  • 截面特性计算程序-附源码 (1,288)
  • OpenGL绘制旋转立方体 (1,104)
  • 判断一个点是否在闭合区域内 (1,032)
  • WordPress分页插件 – WP-PageNavi的使用(替换现有脚本) (948)
  • OpenGL实现billboard效果(CPU) (864)
  • Midas W-满堂支架快速建模助手开发 (832)
  • 从DLL中动态加载一个函数:LoadLibrary和GetProcAddress的使用 (746)
  • Midas XD [错误] 右侧挡土墙的最下端深度必须小于地基的最下端深度 (708)
  • 两跨连续梁影响线绘制-附源码 (685)
  • 土木想往土木软件开发方向发展,应该如何准备 (679)
  • OpenGL几何着色器实现贝塞尔曲线 (663)
  • 通过Spy++抓取窗口以查询对话框id (612)
  • 使用ODA数据库出现 “ODA_ASSUME”: 找不到标识符的错误 (547)
  • #pragma message 编译时提示信息 (527)
  • OpenGL雾化效果实现-每像素雾化 (507)
  • midas XD2020的开发 (474)

分类

  • C# (3)
  • C++ (19)
  • GIS (1)
  • MFC (3)
  • ObjectARX (2)
  • OpenGL (11)
  • Revit开发 (1)
  • 学习笔记 (2)
  • 岩土 (2)
  • 算法 (1)
  • 结构设计 (7)
  • 职场生涯 (1)
  • 计算几何 (3)

归档

  • 2024 年 12 月 (1)
  • 2024 年 10 月 (1)
  • 2024 年 9 月 (1)
  • 2023 年 3 月 (2)
  • 2022 年 10 月 (1)
  • 2022 年 3 月 (1)
  • 2022 年 2 月 (1)
  • 2022 年 1 月 (5)
  • 2021 年 11 月 (7)
  • 2021 年 6 月 (3)
  • 2021 年 5 月 (2)
  • 2021 年 3 月 (2)
  • 2021 年 2 月 (8)
  • 2021 年 1 月 (18)

标签

3dtiles anr Bezier Curves BillBoard C++ CDN CivilDesigner DLL EasyX fog glTF MFC Midas W Midas XD NormalMapping ObjectARX ODA OpenGL OpenXML Open XML PBR revit WinDbg 基坑设计 影响线 截面特性 桥梁 桥梁检测 桥梁设计 算法 计算几何 设计模式

书签

  • 李燕良的CSDN
  • 崔济东的博客
  • C++爱好者博客
  • 陈学伟的博客
  • 贾苏的博客
  • 陈睦锋的博客
  • 孙勇的博客

统计

  • 0
  • 226
  • 100
  • 304
  • 127
  • 267,124
  • 76,991

实时访问地域

© 2025   liyanliang.net Copyright. All Rights Reserved.