添加表格

ShapeCollection.AddTable 方法

创建一个新表格并将其添加到集合的末尾。

public ITable AddTable(float x, float y, double[] columnWidths, double[] rowHeights)  
参数类型描述
x单精度浮点数形状框架左侧的 X 坐标。
y单精度浮点数形状框架顶部的 Y 坐标。
columnWidths双精度浮点数数组表格中表示列宽的双精度浮点数数组。
rowHeights双精度浮点数数组表格中表示行高的双精度浮点数数组。

返回值

创建的表格对象。

示例

以下示例演示了如何在 PowerPoint 演示文稿中添加表格。

[C#]  
// 实例化表示 PPTX 文件的 Presentation 类  
using (Presentation pres = new Presentation()){  
	// 访问第一张幻灯片  
	ISlide sld = pres.Slides[0];  
	// 定义带宽的列和带高的行  
	double[] dblCols = { 50, 50, 50 };  
	double[] dblRows = { 50, 30, 30, 30, 30 };  
	// 向幻灯片添加表格形状  
	ITable tbl = sld.Shapes.AddTable(100, 50, dblCols, dblRows);  
	// 为每个单元格设置边框格式  
	for (int row = 0; row < tbl.Rows.Count; row++)  
	{  
		for (int cell = 0; cell < tbl.Rows[row].Count; cell++)  
		{  
			tbl.Rows[row][cell].CellFormat.BorderTop.FillFormat.FillType = FillType.Solid;  
			tbl.Rows[row][cell].CellFormat.BorderTop.FillFormat.SolidFillColor.Color = Color.Red;  
			tbl.Rows[row][cell].CellFormat.BorderTop.Width = 5;  
			tbl.Rows[row][cell].CellFormat.BorderBottom.FillFormat.FillType = (FillType.Solid);  
			tbl.Rows[row][cell].CellFormat.BorderBottom.FillFormat.SolidFillColor.Color= Color.Red;  
			tbl.Rows[row][cell].CellFormat.BorderBottom.Width =5;  
			tbl.Rows[row][cell].CellFormat.BorderLeft.FillFormat.FillType = FillType.Solid;  
			tbl.Rows[row][cell].CellFormat.BorderLeft.FillFormat.SolidFillColor.Color =Color.Red;  
			tbl.Rows[row][cell].CellFormat.BorderLeft.Width = 5;  
			tbl.Rows[row][cell].CellFormat.BorderRight.FillFormat.FillType = FillType.Solid;  
			tbl.Rows[row][cell].CellFormat.BorderRight.FillFormat.SolidFillColor.Color = Color.Red;  
			tbl.Rows[row][cell].CellFormat.BorderRight.Width = 5;  
		}  
	}  
	// 合并第 1 行的单元格 1 和 2  
	tbl.MergeCells(tbl.Rows[0][0], tbl.Rows[1][1], false);  
	// 向合并的单元格添加文本  
	tbl.Rows[0][0].TextFrame.Text = "合并单元格";  
	// 将 PPTX 保存到磁盘  
	pres.Save("table.pptx", SaveFormat.Pptx);  
}  

另请参见