AddTable

ShapeCollection.AddTable 方法

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

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

返回值

创建的表对象。

示例

以下示例演示如何在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);
}

另请参阅