C#代码实现在PowerPoint演示文稿中插入表格
在 PowerPoint 中,表格是一种非常实用的工具,可以帮助你以清晰、简洁且直观的方式展示和整理数据。通过使用表格,你能够更有效地传达复杂信息,让观众更容易理解并记住重点内容。
本文将介绍如何使用 Spire.Presentation for .NET,在 C# 和 VB.NET 中向 PowerPoint 演示文稿插入表格。
安装 Spire.Presentation for .NET
在开始之前,您需要将 Spire.Presentation for .NET 安装包中的 DLL 文件添加为 .NET 项目的引用。您可以通过官方下载链接获取 DLL 文件,或直接通过 NuGet 进行安装。
PM> Install-Package Spire.Presentation
在 C# 和 VB.NET 中向 PowerPoint 演示文稿插入表格
您可以使用 ISlide.Shapes.AppendTable(float x, float y, double[] widths, double[] heights) 方法,在指定幻灯片上添加表格。
具体步骤如下
- 创建
Presentation类的实例。 - 通过
Presentation.LoadFromFile(string file)方法加载 PowerPoint 文件。 - 使用
Presentation.Slides[int index]属性获取指定的幻灯片。 - 定义两个
double数组(widths和heights),分别用于设置表格列数、列宽以及行数、行高。 - 调用
ISlide.Shapes.AppendTable(float x, float y, double[] widths, double[] heights)方法,在幻灯片指定位置添加具有指定行列数量和尺寸的表格。 - 使用一个二维字符串数组存储表格数据。
- 遍历该二维数组,并通过
ITable[int columnIndex, int rowIndex].TextFrame.Text属性,将数据填充到对应的单元格中。 - 将表格首行内容设置为居中对齐。
- 通过
ITable.StylePreset属性为表格应用内置样式。 - 最后,使用
Presentation.SaveToFile(string file, FileFormat fileFormat)方法保存演示文稿。
完整示例代码如下
using Spire.Presentation;
namespace InsertTable
{
internal class Program
{
static void Main(string[] args)
{
//初始化 Presentation 类的实例
Presentation presentation = new Presentation();
//加载 PowerPoint 演示文稿
presentation.LoadFromFile(@"Input.pptx");
//获取第一张幻灯片
ISlide slide = presentation.Slides[0];
//定义两个 double 数组 widths 和 heights,用于指定表格的列数、列宽以及行数、行高
double[] widths = new double[] { 100, 100, 150, 100, 100 };
double[] heights = new double[] { 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15 };
//在幻灯片的指定位置添加具有指定行列数量和尺寸的表格
ITable table = slide.Shapes.AppendTable(presentation.SlideSize.Size.Width / 2 - 275, 90, widths, heights);
//使用二维字符串数组存储表格数据
string[,] data = new string[,]{
{"Name", "Capital", "Continent", "Area", "Population"},
{"Venezuela", "Caracas", "South America", "912047", "19700000"},
{"Bolivia", "La Paz", "South America", "1098575", "7300000"},
{"Brazil", "Brasilia", "South America", "8511196", "150400000"},
{"Canada", "Ottawa", "North America", "9976147", "26500000"},
{"Chile", "Santiago", "South America", "756943", "13200000"},
{"Colombia", "Bogota", "South America", "1138907", "33000000"},
{"Cuba", "Havana", "North America", "114524", "10600000"},
{"Ecuador", "Quito", "South America", "455502", "10600000"},
{"Paraguay", "Asuncion", "South America", "406576", "4660000"},
{"Peru", "Lima", "South America", "1285215", "21600000"},
{"Jamaica", "Kingston", "North America", "11424", "2500000"},
{"Mexico", "Mexico City", "North America", "1967180", "88600000"}
};
//遍历字符串数组,并将数据填充到表格的每个单元格中
for (int i = 0; i < 13; i++)
for (int j = 0; j < 5; j++)
{
//为表格的每个单元格赋值
table[j, i].TextFrame.Text = data[i, j];
//设置字体名称和字体大小
table[j, i].TextFrame.Paragraphs[0].TextRanges[0].LatinFont = new TextFont("Times New Roman");
table[j, i].TextFrame.Paragraphs[0].TextRanges[0].FontHeight = 16;
}
//将表格第一行的内容设置为居中对齐
for (int i = 0; i < 5; i++)
{
table[i, 0].TextFrame.Paragraphs[0].Alignment = TextAlignmentType.Center;
}
//为表格应用内置样式
table.StylePreset = TableStylePreset.MediumStyle2Accent6;
//将演示文稿保存到文件
presentation.SaveToFile("InsertTable.pptx", FileFormat.Pptx2013);
presentation.Dispose();
}
}
}知识扩展
Spire.Presentation提供了丰富全面的API供程序员对PowerPoint表格进行处理。本文将介绍如何使用该组件创建表格,删除行和列,合并单元格,设置自定义格式和删除表格。
创建表格
//创建一个PowerPoint文档
Presentation ppt = new Presentation();
ppt.SlideSize.Type = SlideSizeType.Screen16x9;
//初始化一个ITable实例,并指定位置、行数和列数、行高和列宽
double[] widths = new double[] { 100, 100, 100, 100 };
double[] heights = new double[] { 15, 15, 15, 15};
ITable table = ppt.Slides[0].Shapes.AppendTable(80, 80, widths, heights);
//为表格设置内置格式
table.StylePreset = TableStylePreset.LightStyle1Accent2;
//声明并初始化一个String[,]数组
string[,] data = new string[,]
{
{"姓名","年龄","性别","工号" },
{"张三","28","男","0023" },
{"李四","30","男","0024" },
{"王五","26","女","0025" }
};
//将数组内容填充到表格
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
table[j, i].TextFrame.Text = data[i, j];
table[j, i].TextFrame.Paragraphs[0].TextRanges[0].LatinFont = new TextFont("Arial");
}
}
//保存文档
ppt.SaveToFile("创建表格.pptx", FileFormat.Pptx2013);
删除行和列
//初始化一个Presentation实例
Presentation ppt = new Presentation();
//加载一个PowerPoint文档
ppt.LoadFromFile("创建表格.pptx");
//获取第一张幻灯片上的表格
ITable table = null;
foreach (IShape shape in ppt.Slides[0].Shapes)
{
if (shape is ITable)
{
table = (ITable)shape;
//删除第三列
table.ColumnsList.RemoveAt(2, false);
//删除第四行
table.TableRows.RemoveAt(3, false);
}
}
//保存文档
ppt.SaveToFile("删除行与列.pptx",FileFormat.Pptx2013);
合并单元格、设置单元格格式
//创建一个PowerPoint文档
Presentation ppt = new Presentation();
ppt.SlideSize.Type = SlideSizeType.Screen16x9;
//初始化一个ITable实例,并指定位置、行数和列数、行高和列宽
double[] widths = new double[] { 100, 100, 100, 100 };
double[] heights = new double[] { 15, 15, 15, 15 };
ITable table = ppt.Slides[0].Shapes.AppendTable(80, 80, widths, heights);
//声明并初始化一个String[,]数组
string[,] data = new string[,]
{
{"订单及支付情况","","","" },
{"订单号","日期","客户","是否支付" },
{"1021","2017/5/1","阳光地产","是" },
{"1022","2017/5/2","","否" }
};
//将数组内容填充到表格
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
table[j, i].TextFrame.Text = data[i, j];
table[j, i].TextFrame.Paragraphs[0].TextRanges[0].LatinFont = new TextFont("Arial");
}
}
//将表格的默认格式设为空(自定义表格格式须清除表格默认格式)
table.StylePreset = TableStylePreset.None;
//合并第一行的单元格
for (int i = 0; i < table.ColumnsList.Count-1; i++)
{
Cell cell1 = table[i, 0];
Cell cell2 = table[i+1, 0];
table.MergeCells(cell1, cell2, true);
}
//在合并后新的单元格设置文字水平对齐方式和背景色
Cell newCell1 = table[0, 0];
newCell1.TextFrame.Paragraphs[0].Alignment = TextAlignmentType.Center;
newCell1.FillFormat.FillType = FillFormatType.Solid;
newCell1.FillFormat.SolidColor.Color = System.Drawing.Color.Gray;
//垂直合并单元格[2,2]和单元格[2,3]
Cell cell3 = table[2, 2];
Cell cell4 = table[2, 3];
table.MergeCells(cell3, cell4, true);
//在合并后新的单元格设置文字垂直对齐方式
Cell newCell2 = table[2, 2];
newCell2.TextAnchorType = TextAnchorType.Center;
//为单元格[3,2]、[3,3]设置背景色
Cell cell5 = table[3, 2];
Cell cell6 = table[3, 3];
cell5.FillFormat.FillType = FillFormatType.Solid;
cell6.FillFormat.FillType = FillFormatType.Solid;
cell5.FillFormat.SolidColor.Color = System.Drawing.Color.Green;
cell6.FillFormat.SolidColor.Color = System.Drawing.Color.Red;
//保存文档
ppt.SaveToFile("操作单元格.pptx", FileFormat.Pptx2013);
删除表格
//初始化一个Presentation实例
Presentation ppt = new Presentation();
//加载一个PowerPoint文档
ppt.LoadFromFile("创建表格.pptx");
//初始化一个List对象,元素类型为IShape
List<IShape> tableShapes= new List<IShape>();
//获取第一张幻灯片上所有的表格图形并添加到List
foreach (IShape shape in ppt.Slides[0].Shapes)
{
if (shape is ITable)
{
tableShapes.Add(shape);
}
}
//从幻灯片删除第一个表格图形
ppt.Slides[0].Shapes.Remove(tableShapes[0]);
//保存文档
ppt.SaveToFile("删除表格.pptx", FileFormat.Pptx2013);
到此这篇关于C#代码实现在PowerPoint演示文稿中插入表格的文章就介绍到这了,更多相关C# PowerPoint插入表格内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!


最新评论