使用OpenXML SDK往PPT中指定的幻灯片插入表格
本主题演示如何使用 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();
}
效果:





