EXCEL 工具

如何在C#中查看PowerPoint文件

发布 2024年四月3日
分享:

简介

在软件开发领域,要想提供丰富而迷人的用户体验,就必须有能力将多种媒体纳入程序。由于 Microsoft Office PowerPoint 演示文稿经常被用于交流信息,因此在 C# WinForms 程序中集成 PowerPoint 查看器控件非常有利。本文将介绍如何集成此类控件,使开发人员能够在其应用程序中添加 PowerPoint 查看功能,并对其进行改进。在本文中,我们将在不安装 MS PowerPoint Viewer 的情况下创建 C# PowerPoint 查看器。

如何在 C&num 中查看 Powerpoint 文件;

1.创建 PowerPoint 应用程序实例。

2.将 Interop 引用添加到项目中

3.使用 "实例 "打开 "演示"。

4.选中并为导出文件创建输出文件夹。

5.将创建的幻灯片图像加载到图片框中。使用按钮在幻灯片之间移动。

6.关闭演示文稿并退出应用程序。

了解 PowerPoint 查看器控件

PowerPoint 演示文稿广泛应用于教育和商业等多个领域。在 WinForms 应用程序中加入 PowerPoint 查看器控件有几个优点。

  • 平滑集成:通过允许用户在应用程序中直接查看 PowerPoint 演示文稿并与之互动,用户可以减少在各种程序之间切换的时间,并简化工作流程。
  • 增强用户体验:该程序通过提供可识别的界面来处理 PowerPoint 幻灯片,从而改善了用户体验并提高了效率。

  • 增强灵活性:对于 .NET 应用程序中的 Windows 开发人员来说,通过添加导航控件或引入显示模式,开发人员可以定制查看器控件,以满足独特的需求。

添加参考资料

首先为必要的程序集添加引用:要在项目中添加 Microsoft.Office.Interop.PowerPoint 引用,请右键单击 Visual Studio 中的项目,选择 "添加">"引用",然后从 COM 选项卡中添加它们。

浏览器 PowerPoint

阅览器 Microsoft.Office.Interop.PowerPoint 命名空间提供了与 PowerPoint 演示文稿进行编程交互的类和方法。确保系统已安装 PowerPoint。让我们对前面示例中的代码进行剖析和解释:

