C# 給PPT中的圖表添加趨勢線的方法
支持趨勢線的圖表類型包括二維面積圖、條形圖、柱形圖、柱形圖、股價圖、xy (散點圖) 和氣泡圖中;不能向三維、堆積、雷達圖、餅圖、曲面圖或圓環(huán)圖的數(shù)據(jù)系列添加趨勢線??商砑拥内厔菥€類型包括6種,即多項式(Polynomial)趨勢線、指數(shù)(Exponential)趨勢線、線性(Linear)趨勢線、對數(shù)(Logarithmic)趨勢線、冪(Power)趨勢線、移動平均(移動平均)趨勢線。下面以柱形圖表為例,添加趨勢線。方法及步驟參考如下。
程序環(huán)境:
- Visual Studio 2017
- .net framework 4.6.1
- Power Point 2013 (.pptx)
- PPT類庫:Spire.Presentation for .NET
一、 實現(xiàn)方法
通過調(diào)用Spire.Presentation.dll中Itrendline接口提供的方法 AddTrendLine(TrendlinesType type) 來添加趨勢線,編輯代碼前,請先按照如下第2點中的方法在程序中添加引用Spire.Presentation.dll。
二、PPT 類庫安裝
關(guān)于PPT 類庫安裝:可直接通過Nuget 搜索安裝到程序。具體方法如下:
鼠標(biāo)右鍵點擊“引用”,“管理Nuget包”,然后按照下圖步驟操作;



完成安裝:

三、代碼實現(xiàn)
1、C#
using Spire.Presentation;
using Spire.Presentation.Charts;
namespace AddTrendline
{
class Program
{
static void Main(string[] args)
{
//創(chuàng)建Presentation類的實例
Presentation ppt = new Presentation();
//加載PowerPoint文檔
ppt.LoadFromFile("test.pptx");
//獲取第一張幻燈片
ISlide slide = ppt.Slides[0];
//獲取幻燈片上的第一個圖表
IChart chart = (IChart)slide.Shapes[0];
//給圖表的第一個數(shù)據(jù)系列添加線性趨勢線
ITrendlines trendLine = chart.Series[0].AddTrendLine(TrendlinesType.Polynomial);//多項式趨勢線
//ITrendlines trendLine = chart.Series[0].AddTrendLine(TrendlinesType.Exponential);//指數(shù)趨勢線
//ITrendlines trendLine = chart.Series[0].AddTrendLine(TrendlinesType.Linear);//線性趨勢線
//ITrendlines trendLine = chart.Series[0].AddTrendLine(TrendlinesType.Logarithmic);//對數(shù)趨勢線
//ITrendlines trendLine = chart.Series[0].AddTrendLine(TrendlinesType.Power);//冪趨勢線
//ITrendlines trendLine = chart.Series[0].AddTrendLine(TrendlinesType.MovingAverage);//移動平均趨勢線
//顯示公式
trendLine.displayEquation = true;
//顯示R平方值
trendLine.displayRSquaredValue = true;
//保存結(jié)果文檔
ppt.SaveToFile("AddTrendline.pptx", FileFormat.Pptx2013);
System.Diagnostics.Process.Start("AddTrendline.pptx");
}
}
}
趨勢線添加效果:

2、vb.net
Imports Spire.Presentation
Imports Spire.Presentation.Charts
Namespace AddTrendline
Class Program
Private Shared Sub Main(args As String())
'創(chuàng)建Presentation類的實例
Dim ppt As New Presentation()
'加載PowerPoint文檔
ppt.LoadFromFile("test.pptx")
'獲取第一張幻燈片
Dim slide As ISlide = ppt.Slides(0)
'獲取幻燈片上的第一個圖表
Dim chart As IChart = DirectCast(slide.Shapes(0), IChart)
'給圖表的第一個數(shù)據(jù)系列添加線性趨勢線
Dim trendLine As ITrendlines = chart.Series(0).AddTrendLine(TrendlinesType.Polynomial)
'多項式趨勢線
'ITrendlines trendLine = chart.Series[0].AddTrendLine(TrendlinesType.Exponential); '指數(shù)趨勢線
'ITrendlines trendLine = chart.Series[0].AddTrendLine(TrendlinesType.Linear); '線性趨勢線
'ITrendlines trendLine = chart.Series[0].AddTrendLine(TrendlinesType.Logarithmic); '對數(shù)趨勢線
'ITrendlines trendLine = chart.Series[0].AddTrendLine(TrendlinesType.Power); '冪趨勢線
'ITrendlines trendLine = chart.Series[0].AddTrendLine(TrendlinesType.MovingAverage); '移動平均趨勢線
'顯示公式
trendLine.displayEquation = True
'顯示R平方值
trendLine.displayRSquaredValue = True
'保存結(jié)果文檔
ppt.SaveToFile("AddTrendline.pptx", FileFormat.Pptx2013)
System.Diagnostics.Process.Start("AddTrendline.pptx")
End Sub
End Class
End Namespace
- C# 給PPT中的圖表添加趨勢線的方法
- C#如何給PPT中圖表添加趨勢線詳解
- 使用C#實現(xiàn)一個PPT遙控器
- C# 多進程打開PPT的示例教程
- C# 實現(xiàn)PPT 每一頁轉(zhuǎn)成圖片過程解析
- C#將PPT文件轉(zhuǎn)換成PDF文件
- C#如何添加PPT背景
- C# 實現(xiàn)對PPT文檔加密、解密及重置密碼的操作方法
- C#提取PPT文本和圖片的實現(xiàn)方法
- C# 使用Free Spire.Presentation 實現(xiàn)對PPT插入、編輯、刪除表格
- 在C#里面給PPT文檔添加注釋的實現(xiàn)代碼
- C#向PPT文檔插入圖片以及導(dǎo)出圖片的實例
- C#實現(xiàn)將PPT轉(zhuǎn)換成HTML的方法
相關(guān)文章
C# SqlSugar批量執(zhí)行SQL語句及批量更新實體對象的操作方法
SqlSugar 是一款 老牌 .NET開源ORM框架,由果糖大數(shù)據(jù)科技團隊維護和更新 ,開箱即用最易上手的ORM,這篇文章主要介紹了C# SqlSugar批量執(zhí)行SQL語句以及批量更新實體對象,需要的朋友可以參考下2024-07-07
C# datatable 不能通過已刪除的行訪問該行的信息處理方法
采用datatable.Rows[i].Delete()刪除行后再訪問該表時出現(xiàn)出現(xiàn)“不能通過已刪除的行訪問該行的信息”的錯誤2012-11-11
C#使用表達式樹(LambdaExpression)動態(tài)更新類的屬性值(示例代碼)
這篇文章主要介紹了C#使用表達式樹(LambdaExpression)動態(tài)更新類的屬性值,本文通過示例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-01-01
C#使用FileInfo和DirectoryInfo類來執(zhí)行文件和文件夾操作
System.IO.FileInfo?和?System.IO.DirectoryInfo?是C#中用于操作文件和文件夾的類,它們提供了許多有用的方法和屬性來管理文件和文件夾,這篇文章主要介紹了C#使用FileInfo和DirectoryInfo類來執(zhí)行文件和文件夾操作,需要的朋友可以參考下2023-08-08

