C#代碼實(shí)現(xiàn)在PowerPoint演示文稿中插入表格
在 PowerPoint 中,表格是一種非常實(shí)用的工具,可以幫助你以清晰、簡(jiǎn)潔且直觀的方式展示和整理數(shù)據(jù)。通過使用表格,你能夠更有效地傳達(dá)復(fù)雜信息,讓觀眾更容易理解并記住重點(diǎn)內(nèi)容。
本文將介紹如何使用 Spire.Presentation for .NET,在 C# 和 VB.NET 中向 PowerPoint 演示文稿插入表格。
安裝 Spire.Presentation for .NET
在開始之前,您需要將 Spire.Presentation for .NET 安裝包中的 DLL 文件添加為 .NET 項(xiàng)目的引用。您可以通過官方下載鏈接獲取 DLL 文件,或直接通過 NuGet 進(jìn)行安裝。
PM> Install-Package Spire.Presentation
在 C# 和 VB.NET 中向 PowerPoint 演示文稿插入表格
您可以使用 ISlide.Shapes.AppendTable(float x, float y, double[] widths, double[] heights) 方法,在指定幻燈片上添加表格。
具體步驟如下
- 創(chuàng)建
Presentation類的實(shí)例。 - 通過
Presentation.LoadFromFile(string file)方法加載 PowerPoint 文件。 - 使用
Presentation.Slides[int index]屬性獲取指定的幻燈片。 - 定義兩個(gè)
double數(shù)組(widths和heights),分別用于設(shè)置表格列數(shù)、列寬以及行數(shù)、行高。 - 調(diào)用
ISlide.Shapes.AppendTable(float x, float y, double[] widths, double[] heights)方法,在幻燈片指定位置添加具有指定行列數(shù)量和尺寸的表格。 - 使用一個(gè)二維字符串?dāng)?shù)組存儲(chǔ)表格數(shù)據(jù)。
- 遍歷該二維數(shù)組,并通過
ITable[int columnIndex, int rowIndex].TextFrame.Text屬性,將數(shù)據(jù)填充到對(duì)應(yīng)的單元格中。 - 將表格首行內(nèi)容設(shè)置為居中對(duì)齊。
- 通過
ITable.StylePreset屬性為表格應(yīng)用內(nèi)置樣式。 - 最后,使用
Presentation.SaveToFile(string file, FileFormat fileFormat)方法保存演示文稿。
完整示例代碼如下
using Spire.Presentation;
namespace InsertTable
{
internal class Program
{
static void Main(string[] args)
{
//初始化 Presentation 類的實(shí)例
Presentation presentation = new Presentation();
//加載 PowerPoint 演示文稿
presentation.LoadFromFile(@"Input.pptx");
//獲取第一張幻燈片
ISlide slide = presentation.Slides[0];
//定義兩個(gè) double 數(shù)組 widths 和 heights,用于指定表格的列數(shù)、列寬以及行數(shù)、行高
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 };
//在幻燈片的指定位置添加具有指定行列數(shù)量和尺寸的表格
ITable table = slide.Shapes.AppendTable(presentation.SlideSize.Size.Width / 2 - 275, 90, widths, heights);
//使用二維字符串?dāng)?shù)組存儲(chǔ)表格數(shù)據(jù)
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"}
};
//遍歷字符串?dāng)?shù)組,并將數(shù)據(jù)填充到表格的每個(gè)單元格中
for (int i = 0; i < 13; i++)
for (int j = 0; j < 5; j++)
{
//為表格的每個(gè)單元格賦值
table[j, i].TextFrame.Text = data[i, j];
//設(shè)置字體名稱和字體大小
table[j, i].TextFrame.Paragraphs[0].TextRanges[0].LatinFont = new TextFont("Times New Roman");
table[j, i].TextFrame.Paragraphs[0].TextRanges[0].FontHeight = 16;
}
//將表格第一行的內(nèi)容設(shè)置為居中對(duì)齊
for (int i = 0; i < 5; i++)
{
table[i, 0].TextFrame.Paragraphs[0].Alignment = TextAlignmentType.Center;
}
//為表格應(yīng)用內(nèi)置樣式
table.StylePreset = TableStylePreset.MediumStyle2Accent6;
//將演示文稿保存到文件
presentation.SaveToFile("InsertTable.pptx", FileFormat.Pptx2013);
presentation.Dispose();
}
}
}知識(shí)擴(kuò)展
Spire.Presentation提供了豐富全面的API供程序員對(duì)PowerPoint表格進(jìn)行處理。本文將介紹如何使用該組件創(chuàng)建表格,刪除行和列,合并單元格,設(shè)置自定義格式和刪除表格。
創(chuàng)建表格
//創(chuàng)建一個(gè)PowerPoint文檔
Presentation ppt = new Presentation();
ppt.SlideSize.Type = SlideSizeType.Screen16x9;
//初始化一個(gè)ITable實(shí)例,并指定位置、行數(shù)和列數(shù)、行高和列寬
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);
//為表格設(shè)置內(nèi)置格式
table.StylePreset = TableStylePreset.LightStyle1Accent2;
//聲明并初始化一個(gè)String[,]數(shù)組
string[,] data = new string[,]
{
{"姓名","年齡","性別","工號(hào)" },
{"張三","28","男","0023" },
{"李四","30","男","0024" },
{"王五","26","女","0025" }
};
//將數(shù)組內(nèi)容填充到表格
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("創(chuàng)建表格.pptx", FileFormat.Pptx2013);
刪除行和列
//初始化一個(gè)Presentation實(shí)例
Presentation ppt = new Presentation();
//加載一個(gè)PowerPoint文檔
ppt.LoadFromFile("創(chuàng)建表格.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);
合并單元格、設(shè)置單元格格式
//創(chuàng)建一個(gè)PowerPoint文檔
Presentation ppt = new Presentation();
ppt.SlideSize.Type = SlideSizeType.Screen16x9;
//初始化一個(gè)ITable實(shí)例,并指定位置、行數(shù)和列數(shù)、行高和列寬
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);
//聲明并初始化一個(gè)String[,]數(shù)組
string[,] data = new string[,]
{
{"訂單及支付情況","","","" },
{"訂單號(hào)","日期","客戶","是否支付" },
{"1021","2017/5/1","陽光地產(chǎn)","是" },
{"1022","2017/5/2","","否" }
};
//將數(shù)組內(nèi)容填充到表格
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");
}
}
//將表格的默認(rèn)格式設(shè)為空(自定義表格格式須清除表格默認(rèn)格式)
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);
}
//在合并后新的單元格設(shè)置文字水平對(duì)齊方式和背景色
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);
//在合并后新的單元格設(shè)置文字垂直對(duì)齊方式
Cell newCell2 = table[2, 2];
newCell2.TextAnchorType = TextAnchorType.Center;
//為單元格[3,2]、[3,3]設(shè)置背景色
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);
刪除表格
//初始化一個(gè)Presentation實(shí)例
Presentation ppt = new Presentation();
//加載一個(gè)PowerPoint文檔
ppt.LoadFromFile("創(chuàng)建表格.pptx");
//初始化一個(gè)List對(duì)象,元素類型為IShape
List<IShape> tableShapes= new List<IShape>();
//獲取第一張幻燈片上所有的表格圖形并添加到List
foreach (IShape shape in ppt.Slides[0].Shapes)
{
if (shape is ITable)
{
tableShapes.Add(shape);
}
}
//從幻燈片刪除第一個(gè)表格圖形
ppt.Slides[0].Shapes.Remove(tableShapes[0]);
//保存文檔
ppt.SaveToFile("刪除表格.pptx", FileFormat.Pptx2013);
到此這篇關(guān)于C#代碼實(shí)現(xiàn)在PowerPoint演示文稿中插入表格的文章就介紹到這了,更多相關(guān)C# PowerPoint插入表格內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C#反射實(shí)現(xiàn)插件式開發(fā)的過程詳解
插件式架構(gòu),一種全新的、開放性的、高擴(kuò)展性的架構(gòu)體系,插件式架構(gòu)設(shè)計(jì)好處很多,把擴(kuò)展功能從框架中剝離出來,降低了框架的復(fù)雜度,讓框架更容易實(shí)現(xiàn),這篇文章主要介紹了C#反射實(shí)現(xiàn)插件式開發(fā),需要的朋友可以參考下2023-09-09
C#實(shí)現(xiàn)將數(shù)據(jù)導(dǎo)出到word或者Excel中的方法
這篇文章主要介紹了C#實(shí)現(xiàn)將數(shù)據(jù)導(dǎo)出到word或者Excel中的方法,涉及C#操作word及Excel格式文件的方法,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-08-08