using Microsoft.Office.Core;
using Microsoft.Office.Interop.PowerPoint;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace DataTableWindowsForm
{
    public partial class PowerPointViewer : Form
    {
        private Microsoft.Office.Interop.PowerPoint.Application pptApplication = new Microsoft.Office.Interop.PowerPoint.Application();
        private Presentation pptPresentation;
        private string outputFolder = @"output_images";
        private int slideIndex = 1;
        public PowerPointViewer()
        {
            InitializeComponent();
        }
        private void DisplaySlide()
        {
            if (!Directory.Exists(outputFolder))
                Directory.CreateDirectory(outputFolder);
            // Export the slide as an png file
            string tempHtmlFile = Path.Combine(outputFolder, "temp.png");
            pptPresentation.Slides [slideIndex].Export(tempHtmlFile, "png", 1024, 768);
            // Load the HTML file into the picture box control
            pictureBox1.ImageLocation = tempHtmlFile;
        }
        private void Next_Click(object sender, EventArgs e)
        {
            int currentSlideIndex = slideIndex;
            if (currentSlideIndex < pptPresentation.Slides.Count)
            {
                slideIndex = slideIndex + 1;
                DisplaySlide();
            }
        }
        private void PowerPointViewercs_Load(object sender, EventArgs e)
        {
            // Load PowerPoint Presentation
            string pptFilePath = "demo.pptx"; // Path to your PowerPoint file
            pptPresentation = pptApplication.Presentations.Open(pptFilePath, MsoTriState.msoFalse, MsoTriState.msoFalse, MsoTriState.msoFalse);
        // Load PowerPoint files here
            DisplaySlide();
        }
        private void Previous_Click(object sender, EventArgs e)
        {
            int currentSlideIndex = slideIndex;
            if (currentSlideIndex > 1)
            {
                slideIndex = slideIndex - 1;
                DisplaySlide();
            }
        }
        private void PowerPointViewercs_FormClosing(object sender, FormClosingEventArgs e)
        {
            // Close the PowerPoint presentation and quit the application when the form is closing
            pptPresentation.Close();
            pptApplication.Quit();
        }
    }
}
using Microsoft.Office.Core;
using Microsoft.Office.Interop.PowerPoint;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace DataTableWindowsForm
{
    public partial class PowerPointViewer : Form
    {
        private Microsoft.Office.Interop.PowerPoint.Application pptApplication = new Microsoft.Office.Interop.PowerPoint.Application();
        private Presentation pptPresentation;
        private string outputFolder = @"output_images";
        private int slideIndex = 1;
        public PowerPointViewer()
        {
            InitializeComponent();
        }
        private void DisplaySlide()
        {
            if (!Directory.Exists(outputFolder))
                Directory.CreateDirectory(outputFolder);
            // Export the slide as an png file
            string tempHtmlFile = Path.Combine(outputFolder, "temp.png");
            pptPresentation.Slides [slideIndex].Export(tempHtmlFile, "png", 1024, 768);
            // Load the HTML file into the picture box control
            pictureBox1.ImageLocation = tempHtmlFile;
        }
        private void Next_Click(object sender, EventArgs e)
        {
            int currentSlideIndex = slideIndex;
            if (currentSlideIndex < pptPresentation.Slides.Count)
            {
                slideIndex = slideIndex + 1;
                DisplaySlide();
            }
        }
        private void PowerPointViewercs_Load(object sender, EventArgs e)
        {
            // Load PowerPoint Presentation
            string pptFilePath = "demo.pptx"; // Path to your PowerPoint file
            pptPresentation = pptApplication.Presentations.Open(pptFilePath, MsoTriState.msoFalse, MsoTriState.msoFalse, MsoTriState.msoFalse);
        // Load PowerPoint files here
            DisplaySlide();
        }
        private void Previous_Click(object sender, EventArgs e)
        {
            int currentSlideIndex = slideIndex;
            if (currentSlideIndex > 1)
            {
                slideIndex = slideIndex - 1;
                DisplaySlide();
            }
        }
        private void PowerPointViewercs_FormClosing(object sender, FormClosingEventArgs e)
        {
            // Close the PowerPoint presentation and quit the application when the form is closing
            pptPresentation.Close();
            pptApplication.Quit();
        }
    }
}
Imports Microsoft.Office.Core
Imports Microsoft.Office.Interop.PowerPoint
Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.IO
Imports System.Linq
Imports System.Text
Imports System.Threading.Tasks
Imports System.Windows.Forms
Namespace DataTableWindowsForm
	Partial Public Class PowerPointViewer
		Inherits Form

		Private pptApplication As New Microsoft.Office.Interop.PowerPoint.Application()
		Private pptPresentation As Presentation
		Private outputFolder As String = "output_images"
		Private slideIndex As Integer = 1
		Public Sub New()
			InitializeComponent()
		End Sub
		Private Sub DisplaySlide()
			If Not Directory.Exists(outputFolder) Then
				Directory.CreateDirectory(outputFolder)
			End If
			' Export the slide as an png file
			Dim tempHtmlFile As String = Path.Combine(outputFolder, "temp.png")
			pptPresentation.Slides (slideIndex).Export(tempHtmlFile, "png", 1024, 768)
			' Load the HTML file into the picture box control
			pictureBox1.ImageLocation = tempHtmlFile
		End Sub
		Private Sub Next_Click(ByVal sender As Object, ByVal e As EventArgs)
			Dim currentSlideIndex As Integer = slideIndex
			If currentSlideIndex < pptPresentation.Slides.Count Then
				slideIndex = slideIndex + 1
				DisplaySlide()
			End If
		End Sub
		Private Sub PowerPointViewercs_Load(ByVal sender As Object, ByVal e As EventArgs)
			' Load PowerPoint Presentation
			Dim pptFilePath As String = "demo.pptx" ' Path to your PowerPoint file
			pptPresentation = pptApplication.Presentations.Open(pptFilePath, MsoTriState.msoFalse, MsoTriState.msoFalse, MsoTriState.msoFalse)
		' Load PowerPoint files here
			DisplaySlide()
		End Sub
		Private Sub Previous_Click(ByVal sender As Object, ByVal e As EventArgs)
			Dim currentSlideIndex As Integer = slideIndex
			If currentSlideIndex > 1 Then
				slideIndex = slideIndex - 1
				DisplaySlide()
			End If
		End Sub
		Private Sub PowerPointViewercs_FormClosing(ByVal sender As Object, ByVal e As FormClosingEventArgs)
			' Close the PowerPoint presentation and quit the application when the form is closing
			pptPresentation.Close()
			pptApplication.Quit()
		End Sub
	End Class
