本简短教程描述了使用 C# 将 Excel 嵌入 PowerPoint 的过程。它提供了设置开发环境的详细信息、编写应用程序的步骤列表以及一个示例代码,展示了如何使用 C# 在 PowerPoint 中嵌入 Excel 文件。您将学习如何将 Excel 文件嵌入幻灯片中的指定框架内。
使用 C# 在 PowerPoint 中嵌入 Excel 文件的步骤
- 设置 IDE 以使用 Aspose.Slides for .NET 在演示文稿中嵌入 Excel 文件
- 创建 Presentation 类对象
- 访问要嵌入 Excel 文件的第一张幻灯片
- 将Excel文件读入MemoryStream对象
- 创建 embedded data information 对象
- 将 OLE 对象框架与所需参数一起添加到幻灯片中
- 保存演示文稿
按照以下步骤学习如何使用 C# 在 PowerPoint 中嵌入 Excel 文件。创建 Presentation 类的对象,访问目标幻灯片,并将源 Excel 文件读入 MemoryStream。在幻灯片上指定宽度和高度的位置创建 OleEmbeddedDataInfo 对象,最后将 OLE 对象添加到演示文稿中。
使用 C# 将 Excel 工作簿插入 PowerPoint 的代码
using System; | |
using System.IO; | |
using Aspose.Slides; | |
using Aspose.Slides.Export; | |
using Aspose.Slides.DOM.Ole; | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
new License().SetLicense("License.lic"); | |
// Instantiates the Presentation object | |
using (Presentation presentation = new Presentation()) | |
{ | |
// Get the first slide | |
ISlide sld = presentation.Slides[0]; | |
// Read the XLSX file into the MemoryStream | |
byte[] fileBytes = File.ReadAllBytes("book1.xlsx"); | |
MemoryStream memoryStream = new MemoryStream(fileBytes); | |
// Create a data object | |
IOleEmbeddedDataInfo EmbeddedDataInfo = new OleEmbeddedDataInfo(memoryStream.ToArray(), "xlsx"); | |
// Add an Ole Object Frame shape | |
IOleObjectFrame oleObjectFrame = sld.Shapes.AddOleObjectFrame(0, 0, presentation.SlideSize.Size.Width, | |
presentation.SlideSize.Size.Height, EmbeddedDataInfo); | |
// Write the PPTX file | |
presentation.Save("Output.pptx", SaveFormat.Pptx); | |
} | |
Console.WriteLine("Excel File embedded successfully"); | |
} | |
} |
此代码演示了如何使用 C# 在 PowerPoint 中嵌入 Excel。对多张幻灯片重复此过程,并根据您的要求添加不同的文件类型。另一个重载的 AddOleObjectFrame() 方法接受 OLE 类名和链接文件的路径。
本文教我们如何使用 C# 将 Excel 文件嵌入 PowerPoint。若要在演示文稿中添加音频,请参阅文章:如何使用 C# 在演示文稿中添加音频。