C#代碼實現(xiàn)根據(jù)Excel數(shù)據(jù)創(chuàng)建PowerPoint圖表
創(chuàng)建圖表是提升 PowerPoint 演示文稿表現(xiàn)力的有效方式,它能夠將復雜的數(shù)據(jù)以直觀的形式呈現(xiàn),幫助觀眾快速理解關鍵信息。通過讀取 Excel 數(shù)據(jù)生成圖表,可以減少手動錄入數(shù)據(jù)的工作量,并提高數(shù)據(jù)的準確性。如果希望在 PowerPoint 中直接使用 Excel 文件里的圖表,也可以將圖表以圖片形式插入到幻燈片中,從而完整保留原有的樣式和格式。
本文將介紹如何在 C# 中讀取 Excel 數(shù)據(jù),在 PowerPoint 幻燈片中創(chuàng)建圖表,以及如何將 Excel 圖表以圖片形式插入到 PowerPoint 中。
安裝依賴
開始之前,需要在 .NET 項目中添加所需的程序集引用。你可以下載對應的 DLL 文件并手動引用,也可以通過 NuGet 安裝相關組件。
PM> Install-Package Spire.Office
使用 C# 根據(jù) Excel 數(shù)據(jù)創(chuàng)建 PowerPoint 圖表
在 .NET 中,可以先讀取 Excel 工作表中的數(shù)據(jù),再將這些數(shù)據(jù)作為數(shù)據(jù)源,在 PowerPoint 幻燈片中生成圖表。具體步驟如下:
1.創(chuàng)建 Presentation 對象。
2.創(chuàng)建 Workbook 對象,并使用 Workbook.LoadFromFile() 方法加載 Excel 文件。
3.獲取演示文稿中的第一張幻燈片,并使用 ISlide.Shapes.AppendChart() 方法添加圖表。
4.使用 IChart.ChartData.Clear() 方法清除圖表中的默認示例數(shù)據(jù)。
5.獲取 Excel 工作簿中的第一個工作表。
6.遍歷工作表中的行和列:
- 讀取單元格數(shù)據(jù)。
- 將讀取的數(shù)據(jù)寫入圖表數(shù)據(jù)源。
7.設置圖表標題。
8.設置圖表系列標簽和分類標簽。
9.設置各數(shù)據(jù)系列的值。
10.設置分類軸和值軸的數(shù)字格式。
11.設置圖表樣式。
12.保存 PowerPoint 演示文稿。
完整示例代碼如下:
using Spire.Presentation;
using Spire.Presentation.Charts;
using Spire.Xls;
using System.Drawing;
using FileFormat = Spire.Presentation.FileFormat;
using IChart = Spire.Presentation.Charts.IChart;
namespace PresentationChartExcelData
{
class Program
{
public static void Main(string[] args)
{
// 創(chuàng)建 Presentation 類的實例
Presentation presentation = new Presentation();
// 設置幻燈片大小
presentation.SlideSize.Type = SlideSizeType.Screen16x9;
// 創(chuàng)建 Workbook 類的實例并加載 Excel 文件
Workbook workbook = new Workbook();
workbook.LoadFromFile("Sample.xlsx");
// 獲取工作簿中的第一個工作表
Worksheet sheet = workbook.Worksheets[0];
// 在演示文稿中創(chuàng)建圖表
RectangleF rect = new RectangleF(
50,
100,
presentation.SlideSize.Size.Width - 100,
presentation.SlideSize.Size.Height - 150);
ISlide slide = presentation.Slides[0];
IChart chart = slide.Shapes.AppendChart(ChartType.ColumnClustered, rect);
// 清除圖表中的默認示例數(shù)據(jù)
chart.ChartData.Clear(0, 0, 5, 5);
// 遍歷工作表中的所有行
for (int i = 0; i < sheet.AllocatedRange.RowCount; i++)
{
// 遍歷工作表中的所有列
for (int j = 0; j < sheet.AllocatedRange.ColumnCount; j++)
{
// 將 Excel 單元格數(shù)據(jù)寫入圖表數(shù)據(jù)
chart.ChartData[i, j].Value = sheet.AllocatedRange[i + 1, j + 1].Value2;
// 同時復制數(shù)字格式
chart.ChartData[i, j].NumberFormat = sheet.AllocatedRange[i + 1, j + 1].NumberFormat;
}
}
// 設置圖表標題
chart.ChartTitle.TextProperties.Text = sheet.Name;
chart.ChartTitle.TextProperties.IsCentered = true;
chart.ChartTitle.Height = 25;
chart.HasTitle = true;
// 設置系列標簽和分類標簽
chart.Series.SeriesLabel = chart.ChartData["B1", "C1"];
chart.Categories.CategoryLabels = chart.ChartData["A2", "A" + sheet.AllocatedRange.RowCount];
// 設置系列數(shù)據(jù)
chart.Series[0].Values = chart.ChartData["B2", "B" + sheet.AllocatedRange.RowCount];
chart.Series[1].Values = chart.ChartData["C2", "C" + sheet.AllocatedRange.RowCount];
// 設置坐標軸數(shù)字格式
chart.PrimaryCategoryAxis.NumberFormat = sheet.AllocatedRange["A2"].NumberFormat;
chart.PrimaryValueAxis.NumberFormat = sheet.AllocatedRange["B2"].NumberFormat;
// 設置圖表樣式
chart.ChartStyle = ChartStyle.Style2;
// 設置系列重疊和間隙寬度
chart.OverLap = 50;
chart.GapWidth = 200;
// 保存演示文稿
presentation.SaveToFile("output/PresentationChartExcelData.pptx", FileFormat.Pptx2019);
// 釋放資源
presentation.Dispose();
workbook.Dispose();
}
}
}使用 C# 將 Excel 圖表作為圖片插入 PowerPoint
如果希望將 Excel 工作表中的現(xiàn)有圖表插入到 PowerPoint 幻燈片中,并完整保留其原有的樣式和格式,可以先將圖表導出為圖片,再將圖片插入到幻燈片中。
具體步驟如下:
- 創(chuàng)建
Presentation對象。 - 創(chuàng)建
Workbook對象,并使用Workbook.LoadFromFile()方法加載 Excel 文件。 - 使用
Workbook.SaveChartAsImage()方法將工作表中的圖表保存為圖片。 - 使用
Presentation.Images.Append()方法將圖片添加到演示文稿資源中。 - 使用
Presentation.Slides[].AppendEmbedImage()方法將圖片插入到指定幻燈片。 - 使用
Presentation.SaveToFile()方法保存 PowerPoint 演示文稿。
完整示例代碼如下:
using Spire.Presentation;
using Spire.Presentation.Drawing;
using Spire.Xls;
using System.Drawing;
using FileFormat = Spire.Presentation.FileFormat;
namespace PresentationChartExcelChart
{
class Program
{
public static void Main(string[] args)
{
// 創(chuàng)建 Presentation 類的實例
Presentation presentation = new Presentation();
// 設置幻燈片大小
presentation.SlideSize.Type = SlideSizeType.Screen16x9;
// 創(chuàng)建 Workbook 類的實例并加載 Excel 文件
Workbook workbook = new Workbook();
workbook.LoadFromFile("Sample.xlsx");
// 將第一個工作表中的第一個圖表保存為圖片
Image image = workbook.SaveChartAsImage(workbook.Worksheets[0], 0);
// 將圖片添加到演示文稿資源中
IImageData imageData = presentation.Images.Append(image);
// 將圖片插入到第一張幻燈片
RectangleF rect = new RectangleF(
50,
120,
presentation.SlideSize.Size.Width - 100,
presentation.SlideSize.Size.Height - 170);
presentation.Slides[0].Shapes.AppendEmbedImage(
ShapeType.Rectangle,
imageData,
rect);
// 保存演示文稿
presentation.SaveToFile(
"output/PresentationChartExcelChart.pptx",
FileFormat.Pptx2019);
// 釋放資源
presentation.Dispose();
workbook.Dispose();
}
}
}總結
本文介紹了如何在 C# 中利用 Excel 數(shù)據(jù)生成 PowerPoint 圖表,以及如何將 Excel 圖表作為圖片插入 PowerPoint 演示文稿。第一種方法通過讀取工作表數(shù)據(jù)動態(tài)創(chuàng)建圖表,適用于需要根據(jù)最新數(shù)據(jù)自動生成演示文稿的場景;第二種方法則將 Excel 中已有的圖表導出為圖片并插入幻燈片,能夠完整保留圖表的樣式和格式,適合直接復用現(xiàn)有圖表。開發(fā)者可根據(jù)實際需求選擇合適的方式,實現(xiàn) Excel 數(shù)據(jù)與 PowerPoint 演示文稿之間的高效集成。
到此這篇關于C#代碼實現(xiàn)根據(jù)Excel數(shù)據(jù)創(chuàng)建PowerPoint圖表的文章就介紹到這了,更多相關C#創(chuàng)建PowerPoint圖表內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
基于C#實現(xiàn)Windows服務狀態(tài)啟動和停止服務的方法
這篇文章主要介紹了基于C#實現(xiàn)Windows服務狀態(tài)啟動和停止服務的方法,詳細講述了實現(xiàn)這一功能的具體步驟,代碼簡潔易懂,需要的朋友可以參考下2014-09-09
.net C# 實現(xiàn)任意List的笛卡爾乘積算法代碼
笛卡爾(Descartes)乘積又叫直積。假設集合A={a,b},集合B={0,1,2},則兩個集合的笛卡爾積為{(a,0),(a,1),(a,2),(b,0),(b,1), (b,2)}。2013-05-05
C#中StringBuilder用法以及和String的區(qū)別分析
當我們在初學使用C#時,常常會不知道該用StringBuilder合適還是用String高效,下面是我在學習當中對StringBuilder和String的區(qū)別總結,分享給大家。2013-03-03