End Namespace
VB   C#

首先,我们导入所需的命名空间。文件操作由 System.IO处理,System.Windows.Forms提供 WinForms 功能,而用于与 PowerPoint 交互的 Microsoft.Office 类则包含在 PowerPoint 中。在 PowerPointViewerApp 命名空间中,我们定义了一个名为 PowerPointViewer 的 WinForms 窗体类。视图 PPT 应用程序的主要用户界面就是这个窗体。

据悉,PowerPoint 应用程序和演示文稿的引用将存储在两个私有字段 pptApplicationpptPresentation 中。通过使用 InitializeComponent()构造函数设置设计器定义的表单可视化组件,并初始化表单。当窗体加载时,InitializeComponent() 函数被调用。PowerPoint 应用程序 (ppt 应用) 在此时被初始化并可访问。

接下来,我们启动 pptFilePath 指定的 PowerPoint 文件。使用 pptApplication.Presentations,我们通过提供路径来打开 PowerPoint 文件 (pptFilePath) 到开放() 功能。该过程将返回一个 Presentation 对象,该对象代表已打开的演示文稿。

要显示 PowerPoint 演示文稿的第一张幻灯片,我们使用DisplaySlide() 功能。第一张幻灯片的索引为 1,图片框控件中显示的幻灯片就是该函数的结果。我们使用导出() 功能将选定的幻灯片导出为 PNG 文件。导出的 PNG 文件会暂时存放在系统的临时文件夹中。使用图像位置() 方法,我们将导出的 PNG 文件加载到 PictureBox 控件中。这些技术可以实现幻灯片之间的翻转。

导航按钮btnPrevious_ClickbtnNext_Click可分别跳转到上一张和下一张幻灯片。通过pptPresentation,我们可以在slideIndex变量中获得幻灯片的索引。我们调用 DisplaySlide() 如果当前幻灯片索引在 1 到全部幻灯片数量的有效范围内,则会分别用上一张或下一张幻灯片的索引表示。

窗体关闭时,Close() 函数被调用。在这里,我们通过退出 PowerPoint 程序和使用 Quit 终止 PowerPoint 应用程序来释放系统资源,从而确保资源管理的正确性。() 方法。

PowerPoint 文件

如何用 C# 查看 Powerpoint 文件:图 1 - PowerPoint 文件 demo.pptx

输出:PowerPoint 查看器

如何用 C# 查看 Powerpoint 文件:图 2 - PowerPointViwercs

IronXL

使用流行的 .NET Excel 库,C# 中的 Excel 文件操作变得更简单 铁XL.它具有读取、生成和修改 Excel 文件的丰富功能,是一款适合各种应用的多功能工具。

下面,我将介绍 IronXL 的一些关键特性:

便捷访问和高速

  • 有了 IronXL,开发人员可以快速读取当前 Excel 文件中的数据,并将数据写入新的或现有的 Excel 文件。这就解决了访问工作簿和工作表属性(如单元格值、公式和格式化)的问题。

广泛的 Excel 支持

  • 开发人员可使用 IronXL 将数据库和 CSV 文件等来源的数据导入 Excel 电子表格。同样,Excel 文件中的数据也可以导出为 CSV、HTML、XML 和 PDF 等多种格式。

灵活性

  • 使用 IronXL,开发人员可以动态添加、修改和删除 Excel 文件中的工作表。这样就可以根据应用程序的要求,灵活处理数据格式和组织结构。

准确修改

  • 有了 IronXL,Excel 电子表格中的单个单元格都可以精确修改。开发人员可以通过编程设置单元格值、公式、样式、格式和其他功能。

要了解更多文档信息,请参阅 这里.

安装 IronXL

在继续之前,让我们先安装 IronXL 使用 NuGet 软件包管理器控制台:

Install-Package IronXL.Excel

IronXL 安装后可在我们的 C# 项目中使用。

