溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

C# 實(shí)現(xiàn)對(duì)PPT插入、編輯、刪除表格

發(fā)布時(shí)間:2020-08-07 12:25:47 來源:網(wǎng)絡(luò) 閱讀:786 作者:Eiceblue 欄目:編程語言

在現(xiàn)代學(xué)習(xí)和辦公當(dāng)中,經(jīng)常會(huì)接觸到對(duì)表格的運(yùn)用,像各種單據(jù)、報(bào)表、賬戶等等。在PPT演示文稿中同樣不可避免的應(yīng)用到各種數(shù)據(jù)表格。對(duì)于在PPT中插入表格,我發(fā)現(xiàn)了一個(gè)新方法,不過我用到了一款免費(fèi)的.NET組件——Free Spire.Presentation,在C#中添加該產(chǎn)品DLL文件,可以簡(jiǎn)單快速地實(shí)現(xiàn)對(duì)演示文稿的表格插入、編輯和刪除等操作。有需要的話可以在下面的網(wǎng)址下載:https://www.e-iceblue.cn/Downloads/Free-Spire-Presentation-NET.html

  1. 插入表格


步驟一:創(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, 100 };
double[] heights = new double[] { 15, 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)"},
    {"1","李彪","18270","18270","0011"},
    {"2","李娜","18105","18105","0025"},
    {"3","張麗","17987","17987","0008"},
    {"4","黃艷","17790","17790","0017"},
};

步驟五:將數(shù)組內(nèi)容填充到表格

for (int i = 0; i < 5; 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("Arial");
    }
}

步驟六:保存文檔

ppt.SaveToFile("創(chuàng)建表格.pptx", FileFormat.Pptx2010);

C# 實(shí)現(xiàn)對(duì)PPT插入、編輯、刪除表格


2.刪除行與列


對(duì)于如何刪除表格中不需要的數(shù)據(jù)組,可參考我下面的步驟

步驟一:初始化一個(gè)Presentation實(shí)例并加載一個(gè)PowerPoint文檔

Presentation ppt = new Presentation();
ppt.LoadFromFile(@"C:\Users\Administrator\Desktop\創(chuàng)建表格.pptx");

步驟二:獲取第一張幻燈片上的表格

ITable table = null;
foreach (IShape shape in ppt.Slides[0].Shapes
{
    if (shape is ITable)
    {
        table = (ITable)shape;

步驟三:刪除第四行第四列

table.ColumnsList.RemoveAt(3, false);
table.TableRows.RemoveAt(4, false);
    }
}

步驟四:保存文檔

ppt.SaveToFile("刪除行與列.pptx", FileFormat.Pptx2010);

C# 實(shí)現(xiàn)對(duì)PPT插入、編輯、刪除表格

3.刪除表格


步驟一:初始化一個(gè)Presentation實(shí)例并加載一個(gè)PPT文檔

Presentation ppt = new Presentation();
ppt.LoadFromFile(@"C:\Users\Administrator\Desktop\創(chuàng)建表格.pptx");

步驟二:初始化一個(gè)List對(duì)象,元素類型為IShape

List<IShape> tableShapes = new List<IShape>();

步驟三:獲取第一張幻燈片上所有的表格圖形并添加到List

foreach (IShape shape inppt.Slides[0].Shapes)
{
   if (shape is ITable)
     {
       tableShapes.Add(shape);
     }
}

步驟四:從幻燈片刪除第一個(gè)表格圖形

ppt.Slides[0].Shapes.Remove(tableShapes[0]);

步驟五:保存文檔

ppt.SaveToFile("刪除表格.pptx", FileFormat.Pptx2010);


以上是本人使用Free Spire.Presentation這款組件對(duì)PPT文檔中表格的一些操作,希望能提供幫助,感謝閱讀!


向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

ppt
AI