I've seen and used the code to clone a table in a presentation from an existing table in a presentation. I'm trying to use the 2.0 SDK to add the table without a template - but am running into problems.
I can create the table, grid, cells, etc, objects. However, I can't seem to get the correct object to add to the slide or presentation. Once I can add it - I should be able to get a reference and add to the ShapeTree.
Comparing the output XML appears to be correct (to my untrained eye):
SDK generated XML:
<a:graphicFrame xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main"> <a:nvGraphicFramePr> <a:cNvPr id="4" name="Content Placeholder 3" /> <a:cNvGraphicFramePr> <a:graphicFrameLocks noGrp="1" /> </a:cNvGraphicFramePr> <p:nvPr> <p:ph type="tbl" idx="1" /> </p:nvPr> </a:nvGraphicFramePr> <p:xfrm> <a:off x="612775" y="1600200" /> <a:ext cx="8153400" cy="4450080" /> </p:xfrm> <a:graphic> <a:graphicData uri="http://schemas.openxmlformats.org/drawingml/2006/table"> <a:tbl> <a:tblPr bandRow="1"> <a:tableStyleId>5c22544a-7ee6-4342-b048-85bdc9fd1c3a</a:tableStyleId> </a:tblPr> <a:tblGrid> <a:gridCol w="1630680" /> <a:gridCol w="1630680" /> <a:gridCol w="1630680" /> <a:gridCol w="1630680" /> <a:gridCol w="1630680" /> </a:tblGrid> <a:tr> <a:tc> <a:txBody> <a:bodyPr /> <a:p> <a:r> <a:t>aa</a:t> <a:rPr dirty="0" err="1" smtClean="0" /> </a:r> </a:p> </a:txBody> <a:tcPr /> </a:tc> <a:tc>
PowerPoint generated XML:
<p:graphicFrame> <p:nvGraphicFramePr> <p:cNvPr id="4" name="Content Placeholder 3"/> <p:cNvGraphicFramePr> <a:graphicFrameLocks noGrp="1"/> </p:cNvGraphicFramePr> <p:nvPr> <p:ph sz="quarter" idx="1"/> </p:nvPr> </p:nvGraphicFramePr> <p:xfrm> <a:off x="612775" y="1600200"/> <a:ext cx="8153400" cy="4450080"/> </p:xfrm> <a:graphic> <a:graphicData uri="http://schemas.openxmlformats.org/drawingml/2006/table"> <a:tbl> <a:tblPr firstRow="1" bandRow="1"> <a:tableStyleId>{5C22544A-7EE6-4342-B048-85BDC9FD1C3A}</a:tableStyleId> </a:tblPr> <a:tblGrid> <a:gridCol w="1630680"/> <a:gridCol w="1630680"/> <a:gridCol w="1630680"/> <a:gridCol w="1630680"/> <a:gridCol w="1630680"/> </a:tblGrid> <a:tr h="370840"> <a:tc> <a:txBody> <a:bodyPr/> <a:lstStyle/> <a:p> <a:r> <a:rPr lang="en-US" dirty="0" err="1" smtClean="0"/> <a:t>Sdfsdf</a:t> </a:r> <a:endParaRPr lang="en-US" dirty="0"/> </a:p> </a:txBody> <a:tcPr/> </a:tc> <a:tc>
The code is not much... but it's missing something that causes the output XML to be invalid.
var guid = new Guid("5C22544A-7EE6-4342-B048-85BDC9FD1C3A"); var slide = presentationPart.PresentationPart.SlideParts.Last(); var tableProperties = new TableProperties { BandRow = new BooleanValue(true) }; tableProperties.Append(new TableStyleId(guid.ToString())); var tableGrid = new TableGrid(); for (var i = 0; i < 5; i++) tableGrid.Append(new GridColumn { Width = 1630680 }); var table = new Table(tableProperties, tableGrid); for (var i = 0; i < 5; i++) { var tableRow = new TableRow(); tableRow.Append(CreateTextCell("aa")); tableRow.Append(CreateTextCell("bb")); tableRow.Append(CreateTextCell("cc")); tableRow.Append(CreateTextCell("dd")); tableRow.Append(CreateTextCell("ee")); table.Append(tableRow); } var graphicFrameDrawingProperties = new NonVisualGraphicFrameDrawingProperties {GraphicFrameLocks = new GraphicFrameLocks {NoGrouping = new BooleanValue(true)}}; var appNonVisualDrawingProperties = new AppNonVisualDrawingProperties(new PlaceholderShape {Index = 1, Type = PlaceholderValues.Table}); var extents = new Extents { Cx = 8153400, Cy = 4450080 }; var transform = new Transform( new Offset { X = 612775, Y = 1600200 }, extents); var graphicFrameProperties = new NonVisualGraphicFrameProperties ( new NonVisualDrawingProperties { Id = 4, Name = "Content Placeholder 3" }, graphicFrameDrawingProperties, appNonVisualDrawingProperties ); var graphicFrame = new GraphicFrame(graphicFrameProperties, transform) { Graphic = new Graphic( new GraphicData( tbl) { Uri = "http://schemas.openxmlformats.org/drawingml/2006/table" }) }; slide.Slide.CommonSlideData.ShapeTree.Append(graphicFrame); private static TableCell CreateTextCell(string text) { return new TableCell( new TextBody( new BodyProperties(), new Paragraph( new Run( new Text(text), new RunProperties { Dirty = new BooleanValue(false), SmartTagClean = new BooleanValue(false), SpellingError = new BooleanValue(true) }))), new TableCellProperties()); }
tableProperties.Append(
tableGrid.Append(new GridColumn { Width = 1630680 });
var table = new Table(tableProperties, tableGrid);
for (var i = 0; i < 5; i++)
{
var tableRow = new TableRow();
tableRow.Append(CreateTextCell("aa"));
tableRow.Append(CreateTextCell("bb"));
tableRow.Append(CreateTextCell("cc"));
tableRow.Append(CreateTextCell("dd"));
tableRow.Append(CreateTextCell("ee"));
table.Append(tableRow);
}
{GraphicFrameLocks =
Cx = 8153400,
Cy = 4450080
};
X = 612775,
Y = 1600200
}, extents);
(
Id = 4,
Name =
},
graphicFrameDrawingProperties,
appNonVisualDrawingProperties
);
Graphic =
tbl)
Uri =
})
slide.Slide.CommonSlideData.ShapeTree.Append(graphicFrame);
Dirty =
SmartTagClean =
SpellingError =
}))),
internal class TableHelper { private static int callCount; public static GraphicFrame AddTable(DataTable dt) { var tableProperties = new TableProperties { BandRow = new BooleanValue(true), FirstRow = new BooleanValue(true) }; TableStyleId tableStyleId; // alternative table colors for better appearance switch (callCount++ % 4) { case 0: tableStyleId = new TableStyleId("{3B4B98B0-60AC-42C2-AFA5-B58CD77FA1E5}"); break; case 1: tableStyleId = new TableStyleId("{C083E6E3-FA7D-4D7B-A595-EF9225AFEA82}"); break; case 2: tableStyleId = new TableStyleId("{0E3FDE45-AF77-4B5C-9715-49D594BDF05E}"); break; default: tableStyleId = new TableStyleId("{D27102A9-8310-4765-A935-A1911B00CA55}"); break; } tableProperties.Append(tableStyleId); // make first column larger than the rest var tableGrid = new TableGrid(); var width = 8153400 / (dt.Columns.Count + 1); for (var i = 0; i tableGrid.Append(new GridColumn { Width = i == 0 ? width * 2 : width }); var table = new Table(tableProperties, tableGrid); var tableHeader = new TableRow { Height = 370840L }; foreach (DataColumn dc in dt.Columns) tableHeader.Append(CreateCenteredTableCellWithText(dc.ColumnName)); table.Append(tableHeader); foreach (DataRow dataRow in dt.Rows) { var tableRow = new TableRow { Height = 370840L }; foreach (DataColumn dataColumn in dt.Columns) tableRow.Append(CreateTableCellWithText(dataRow[dataColumn].ToString())); table.Append(tableRow); } return CreateGraphicFrame(table); } private static TableCell CreateTableCellWithText(string text) { return new TableCell( new TextBody( new BodyProperties(), new ListStyle(), new Paragraph( new Run( new RunProperties { FontSize = 1200 }, new Text(text)), new EndParagraphRunProperties { FontSize = 1200 })), new TableCellProperties()); } private static TableCell CreateCenteredTableCellWithText(string text) { return new TableCell( new TextBody( new BodyProperties(), new ListStyle(), new Paragraph( new ParagraphProperties { Alignment = TextAlignValues.Center }, new Run( new RunProperties { FontSize = 1200 }, new Text(text)), new EndParagraphRunProperties { FontSize = 1200 })), new TableCellProperties { Anchor = TextAnchoringValues.Center } ); } private static GraphicFrame CreateGraphicFrame(OpenXmlElement tbl) { return new GraphicFrame( new NonVisualGraphicFrameProperties( new NonVisualDrawingProperties { Id = (UInt32Value)3U, Name = "Content Placeholder 3" }, new NonVisualGraphicFrameDrawingProperties( new GraphicFrameLocks { NoGrouping = true }), new AppNonVisualDrawingProperties( new PlaceholderShape { Size = PlaceholderSizeValues.Quarter, Type = PlaceholderValues.Table, Index = (UInt32Value)1U })), new Transform( new Offset { X = 612775L, Y = 1600200L }, new Extents { Cx = 8153400L, Cy = 741680L }), new Graphic( new GraphicData(tbl) { Uri = "http://schemas.openxmlformats.org/drawingml/2006/table" }) ); } }