使用 IronXL 进行 Excel 操作

让我们来看一个假设场景:我们想使用 IronXL 从 Excel 文件中读取数据。下面是一个如何实现这一目标的小示例:

using IronXL;
using System;
class Program
{
    static void Main(string [] args)
    {
        // Path to the Excel file
        string excelFilePath = "sample.xlsx";
        // Load the Excel file
        WorkBook workbook = WorkBook.Load(excelFilePath);
        // Access the first worksheet
        WorkSheet worksheet = workbook.WorkSheets [0];
        // Iterate through rows and columns to read data
        foreach (var row in worksheet.Rows)
        {
            foreach (var cell in row)
            {
                Console.Write(cell.Value + "\t");
            }
            Console.WriteLine();
        }
    }
}
using IronXL;
using System;
class Program
{
    static void Main(string [] args)
    {
        // Path to the Excel file
        string excelFilePath = "sample.xlsx";
        // Load the Excel file
        WorkBook workbook = WorkBook.Load(excelFilePath);
        // Access the first worksheet
        WorkSheet worksheet = workbook.WorkSheets [0];
        // Iterate through rows and columns to read data
        foreach (var row in worksheet.Rows)
        {
            foreach (var cell in row)
            {
                Console.Write(cell.Value + "\t");
            }
            Console.WriteLine();
        }
    }
}
Imports Microsoft.VisualBasic
Imports IronXL
Imports System
Friend Class Program
	Shared Sub Main(ByVal args() As String)
		' Path to the Excel file
		Dim excelFilePath As String = "sample.xlsx"
		' Load the Excel file
		Dim workbook As WorkBook = WorkBook.Load(excelFilePath)
		' Access the first worksheet
		Dim worksheet As WorkSheet = workbook.WorkSheets (0)
		' Iterate through rows and columns to read data
		For Each row In worksheet.Rows
			For Each cell In row
				Console.Write(cell.Value & vbTab)
			Next cell
			Console.WriteLine()
		Next row
	End Sub
End Class
VB   C#

我们首先要添加必要的命名空间。IronXL 命名空间包含 IronXL 库提供的类和方法。我们给出了要读取的 Excel 文件 sample.xlsx 的路径。利用 WorkBook 加载 Excel 文件。通过 Load 提供的 Workbook 对象,我们可以读取 Excel 文件。() 方法代表 Excel 工作簿。我们可以使用 workbook.WorkSheets 访问工作簿的第一个 工作表。 [0].我们使用堆叠的 for each 循环遍历工作表的行和列。我们将每个单元格的值发送到控制台。

如何用 C# 查看 Powerpoint 文件:图 3 - IronXL 输出

要了解有关 IronXL 代码示例的更多信息,请参阅 这里.

结论

一些软件应用程序需要使用 C# 将 PowerPoint 演示文稿转换为图像。无论是否使用了Microsoft.Office.Interop.PowerPoint命名空间,这一过程都可能相当迅速地完成。借助本文的代码示例,您可以轻松地将 PowerPoint 转换为图像,并将其纳入 C# 程序,从而为信息更改和传递开辟一片广阔的天地。

铁XL IronXL 提供了一种在 C# 中进行 Excel 操作的简单而高效的方法,无需在目标系统中安装 Excel 或依赖 Interop 库。IronXL 具有全面的功能集和用户友好的 API,是开发人员在 C# 应用程序中处理 Excel 数据的得力工具。它简化了 Excel 文件的读写和编辑等任务。对于 Excel 相关的开发项目,IronXL 提供了一个稳定的解决方案,无论您是处理数据、制作报表还是自动执行电子表格任务,它都能提高工作效率和灵活性。

IronXL 提供免费的社区版,但对非商业使用有限制。付费版本的起价为"$liteLicense",可通过订阅或永久方式获得。 许可证制度.它们的功能更全面,提供的功能和支持也更多。IronXL 还提供 免费试用许可证.有关许可证颁发的最新综合信息,请访问 许可证页面.访问此处 网站 了解有关 Iron Software 产品的更多信息。

下一步 >
如何在C#中使用模板创建PowerPoint

准备开始了吗? 版本: 2024.9 刚刚发布

免费NuGet下载 总下载量: 988,189 查看许可证 >