C#如何實如今PPT文檔中插入、編輯和刪除表格的操做

提示:在進行代碼操做以前,須要在添加引用Spire.XLS 的dll文件到項目程序集中數組

問題1:如何建立?ide

第一步,初始化一個Presentation實例spa

Presentation ppt = new Presentation();
ppt.SlideSize.Type = SlideSizeType.Screen16x9;

第二步,初始化一個ITable實例code

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);

第三步,設置表格內置格式orm

table.StylePreset = TableStylePreset.LightStyle1Accent2;

第四步,聲明數組對象

string[,] data = new string[,]
{
    {"排名","姓名","銷售額","回款額","工號"},
    {"1","李彪","18270","18270","0011"},
    {"2","李娜","18105","18105","0025"},
    {"3","張麗","17987","17987","0008"},
    {"4","黃豔","17790","17790","0017"},
};

最後,保存文檔便可文檔

ppt.SaveToFile("建立表格.pptx", FileFormat.Pptx2010);

按上面的操做能夠獲得如下的操做效果圖string

 

問題2,如何編輯?io

eg:刪除行和列table

第一步,初始化一個presentation類實例,並加載一個ppt文檔

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

第二步,獲取幻燈片上的表格

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");
    }
}

第三步,刪除行和列

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

最後,保存文檔

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

效果圖:

問題3,如何刪除表格?

第一步,初始化一個Presentation類實例,並加載一個PPT文檔

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

第二步,初始化一個List對象,元素類型爲IShape

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

第三步,獲取第一張幻燈片上全部的表格圖形

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.Slides[0].Shapes.Remove(tableShapes[0]);

最後,保存文檔

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

查看原文檔能夠發現,ppt中的第一個表格已經被刪除了。

 小結

在本篇文章中的操做步驟及過程是基於Spire.XLS組件,在C#中的應用。在對文檔進行操做時須要添加dll文件,這個是前提條件。但在整個操做過程當中,其實步驟是比較簡單的。

(本篇完)

感謝對本文的支持!

相關文章
相關標籤/搜索