EXCEL 工具

如何在 C# 中查看 PowerPoint 文件

發佈 2024年4月3日
分享:

介紹

在軟體開發領域,提供豐富且吸引人的用戶體驗需要能夠將多種媒體類型融入到程式中。由於 Microsoft Office PowerPoint 簡報經常用來傳達資訊,將 PowerPoint 檢視控制項整合到 C# WinForms 程式中是有利的。本文說明如何整合這種控制項,使開發人員能夠向應用程式添加 PowerPoint 檢視功能並加以改進。在本文中,我們將創建 C# PowerPoint 檢視器,無需安裝 MS PowerPoint 檢視器。

如何在C#中查看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 功能,而 Microsoft.Office 用於與 PowerPoint 互動的類別包含在 PowerPoint 中。在 PowerPointViewerApp 命名空間內,我們定義了一個名為 PowerPointViewer 的 WinForms 表單類別。這個表單將是查看 PPT 應用的主要使用者介面。

宣布將 PowerPoint 應用和簡報的引用存儲在兩個私有欄位,pptApplicationpptPresentation 中。通過使用 InitializeComponent(),構造函數設置了由設計器定義的表單視覺元件並初始化表單。當表單加載時,InitializeComponent()。() 函數被調用。PowerPoint應用程式 (pptApplication) 在此時初始化並設置可訪問。

接下來,我們啟動由pptFilePath指定的PowerPoint文件。使用pptApplication.Presentations,我們通過提供其路徑打開PowerPoint文件 (pptFilePath) 到打開()函數。一個表示已打開簡報的 Presentation 物件會由該程序返回。

要顯示 PowerPoint 簡報的第一張幻燈片,我們使用 DisplaySlide()函式。第一張幻燈片的索引是1。在Picture box控制項中顯示的幻燈片這是此函式的結果。我們使用Export()將選中的幻燈片匯出為PNG文件的功能。匯出的PNG文件暫時存儲在系統的臨時文件夾中。使用ImageLocation()方法中,我們將匯出的 PNG 文件載入 PictureBox 控制項。這些技術處理了幻燈片之間的切換。

導航按鈕 btnPrevious_ClickbtnNext_Click 分別帶你到上一張和下一張幻燈片。使用 pptPresentation,我們能夠在 slideIndex 變數中獲取幻燈片的索引。我們調用 DisplaySlide() 如果當前幻燈片的索引在 1 到整個幻燈片數量的有效範圍內,則分別使用前一張或下一張幻燈片的索引。

當表單關閉時,關閉() 函數被調用。在這裡,我們通過退出 PowerPoint 程序並使用Quit 終止 PowerPoint 應用程序來確保資源管理正確完成。() 方法。

PowerPoint 文件

如何在 C# 中查看 Powerpoint 文件:圖 1 - PowerPoint 文件 demo.pptx

輸出:PowerPoint 檢視器

如何在C#中查看PowerPoint文件:圖2 - PowerPointViewercs

IronXL

使用流行的 .NET Excel 函式庫,在 C# 中更輕鬆地操作 Excel 文件 IronXL這是一個多功能工具,由於其廣泛的功能集,可用於各種應用程式,適用於讀取,生成和修改 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

在安裝後,您可以在我們的 C# 項目中使用 IronXL。

使用 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 庫提供的類別和方法。範例檔案 sample.xlsx 的路徑被給出,我們想要讀取的 Excel 檔案。利用 WorkBook,Excel 檔案被載入。由 Load 提供的 Workbook 物件。() 方法代表 Excel 活頁簿。我們可以通過使用 workbook.WorkSheets 訪問活頁簿的第一個 工作表 [0]**. 我們使用巢狀的 for each 迴圈遍歷工作表的列和欄。我們將每個儲存格的值發送到控制台。

如何在 C# 中查看 Powerpoint 文件:圖 3 - IronXL 輸出

若要了解有關IronXL代碼範例的更多資訊,請參閱 這裡.

結論

許多軟件應用程序需要使用 C# 將 PowerPoint 演示文稿轉換為圖像。不論是否使用 Microsoft.Office.Interop.PowerPoint 命名空間,該過程可能都會非常迅速地完成。憑藉本篇文章中的代碼範例,您可以輕鬆地將 PowerPoint 轉圖像功能整合到您的 C# 程式中,從而開啟信息改變和傳遞的無限可能性。

IronXL 提供了一種簡單且高效的方法在 C# 中進行 Excel 操作,而無需在目標系統上安裝 Excel 或依賴 Interop 庫。IronXL 功能全面且用戶友好的 API,對於在 C# 應用程序中處理 Excel 數據的開發者來說是個方便的工具。它簡化了讀取、寫入和編輯 Excel 文件等任務。對於與 Excel 相關的開發項目,IronXL 提供了一個穩定的解決方案,提高了生產力和靈活性,不論是處理數據、生成報告,還是自動化電子表格任務。

IronXL 提供了一個具有非商業用途限制的免費社區版。付費版本的起價為 $749,可通過訂閱或永久基礎獲取。 授權方案它們功能更加齊全,提供更多的功能和支援。IronXL還提供一個 免費試用許可證如需關於授權的全面和最新資訊,請訪問 授權頁面. 訪問這個 網站 了解更多關於 Iron Software 產品的資訊。

下一個 >
如何在 C# 中使用模板創建 PowerPoint

準備開始了嗎? 版本: 2024.10 剛剛發布

免費 NuGet 下載 總下載次數: 1,023,839 查看許可證 >