Open XML SDKについてまとめてみた。
目次
Open XML SDK とは?ダウンロードサイト
Open XML SDK は、Office Open XML ファイル (Word、Excel、PowerPoint) を作ったり編集したりを比較的簡単に行うことができるSDKです。
マイクロソフトがGitHubでオープンソースとして公開しています。
日本語のダウンロードサイトや公式サイトは存在しないようです。
Open XML SDKの公式サイト
・https://docs.microsoft.com/en-us/office/open-xml/open-xml-sdk
Open XML SDK 2.5 for Microsoft Office の msiインストーラーのダウンロード
・https://www.microsoft.com/en-us/download/details.aspx?id=30425
GitHubのOpen XML SDKサイト
・https://github.com/OfficeDev/Open-XML-SDK
Visual StudioでOpen XML SDKを使うなら、nugetで取ってくるのが一番良いと思います。
Open XML SDK for Office と COM の違い
Open XML SDK は、プログラム的にWord、Excel、PowerPoint を作ったり編集したりすることが可能です。
Word、Excel、PowerPointがインストールされていれば、COMを使うことで、プログラム的にWord、Excel、PowerPoint を作ったり編集したりすることが可能です。
Open XML SDK for Office と COM の違いですが、Open XML SDK for Officeの場合、Word、Excel、PowerPointがインストールされていなくても、それらのファイルを作成、更新、削除することが出来ることです。
Open XML SDK使い方
パワポ (PowerPoint) の作成、書き込み、削除
・https://docs.microsoft.com/en-us/office/open-xml/presentations
エクセル (Excel) の作成、書き込み、削除
・https://docs.microsoft.com/en-us/office/open-xml/spreadsheets
ワード (Word) の作成、書き込み、削除
・https://docs.microsoft.com/en-us/office/open-xml/word-processing
Open XML SDKでパワポを作成するC#サンプルコード
Open XML SDKを使ってパワポを作るサンプルは以下の通りです。
C#のサンプルコード
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 |
using System; using System.Collections.Generic; using System.Linq; using System.Text; using DocumentFormat.OpenXml; using DocumentFormat.OpenXml.Drawing; using DocumentFormat.OpenXml.Packaging; using DocumentFormat.OpenXml.Presentation; using P = DocumentFormat.OpenXml.Presentation; using D = DocumentFormat.OpenXml.Drawing; namespace CreatePresentationDocument { class Program { static void Main(string[] args) { string filepath = @"C:\Users\username\Documents\PresentationFromFilename.pptx"; CreatePresentation(filepath); } public static void CreatePresentation(string filepath) { // Create a presentation at a specified file path. The presentation document type is pptx, by default. PresentationDocument presentationDoc = PresentationDocument.Create(filepath, PresentationDocumentType.Presentation); PresentationPart presentationPart = presentationDoc.AddPresentationPart(); presentationPart.Presentation = new Presentation(); CreatePresentationParts(presentationPart); //Close the presentation handle presentationDoc.Close(); } private static void CreatePresentationParts(PresentationPart presentationPart) { SlideMasterIdList slideMasterIdList1 = new SlideMasterIdList(new SlideMasterId() { Id = (UInt32Value)2147483648U, RelationshipId = "rId1" }); SlideIdList slideIdList1 = new SlideIdList(new SlideId() { Id = (UInt32Value)256U, RelationshipId = "rId2" }); SlideSize slideSize1 = new SlideSize() { Cx = 9144000, Cy = 6858000, Type = SlideSizeValues.Screen4x3 }; NotesSize notesSize1 = new NotesSize() { Cx = 6858000, Cy = 9144000 }; DefaultTextStyle defaultTextStyle1 = new DefaultTextStyle(); presentationPart.Presentation.Append(slideMasterIdList1, slideIdList1, slideSize1, notesSize1, defaultTextStyle1); SlidePart slidePart1; SlideLayoutPart slideLayoutPart1; SlideMasterPart slideMasterPart1; ThemePart themePart1; slidePart1 = CreateSlidePart(presentationPart); slideLayoutPart1 = CreateSlideLayoutPart(slidePart1); slideMasterPart1 = CreateSlideMasterPart(slideLayoutPart1); themePart1 = CreateTheme(slideMasterPart1); slideMasterPart1.AddPart(slideLayoutPart1, "rId1"); presentationPart.AddPart(slideMasterPart1, "rId1"); presentationPart.AddPart(themePart1, "rId5"); } private static SlidePart CreateSlidePart(PresentationPart presentationPart) { SlidePart slidePart1 = presentationPart.AddNewPart<SlidePart>("rId2"); slidePart1.Slide = new Slide( new CommonSlideData( new ShapeTree( new P.NonVisualGroupShapeProperties( new P.NonVisualDrawingProperties() { Id = (UInt32Value)1U, Name = "" }, new P.NonVisualGroupShapeDrawingProperties(), new ApplicationNonVisualDrawingProperties()), new GroupShapeProperties(new TransformGroup()), new P.Shape( new P.NonVisualShapeProperties( new P.NonVisualDrawingProperties() { Id = (UInt32Value)2U, Name = "Title 1" }, new P.NonVisualShapeDrawingProperties(new ShapeLocks() { NoGrouping = true }), new ApplicationNonVisualDrawingProperties(new PlaceholderShape())), new P.ShapeProperties(), new P.TextBody( new BodyProperties(), new ListStyle(), new Paragraph(new EndParagraphRunProperties() { Language = "en-US" }))))), new ColorMapOverride(new MasterColorMapping())); return slidePart1; } private static SlideLayoutPart CreateSlideLayoutPart(SlidePart slidePart1) { SlideLayoutPart slideLayoutPart1 = slidePart1.AddNewPart<SlideLayoutPart>("rId1"); SlideLayout slideLayout = new SlideLayout( new CommonSlideData(new ShapeTree( new P.NonVisualGroupShapeProperties( new P.NonVisualDrawingProperties() { Id = (UInt32Value)1U, Name = "" }, new P.NonVisualGroupShapeDrawingProperties(), new ApplicationNonVisualDrawingProperties()), new GroupShapeProperties(new TransformGroup()), new P.Shape( new P.NonVisualShapeProperties( new P.NonVisualDrawingProperties() { Id = (UInt32Value)2U, Name = "" }, new P.NonVisualShapeDrawingProperties(new ShapeLocks() { NoGrouping = true }), new ApplicationNonVisualDrawingProperties(new PlaceholderShape())), new P.ShapeProperties(), new P.TextBody( new BodyProperties(), new ListStyle(), new Paragraph(new EndParagraphRunProperties()))))), new ColorMapOverride(new MasterColorMapping())); slideLayoutPart1.SlideLayout = slideLayout; return slideLayoutPart1; } private static SlideMasterPart CreateSlideMasterPart(SlideLayoutPart slideLayoutPart1) { SlideMasterPart slideMasterPart1 = slideLayoutPart1.AddNewPart<SlideMasterPart>("rId1"); SlideMaster slideMaster = new SlideMaster( new CommonSlideData(new ShapeTree( new P.NonVisualGroupShapeProperties( new P.NonVisualDrawingProperties() { Id = (UInt32Value)1U, Name = "" }, new P.NonVisualGroupShapeDrawingProperties(), new ApplicationNonVisualDrawingProperties()), new GroupShapeProperties(new TransformGroup()), new P.Shape( new P.NonVisualShapeProperties( new P.NonVisualDrawingProperties() { Id = (UInt32Value)2U, Name = "Title Placeholder 1" }, new P.NonVisualShapeDrawingProperties(new ShapeLocks() { NoGrouping = true }), new ApplicationNonVisualDrawingProperties(new PlaceholderShape() { Type = PlaceholderValues.Title })), new P.ShapeProperties(), new P.TextBody( new BodyProperties(), new ListStyle(), new Paragraph())))), new P.ColorMap() { Background1 = D.ColorSchemeIndexValues.Light1, Text1 = D.ColorSchemeIndexValues.Dark1, Background2 = D.ColorSchemeIndexValues.Light2, Text2 = D.ColorSchemeIndexValues.Dark2, Accent1 = D.ColorSchemeIndexValues.Accent1, Accent2 = D.ColorSchemeIndexValues.Accent2, Accent3 = D.ColorSchemeIndexValues.Accent3, Accent4 = D.ColorSchemeIndexValues.Accent4, Accent5 = D.ColorSchemeIndexValues.Accent5, Accent6 = D.ColorSchemeIndexValues.Accent6, Hyperlink = D.ColorSchemeIndexValues.Hyperlink, FollowedHyperlink = D.ColorSchemeIndexValues.FollowedHyperlink }, new SlideLayoutIdList(new SlideLayoutId() { Id = (UInt32Value)2147483649U, RelationshipId = "rId1" }), new TextStyles(new TitleStyle(), new BodyStyle(), new OtherStyle())); slideMasterPart1.SlideMaster = slideMaster; return slideMasterPart1; } private static ThemePart CreateTheme(SlideMasterPart slideMasterPart1) { ThemePart themePart1 = slideMasterPart1.AddNewPart<ThemePart>("rId5"); D.Theme theme1 = new D.Theme() { Name = "Office Theme" }; D.ThemeElements themeElements1 = new D.ThemeElements( new D.ColorScheme( new D.Dark1Color(new D.SystemColor() { Val = D.SystemColorValues.WindowText, LastColor = "000000" }), new D.Light1Color(new D.SystemColor() { Val = D.SystemColorValues.Window, LastColor = "FFFFFF" }), new D.Dark2Color(new D.RgbColorModelHex() { Val = "1F497D" }), new D.Light2Color(new D.RgbColorModelHex() { Val = "EEECE1" }), new D.Accent1Color(new D.RgbColorModelHex() { Val = "4F81BD" }), new D.Accent2Color(new D.RgbColorModelHex() { Val = "C0504D" }), new D.Accent3Color(new D.RgbColorModelHex() { Val = "9BBB59" }), new D.Accent4Color(new D.RgbColorModelHex() { Val = "8064A2" }), new D.Accent5Color(new D.RgbColorModelHex() { Val = "4BACC6" }), new D.Accent6Color(new D.RgbColorModelHex() { Val = "F79646" }), new D.Hyperlink(new D.RgbColorModelHex() { Val = "0000FF" }), new D.FollowedHyperlinkColor(new D.RgbColorModelHex() { Val = "800080" })) { Name = "Office" }, new D.FontScheme( new D.MajorFont( new D.LatinFont() { Typeface = "Calibri" }, new D.EastAsianFont() { Typeface = "" }, new D.ComplexScriptFont() { Typeface = "" }), new D.MinorFont( new D.LatinFont() { Typeface = "Calibri" }, new D.EastAsianFont() { Typeface = "" }, new D.ComplexScriptFont() { Typeface = "" })) { Name = "Office" }, new D.FormatScheme( new D.FillStyleList( new D.SolidFill(new D.SchemeColor() { Val = D.SchemeColorValues.PhColor }), new D.GradientFill( new D.GradientStopList( new D.GradientStop(new D.SchemeColor(new D.Tint() { Val = 50000 }, new D.SaturationModulation() { Val = 300000 }) { Val = D.SchemeColorValues.PhColor }) { Position = 0 }, new D.GradientStop(new D.SchemeColor(new D.Tint() { Val = 37000 }, new D.SaturationModulation() { Val = 300000 }) { Val = D.SchemeColorValues.PhColor }) { Position = 35000 }, new D.GradientStop(new D.SchemeColor(new D.Tint() { Val = 15000 }, new D.SaturationModulation() { Val = 350000 }) { Val = D.SchemeColorValues.PhColor }) { Position = 100000 } ), new D.LinearGradientFill() { Angle = 16200000, Scaled = true }), new D.NoFill(), new D.PatternFill(), new D.GroupFill()), new D.LineStyleList( new D.Outline( new D.SolidFill( new D.SchemeColor( new D.Shade() { Val = 95000 }, new D.SaturationModulation() { Val = 105000 }) { Val = D.SchemeColorValues.PhColor }), new D.PresetDash() { Val = D.PresetLineDashValues.Solid }) { Width = 9525, CapType = D.LineCapValues.Flat, CompoundLineType = D.CompoundLineValues.Single, Alignment = D.PenAlignmentValues.Center }, new D.Outline( new D.SolidFill( new D.SchemeColor( new D.Shade() { Val = 95000 }, new D.SaturationModulation() { Val = 105000 }) { Val = D.SchemeColorValues.PhColor }), new D.PresetDash() { Val = D.PresetLineDashValues.Solid }) { Width = 9525, CapType = D.LineCapValues.Flat, CompoundLineType = D.CompoundLineValues.Single, Alignment = D.PenAlignmentValues.Center }, new D.Outline( new D.SolidFill( new D.SchemeColor( new D.Shade() { Val = 95000 }, new D.SaturationModulation() { Val = 105000 }) { Val = D.SchemeColorValues.PhColor }), new D.PresetDash() { Val = D.PresetLineDashValues.Solid }) { Width = 9525, CapType = D.LineCapValues.Flat, CompoundLineType = D.CompoundLineValues.Single, Alignment = D.PenAlignmentValues.Center }), new D.EffectStyleList( new D.EffectStyle( new D.EffectList( new D.OuterShadow( new D.RgbColorModelHex( new D.Alpha() { Val = 38000 }) { Val = "000000" }) { BlurRadius = 40000L, Distance = 20000L, Direction = 5400000, RotateWithShape = false })), new D.EffectStyle( new D.EffectList( new D.OuterShadow( new D.RgbColorModelHex( new D.Alpha() { Val = 38000 }) { Val = "000000" }) { BlurRadius = 40000L, Distance = 20000L, Direction = 5400000, RotateWithShape = false })), new D.EffectStyle( new D.EffectList( new D.OuterShadow( new D.RgbColorModelHex( new D.Alpha() { Val = 38000 }) { Val = "000000" }) { BlurRadius = 40000L, Distance = 20000L, Direction = 5400000, RotateWithShape = false }))), new D.BackgroundFillStyleList( new D.SolidFill(new D.SchemeColor() { Val = D.SchemeColorValues.PhColor }), new D.GradientFill( new D.GradientStopList( new D.GradientStop( new D.SchemeColor(new D.Tint() { Val = 50000 }, new D.SaturationModulation() { Val = 300000 }) { Val = D.SchemeColorValues.PhColor }) { Position = 0 }, new D.GradientStop( new D.SchemeColor(new D.Tint() { Val = 50000 }, new D.SaturationModulation() { Val = 300000 }) { Val = D.SchemeColorValues.PhColor }) { Position = 0 }, new D.GradientStop( new D.SchemeColor(new D.Tint() { Val = 50000 }, new D.SaturationModulation() { Val = 300000 }) { Val = D.SchemeColorValues.PhColor }) { Position = 0 }), new D.LinearGradientFill() { Angle = 16200000, Scaled = true }), new D.GradientFill( new D.GradientStopList( new D.GradientStop( new D.SchemeColor(new D.Tint() { Val = 50000 }, new D.SaturationModulation() { Val = 300000 }) { Val = D.SchemeColorValues.PhColor }) { Position = 0 }, new D.GradientStop( new D.SchemeColor(new D.Tint() { Val = 50000 }, new D.SaturationModulation() { Val = 300000 }) { Val = D.SchemeColorValues.PhColor }) { Position = 0 }), new D.LinearGradientFill() { Angle = 16200000, Scaled = true }))) { Name = "Office" }); theme1.Append(themeElements1); theme1.Append(new D.ObjectDefaults()); theme1.Append(new D.ExtraColorSchemeList()); themePart1.Theme = theme1; return themePart1; } } } |
コメント