Free Spire.Presentation for .NET 3.3 (社区版)
Visual Studio
在E-iceblue官网或者Nuget网站上下载Free Spire.Presentation for .NET的安装包后,注意在编辑代码时,添加引用Spire.Presentation.dll到程序。dll文件可在安装路径下的Bin文件夹中获取。
using Spire.Presentation;namespace InsertTableToPPT{ class Program { static void Main(string[] args) { //创建一个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,15}; ITable table = ppt.Slides[0].Shapes.AppendTable(80, 80, widths, heights); //为表格设置内置格式 table.StylePreset = TableStylePreset.LightStyle1Accent3; //声明并初始化一个String[,]数组 string[,] data = new string[,] { {'排名','国家','月出口','月进口'}, {'1','中国','18270','18270'}, {'2','韩国','18105','18105'}, {'3','日本','17987','17987'}, {'4','新加坡','17790','17790'}, }; //将数组内容填充到表格 for (int i = 0; i < 5; 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.Pptx2010); System.Diagnostics.Process.Start('创建表格.pptx'); } }}
代码完成后,调试运行程序,生成文档。如下图: