在生产环境中测试,无水印。
随时随地满足您的需求。
获得30天的全功能产品。
几分钟内就能启动并运行。
在您的产品试用期间,全面访问我们的支持工程团队。
在软件开发领域,要想提供丰富而吸引人的用户体验,就必须具备将多种媒体融入程序的能力。 由于 Microsoft Office PowerPoint 演示文稿经常用于交流信息,因此在 C# WinForms 程序中加入 PowerPoint 查看器控件非常有利。 本文介绍了如何集成此类控件,使开发人员能够在其应用程序中添加 PowerPoint 查看功能并对其进行改进。 在本文中,我们将在不安装MS PowerPoint Viewer的情况下创建C# PowerPoint Viewer。
创建 PowerPoint 应用程序实例。
将 Interop 参考添加到项目中
使用实例打开演示文稿。
检查并为导出文件创建输出文件夹。
将创建的幻灯片图像加载到图片框中。 使用按钮在幻灯片之间移动。
PowerPoint 演示文稿广泛应用于教育和商业等多个领域。 在 WinForms 应用程序中加入 PowerPoint 查看器控件有几个优点。
首先添加对必要程序集的引用:要在您的项目中添加Microsoft.Office.Interop.PowerPoint引用,请在Visual Studio中右键点击项目,选择“添加” > “引用”,然后从COM选项卡中添加它们。
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
首先,我们导入所需的命名空间。 文件操作由System.IO处理,System.Windows.Forms提供WinForms功能,而用于与PowerPoint交互的Microsoft.Office类包含在PowerPoint中。 在 PowerPointViewerApp 命名空间内,我们定义了一个名为 PowerPointViewer 的 WinForms Form 类。 视图 PPT 应用程序的主要用户界面将是此表格。
宣布PowerPoint应用程序和演示文稿的引用将存储在两个私有字段中,pptApplication和pptPresentation。 通过使用InitializeComponent(),构造函数设置由设计器定义的表单的视觉组件并初始化表单。 当表单加载时,会调用 InitializeComponent() 函数。 此时,PowerPoint 应用程序 (pptApplication) 已初始化并可访问。
接下来,我们启动pptFilePath指定的PowerPoint文件。 使用pptApplication.Presentations,我们通过将其路径(pptFilePath)提供给Open()函数来打开PowerPoint文件。 该过程将返回一个 Presentation 对象,该对象代表已打开的演示文稿。
要显示PowerPoint演示文稿的第一页幻灯片,我们使用DisplaySlide()函数。 第一张幻灯片的索引为 1,图片框控件中显示的幻灯片就是该功能的结果。 我们使用Export()函数将选定的幻灯片导出为PNG文件。导出的PNG文件暂时存储在系统的临时文件夹中。 使用ImageLocation()方法,我们将导出的PNG文件加载到PictureBox控件中。 这些技术可以在幻灯片之间进行翻转。
导航按钮btnPrevious_Click和btnNext_Click分别带您进入上一个和下一个幻灯片。 使用pptPresentation,我们可以在slideIndex变量中获取幻灯片的索引。 如果当前幻灯片索引在1到所有幻灯片数量的有效范围内,我们分别使用前一张或下一张幻灯片的索引调用DisplaySlide()。
当表单关闭时,将调用Close()函数。 在这里,我们通过退出PowerPoint程序并使用Quit()方法终止PowerPoint应用程序来确保资源管理正确进行,从而释放系统资源。
在 C# 中轻松操控 Excel 文件, 使用流行的 .NET Excel 库 IronXL。 Excel 是一款多功能工具,具有读取、生成和修改 Excel 文件的丰富功能,适用于各种应用。
下面,我将介绍 IronXL 的一些关键特性:
由于 IronXL.Excel 的存在,Excel 电子表格中的单个单元格可能会被准确修改。 开发人员可以通过编程设置单元格值、公式、样式、格式和其他功能。
要了解更多关于文档的信息,请参考这里。
在继续之前,让我们首先使用 NuGet 包管理器控制台安装 IronXL:
Install-Package IronXL.Excel
IronXL 安装后可在我们的 C# 项目中使用。
让我们来看一个假设场景:我们想使用 IronXL.Excel 从 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
我们首先要添加必要的命名空间。 IronXL 命名空间包含 IronXL 库提供的类和方法。 我们要阅读的 Excel 文件 sample.xlsx 的路径已给出。 利用WorkBook,加载Excel文件。 Load() 方法提供的 Workbook 对象代表 Excel 工作簿。 我们可以通过使用 workbook.WorkSheets [0] 来访问工作簿的第一个工作表。 我们对工作表的每一行和每一列进行堆叠循环。 我们将每个单元格的值发送到控制台。
要了解更多关于IronXL代码示例的信息,请参考这里。
一些软件应用程序需要使用 C# 将 PowerPoint 演示文稿转换为图像。 无论是否使用Microsoft.Office.Interop.PowerPoint 命名空间,过程可能会非常快速地完成。 借助本文的代码示例,您可以轻松地将 PowerPoint 转换为图片,并将其纳入 C# 程序,为信息修改和传递开辟一片天地。
IronXL 提供了一种简单而高效的方法,在 C# 中执行 Excel 操作,而无需在目标系统上安装 Excel 或依赖 Interop 库。 IronXL.Excel 具有全面的功能集和用户友好的 API,是开发人员在 C# 应用程序中处理 Excel 数据的得力工具。 它简化了读取、编写和编辑 Excel 文件等任务。 对于 Excel 相关的开发项目,IronXL.Excel 提供了一个稳定的解决方案,无论您是处理数据、制作报告还是自动执行电子表格任务,它都能提高工作效率和灵活性。
IronXL 提供了免费的社区版,但对非商业使用有限制。 付费版本起价为$749,可以通过订阅或永久性的授权方案获取。 这些工具功能更全面,提供的功能和支持更多。 IronXL 还提供免费试用许可证。 有关许可的全面和最新信息,请访问许可证页面。 访问此网站以了解有关Iron Software产品的更多信息。