{"id":249,"date":"2021-02-16T12:43:11","date_gmt":"2021-02-16T04:43:11","guid":{"rendered":"http:\/\/liyanliang.net\/?p=249"},"modified":"2021-02-16T12:43:13","modified_gmt":"2021-02-16T04:43:13","slug":"%e4%bd%bf%e7%94%a8openxml-sdk%e5%be%80ppt%e4%b8%ad%e6%8c%87%e5%ae%9a%e7%9a%84%e5%b9%bb%e7%81%af%e7%89%87%e6%8f%92%e5%85%a5%e8%a1%a8%e6%a0%bc","status":"publish","type":"post","link":"http:\/\/liyanliang.net\/index.php\/2021\/02\/16\/%e4%bd%bf%e7%94%a8openxml-sdk%e5%be%80ppt%e4%b8%ad%e6%8c%87%e5%ae%9a%e7%9a%84%e5%b9%bb%e7%81%af%e7%89%87%e6%8f%92%e5%85%a5%e8%a1%a8%e6%a0%bc\/","title":{"rendered":"\u4f7f\u7528OpenXML SDK\u5f80PPT\u4e2d\u6307\u5b9a\u7684\u5e7b\u706f\u7247\u63d2\u5165\u8868\u683c"},"content":{"rendered":"\n<p>\u672c\u4e3b\u9898\u6f14\u793a\u5982\u4f55\u4f7f\u7528 Open XML SDK\u4e2d\u7684\u7c7b\u4ee5\u7f16\u7a0b\u65b9\u5f0f\u5f80\u6f14\u793a\u6587\u7a3f\u4e2d\u6307\u5b9a\u7684\u5e7b\u706f\u7247\u63d2\u5165\u8868\u683c\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">       public static void AddTableToPresentation(string file, int slideIndex, int nRow, int nColumn, long width, long height)\n        {\n            using (PresentationDocument doc = PresentationDocument.Open(file, true))\n            {\n                \/\/ Get the slide part for the specified slide.\n                SlidePart slidePart = GetSlidePartbyIndex(doc, slideIndex);\n\n                CreateTable(slidePart, nRow, nColumn, width,  height);\n            }\n        }\n\n        private static SlidePart GetSlidePartbyIndex(PresentationDocument doc, int slideIndex)\n        {\n            if (doc == null)\n            {\n                throw new ArgumentNullException(\"presentationDocument\");\n            }\n\n            \/\/ Use the CountSlides sample to get the number of slides in the presentation.\n            int slidesCount = CountSlides(doc);\n\n            if (slideIndex &lt; 0 || slideIndex >= slidesCount)\n            {\n                throw new ArgumentOutOfRangeException(\"slideIndex\");\n            }\n\n            \/\/ Get the presentation part from the presentation document. \n            PresentationPart presentationPart = doc.PresentationPart;\n\n            \/\/ Get the presentation from the presentation part.\n            Presentation presentation = presentationPart.Presentation;\n\n            \/\/ Get the list of slide IDs in the presentation.\n            SlideIdList slideIdList = presentation.SlideIdList;\n\n            \/\/ Get the slide ID of the specified slide\n            SlideId slideId = slideIdList.ChildElements[slideIndex] as SlideId;\n\n            \/\/ Get the relationship ID of the slide.\n            string slideRelId = slideId.RelationshipId;\n\n            \/\/ Save the modified presentation.\n            presentation.Save();\n\n            \/\/ Get the slide part for the specified slide.\n            SlidePart slidePart = presentationPart.GetPartById(slideRelId) as SlidePart;\n            return slidePart;\n        }\n\n        private static void CreateTable(SlidePart slidePart, int nRow, int nColumn, long width = 8127999L, long height = 370840L )\n        {\n            \/\/\u5217\u5bbd\n            if (width == 8127999L)\n            {\n                width = 8127999L \/ nColumn;\n            }\n\n            Slide slide1 = new Slide();\n            slide1.AddNamespaceDeclaration(\"a\", \"http:\/\/schemas.openxmlformats.org\/drawingml\/2006\/main\");\n            slide1.AddNamespaceDeclaration(\"r\", \"http:\/\/schemas.openxmlformats.org\/officeDocument\/2006\/relationships\");\n            slide1.AddNamespaceDeclaration(\"p\", \"http:\/\/schemas.openxmlformats.org\/presentationml\/2006\/main\");\n\n            \/\/\n            CommonSlideData commonSlideData1 = new CommonSlideData();\n\n            ShapeTree shapeTree1 = new ShapeTree();\n\n            NonVisualGroupShapeProperties nonVisualGroupShapeProperties1 = new NonVisualGroupShapeProperties();\n            NonVisualDrawingProperties nonVisualDrawingProperties1 = new NonVisualDrawingProperties() { Id = (UInt32Value)1U, Name = \"\" };\n            NonVisualGroupShapeDrawingProperties nonVisualGroupShapeDrawingProperties1 = new NonVisualGroupShapeDrawingProperties();\n            ApplicationNonVisualDrawingProperties applicationNonVisualDrawingProperties1 = new ApplicationNonVisualDrawingProperties();\n\n            nonVisualGroupShapeProperties1.Append(nonVisualDrawingProperties1);\n            nonVisualGroupShapeProperties1.Append(nonVisualGroupShapeDrawingProperties1);\n            nonVisualGroupShapeProperties1.Append(applicationNonVisualDrawingProperties1);\n\n            GroupShapeProperties groupShapeProperties1 = new GroupShapeProperties();\n\n            A.TransformGroup transformGroup1 = new A.TransformGroup();\n            A.Offset offset1 = new A.Offset() { X = 0L, Y = 0L };\n            A.Extents extents1 = new A.Extents() { Cx = 0L, Cy = 0L };\n            A.ChildOffset childOffset1 = new A.ChildOffset() { X = 0L, Y = 0L };\n            A.ChildExtents childExtents1 = new A.ChildExtents() { Cx = 0L, Cy = 0L };\n\n            transformGroup1.Append(offset1);\n            transformGroup1.Append(extents1);\n            transformGroup1.Append(childOffset1);\n            transformGroup1.Append(childExtents1);\n\n            groupShapeProperties1.Append(transformGroup1);\n\n\n            \/\/\n            GraphicFrame graphicFrame1 = new GraphicFrame();\n\n            NonVisualGraphicFrameProperties nonVisualGraphicFrameProperties1 = new NonVisualGraphicFrameProperties();\n            NonVisualDrawingProperties nonVisualDrawingProperties4 = new NonVisualDrawingProperties() { Id = (UInt32Value)4U, Name = \"\u8868\u683c 1\" };\n\n            NonVisualGraphicFrameDrawingProperties nonVisualGraphicFrameDrawingProperties1 = new NonVisualGraphicFrameDrawingProperties();\n            A.GraphicFrameLocks graphicFrameLocks1 = new A.GraphicFrameLocks() { NoGrouping = true };\n\n            nonVisualGraphicFrameDrawingProperties1.Append(graphicFrameLocks1);\n            ApplicationNonVisualDrawingProperties applicationNonVisualDrawingProperties4 = new ApplicationNonVisualDrawingProperties();\n\n            nonVisualGraphicFrameProperties1.Append(nonVisualDrawingProperties4);\n            nonVisualGraphicFrameProperties1.Append(nonVisualGraphicFrameDrawingProperties1);\n            nonVisualGraphicFrameProperties1.Append(applicationNonVisualDrawingProperties4);\n\n            Transform transform1 = new Transform();\n            A.Offset offset2 = new A.Offset() { X = 2032000L, Y = 719666L };\n            A.Extents extents2 = new A.Extents() { Cx = 8127999L, Cy = 741680L };\n\n            transform1.Append(offset2);\n            transform1.Append(extents2);\n\n            A.Graphic graphic1 = new A.Graphic();\n\n            A.GraphicData graphicData1 = new A.GraphicData() { Uri = \"http:\/\/schemas.openxmlformats.org\/drawingml\/2006\/table\" };\n\n            A.Table table1 = new A.Table();\n\n            A.TableProperties tableProperties1 = new A.TableProperties() { FirstRow = true, BandRow = true };\n            A.TableStyleId tableStyleId1 = new A.TableStyleId();\n            tableStyleId1.Text = \"{5C22544A-7EE6-4342-B048-85BDC9FD1C3A}\";\n\n            tableProperties1.Append(tableStyleId1);\n\n            A.TableGrid tableGrid1 = new A.TableGrid();\n\n            for (int idx = 0; idx &lt; nColumn; ++idx)\n            {\n                A.GridColumn gridColumn1 = new A.GridColumn() { Width = width };\n\n                A.ExtensionList extensionList1 = new A.ExtensionList();\n\n                A.Extension extension1 = new A.Extension() { Uri = \"{9D8B030D-6E8A-4147-A177-3AD203B41FA5}\" };\n\n                OpenXmlUnknownElement openXmlUnknownElement1 = OpenXmlUnknownElement.CreateOpenXmlUnknownElement(\"&lt;a16:colId xmlns:a16=\\\"http:\/\/schemas.microsoft.com\/office\/drawing\/2014\/main\\\" val=\\\"3310506284\\\" \/>\");\n\n                extension1.Append(openXmlUnknownElement1);\n\n                extensionList1.Append(extension1);\n\n                gridColumn1.Append(extensionList1);\n                tableGrid1.Append(gridColumn1);  \n            }\n\n            table1.Append(tableProperties1);\n            table1.Append(tableGrid1);\n\n            for (int i = 0; i &lt; nRow; ++i)\n            {\n                A.TableRow tableRow1 = new A.TableRow() { Height = height };\n\n                for (int j = 0; j &lt; nColumn; ++j)\n                {\n                    A.TableCell tableCell1 = new A.TableCell();\n\n                    A.TextBody textBody3 = new A.TextBody();\n                    A.BodyProperties bodyProperties3 = new A.BodyProperties();\n                    A.ListStyle listStyle3 = new A.ListStyle();\n\n                    A.Paragraph paragraph3 = new A.Paragraph();\n                    A.EndParagraphRunProperties endParagraphRunProperties3 = new A.EndParagraphRunProperties() { Language = \"zh-CN\", AlternativeLanguage = \"en-US\", Dirty = false };\n\n                    paragraph3.Append(endParagraphRunProperties3);\n\n                    textBody3.Append(bodyProperties3);\n                    textBody3.Append(listStyle3);\n                    textBody3.Append(paragraph3);\n                    A.TableCellProperties tableCellProperties1 = new A.TableCellProperties();\n\n                    tableCell1.Append(textBody3);\n                    tableCell1.Append(tableCellProperties1);\n\n\n                    tableRow1.Append(tableCell1);\n                }\n\n                A.ExtensionList extensionList4 = new A.ExtensionList();\n\n                A.Extension extension4 = new A.Extension() { Uri = \"{0D108BD9-81ED-4DB2-BD59-A6C34878D82A}\" };\n\n                OpenXmlUnknownElement openXmlUnknownElement4 = OpenXmlUnknownElement.CreateOpenXmlUnknownElement(\"&lt;a16:rowId xmlns:a16=\\\"http:\/\/schemas.microsoft.com\/office\/drawing\/2014\/main\\\" val=\\\"1915637097\\\" \/>\");\n\n                extension4.Append(openXmlUnknownElement4);\n\n                extensionList4.Append(extension4);\n\n                tableRow1.Append(extensionList4);\n\n                table1.Append(tableRow1);\n            }\n\n            graphicData1.Append(table1);\n\n            graphic1.Append(graphicData1);\n\n            graphicFrame1.Append(nonVisualGraphicFrameProperties1);\n            graphicFrame1.Append(transform1);\n            graphicFrame1.Append(graphic1);\n\n            \/\/\n            shapeTree1.Append(nonVisualGroupShapeProperties1);\n            shapeTree1.Append(groupShapeProperties1);\n            shapeTree1.Append(graphicFrame1);\n\n            CommonSlideDataExtensionList commonSlideDataExtensionList1 = new CommonSlideDataExtensionList();\n\n            CommonSlideDataExtension commonSlideDataExtension1 = new CommonSlideDataExtension() { Uri = \"{BB962C8B-B14F-4D97-AF65-F5344CB8AC3E}\" };\n\n            P14.CreationId creationId1 = new P14.CreationId() { Val = (UInt32Value)3891535481U };\n            creationId1.AddNamespaceDeclaration(\"p14\", \"http:\/\/schemas.microsoft.com\/office\/powerpoint\/2010\/main\");\n\n            commonSlideDataExtension1.Append(creationId1);\n\n            commonSlideDataExtensionList1.Append(commonSlideDataExtension1);\n\n            commonSlideData1.Append(shapeTree1);\n            commonSlideData1.Append(commonSlideDataExtensionList1);\n\n            ColorMapOverride colorMapOverride1 = new ColorMapOverride();\n            A.MasterColorMapping masterColorMapping1 = new A.MasterColorMapping();\n\n            colorMapOverride1.Append(masterColorMapping1);\n\n            Timing timing1 = new Timing();\n\n            TimeNodeList timeNodeList1 = new TimeNodeList();\n\n            ParallelTimeNode parallelTimeNode1 = new ParallelTimeNode();\n            CommonTimeNode commonTimeNode1 = new CommonTimeNode() { Id = (UInt32Value)1U, Duration = \"indefinite\", Restart = TimeNodeRestartValues.Never, NodeType = TimeNodeValues.TmingRoot };\n\n            parallelTimeNode1.Append(commonTimeNode1);\n\n            timeNodeList1.Append(parallelTimeNode1);\n\n            timing1.Append(timeNodeList1);\n\n            slide1.Append(commonSlideData1);\n            slide1.Append(colorMapOverride1);\n            slide1.Append(timing1);\n\n\n\n            slidePart.Slide = slide1;\n\n            \/\/ Save the modified slide.\n            slidePart.Slide.Save();\n        }<\/pre>\n\n\n\n<p>\u6548\u679c\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-style-default\"><img decoding=\"async\" src=\"https:\/\/liyanliangpublic.oss-cn-hongkong.aliyuncs.com\/img\/20201112200739.png\" alt=\"\"\/><\/figure>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u672c\u4e3b\u9898\u6f14\u793a\u5982\u4f55\u4f7f\u7528 Open XML SDK\u4e2d\u7684\u7c7b\u4ee5\u7f16\u7a0b\u65b9\u5f0f\u5f80\u6f14\u793a\u6587\u7a3f\u4e2d\u6307\u5b9a\u7684\u5e7b\u706f\u7247\u63d2\u5165\u8868\u683c\u3002 \u6548\u679c\uff1a<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[29],"tags":[31],"class_list":["post-249","post","type-post","status-publish","format-standard","hentry","category-c-2","tag-openxml"],"_links":{"self":[{"href":"http:\/\/liyanliang.net\/index.php\/wp-json\/wp\/v2\/posts\/249","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/liyanliang.net\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/liyanliang.net\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/liyanliang.net\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/liyanliang.net\/index.php\/wp-json\/wp\/v2\/comments?post=249"}],"version-history":[{"count":1,"href":"http:\/\/liyanliang.net\/index.php\/wp-json\/wp\/v2\/posts\/249\/revisions"}],"predecessor-version":[{"id":250,"href":"http:\/\/liyanliang.net\/index.php\/wp-json\/wp\/v2\/posts\/249\/revisions\/250"}],"wp:attachment":[{"href":"http:\/\/liyanliang.net\/index.php\/wp-json\/wp\/v2\/media?parent=249"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/liyanliang.net\/index.php\/wp-json\/wp\/v2\/categories?post=249"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/liyanliang.net\/index.php\/wp-json\/wp\/v2\/tags?post=249"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}