创建图表是提升 PowerPoint 演示文稿表现力的有效方式,它能够将复杂的数据以直观的形式呈现,帮助观众快速理解关键信息。通过读取 Excel 数据生成图表,可以减少手动录入数据的工作量,并提高数据的准确性。如果希望在 PowerPoint 中直接使用 Excel 文件里的图表,也可以将图表以图片形式插入到幻灯片中,从而完整保留原有的样式和格式。
本文将介绍如何在 C# 中读取 Excel 数据,在 PowerPoint 幻灯片中创建图表,以及如何将 Excel 图表以图片形式插入到 PowerPoint 中。
安装依赖
开始之前,需要在 .NET 项目中添加所需的程序集引用。你可以下载对应的 DLL 文件并手动引用,也可以通过 NuGet 安装相关组件。
PM> Install-Package Spire.Office使用 C# 根据 Excel 数据创建 PowerPoint 图表
在 .NET 中,可以先读取 Excel 工作表中的数据,再将这些数据作为数据源,在 PowerPoint 幻灯片中生成图表。具体步骤如下:
创建
Presentation对象。创建
Workbook对象,并使用Workbook.LoadFromFile()方法加载 Excel 文件。获取演示文稿中的第一张幻灯片,并使用
ISlide.Shapes.AppendChart()方法添加图表。使用
IChart.ChartData.Clear()方法清除图表中的默认示例数据。获取 Excel 工作簿中的第一个工作表。
遍历工作表中的行和列:
读取单元格数据。
将读取的数据写入图表数据源。
设置图表标题。
设置图表系列标签和分类标签。
设置各数据系列的值。
设置分类轴和值轴的数字格式。
设置图表样式。
保存 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) { // 创建 Presentation 类的实例 Presentation presentation = new Presentation(); // 设置幻灯片大小 presentation.SlideSize.Type = SlideSizeType.Screen16x9; // 创建 Workbook 类的实例并加载 Excel 文件 Workbook workbook = new Workbook(); workbook.LoadFromFile("Sample.xlsx"); // 获取工作簿中的第一个工作表 Worksheet sheet = workbook.Worksheets[0]; // 在演示文稿中创建图表 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); // 清除图表中的默认示例数据 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 单元格数据写入图表数据 chart.ChartData[i, j].Value = sheet.AllocatedRange[i + 1, j + 1].Value2; // 同时复制数字格式 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]; // 设置系列数据 chart.Series[0].Values = chart.ChartData["B2", "B" + sheet.AllocatedRange.RowCount]; chart.Series[1].Values = chart.ChartData["C2", "C" + sheet.AllocatedRange.RowCount]; // 设置坐标轴数字格式 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 工作表中的现有图表插入到 PowerPoint 幻灯片中,并完整保留其原有的样式和格式,可以先将图表导出为图片,再将图片插入到幻灯片中。
具体步骤如下:
创建
Presentation对象。创建
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) { // 创建 Presentation 类的实例 Presentation presentation = new Presentation(); // 设置幻灯片大小 presentation.SlideSize.Type = SlideSizeType.Screen16x9; // 创建 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 数据生成 PowerPoint 图表,以及如何将 Excel 图表作为图片插入 PowerPoint 演示文稿。第一种方法通过读取工作表数据动态创建图表,适用于需要根据最新数据自动生成演示文稿的场景;第二种方法则将 Excel 中已有的图表导出为图片并插入幻灯片,能够完整保留图表的样式和格式,适合直接复用现有图表。开发者可根据实际需求选择合适的方式,实现 Excel 数据与 PowerPoint 演示文稿之间的高效集成。