在實際環境中測試
在生產環境中測試無浮水印。
在任何需要的地方都能運作。
需要轉換 PowerPoint 在軟體開發領域中,將簡報轉換為圖片格式的需求頻繁出現。許多開發人員發現能夠以編程方式將 PowerPoint 文件轉換為照片非常有用,無論是用於預覽生成、縮略圖創建,還是系統集成。本文將說明如何使用 C# 將 ppt 轉換為圖片,並包括一些範例代碼來幫助你完成這一操作。
創建 PowerPoint 應用實例。
使用該實例打開演示文稿。
檢查並創建輸出文件夾。
遍歷幻燈片並將其導出為圖像。
在進入詳細內容之前,讓我們快速看看將 PowerPoint 投影片轉換成照片的重要性。即使 PowerPoint 是一個製作動態簡報的好工具,但並不是每次分享這些文件時都能以原始格式進行。有時只需要某些特定的投影片或從簡報中拍攝的照片,其他時候,不同的系統和設定可能不支持直接渲染 PowerPoint 文件。將 PowerPoint 簡報轉換成圖片提供了一個簡便的解決方案,可在各種設備和應用上輕鬆分享和查看。
有幾種方法可以在 C# 中將 PowerPoint 演示文稿轉換為圖片。使用 Microsoft.Office.Interop.PowerPoint 命名空間,提供用於程式化介接 PowerPoint 應用程式的類別和方法,是一種流行的方法。提供處理 PowerPoint 文件的廣泛功能,是另一種策略。
按照以下步驟建立新的 Visual Studio 專案:
打開 Visual Studio IDE。確保您已安裝 Visual Studio 在使用之前,請在您的電腦上安裝。
啟動一個新項目:
選擇文件,新建,然後選擇項目。
在「建立新專案」框中,選擇你喜愛的程式語言 (例如 C#) 從左側選擇。
接下來,選擇「Console App」或「Console App」 (.NET Core)從可用的專案模板列表中選擇一個模板。
請完成“名稱”部分,給您的專案命名。
選擇專案的儲存位置。
點擊「建立」開始進行新的控制台應用程式專案。
首先,讓我們看看如何使用 Microsoft.Office.Interop.PowerPoint 命名空間將 PowerPoint 幻燈片轉換為圖片。請確保將所需的程序集安裝並添加到您的 C# 專案中作為引用。這些程序集通常可以通過直接引用 InterOp 程序集或安裝 Microsoft Office 主互操作程序集來找到。 (PIA).
using System.IO;
using Microsoft.Office.Interop.PowerPoint;
class Program
{
static void Main(string [] args)
{
string pptFilePath = "demo.pptx"; // Path to your PowerPoint file
string outputFolder = "output_images"; // Output folder path where images will be saved
ConvertPptToImages(pptFilePath, outputFolder);
}
static void ConvertPptToImages(string pptFilePath, string outputFolder)
{
Application pptApplication = new Application();
Presentation pptPresentation = pptApplication.Presentations.Open(pptFilePath, MsoTriState.msoFalse, MsoTriState.msoFalse, MsoTriState.msoFalse);
if (!Directory.Exists(outputFolder))
Directory.CreateDirectory(outputFolder);
int slidesCount = pptPresentation.Slides.Count;
for (int i = 1; i <= slidesCount; i++)
{
string outputPath = Path.Combine(outputFolder, $"Slide{i}.png");
pptPresentation.Slides [i].Export(outputPath, "png", 1024, 768);
//saving the presentation slides into png images
}
pptPresentation.Close();
pptApplication.Quit();
}
}
using System.IO;
using Microsoft.Office.Interop.PowerPoint;
class Program
{
static void Main(string [] args)
{
string pptFilePath = "demo.pptx"; // Path to your PowerPoint file
string outputFolder = "output_images"; // Output folder path where images will be saved
ConvertPptToImages(pptFilePath, outputFolder);
}
static void ConvertPptToImages(string pptFilePath, string outputFolder)
{
Application pptApplication = new Application();
Presentation pptPresentation = pptApplication.Presentations.Open(pptFilePath, MsoTriState.msoFalse, MsoTriState.msoFalse, MsoTriState.msoFalse);
if (!Directory.Exists(outputFolder))
Directory.CreateDirectory(outputFolder);
int slidesCount = pptPresentation.Slides.Count;
for (int i = 1; i <= slidesCount; i++)
{
string outputPath = Path.Combine(outputFolder, $"Slide{i}.png");
pptPresentation.Slides [i].Export(outputPath, "png", 1024, 768);
//saving the presentation slides into png images
}
pptPresentation.Close();
pptApplication.Quit();
}
}
Imports System.IO
Imports Microsoft.Office.Interop.PowerPoint
Friend Class Program
Shared Sub Main(ByVal args() As String)
Dim pptFilePath As String = "demo.pptx" ' Path to your PowerPoint file
Dim outputFolder As String = "output_images" ' Output folder path where images will be saved
ConvertPptToImages(pptFilePath, outputFolder)
End Sub
Private Shared Sub ConvertPptToImages(ByVal pptFilePath As String, ByVal outputFolder As String)
Dim pptApplication As New Application()
Dim pptPresentation As Presentation = pptApplication.Presentations.Open(pptFilePath, MsoTriState.msoFalse, MsoTriState.msoFalse, MsoTriState.msoFalse)
If Not Directory.Exists(outputFolder) Then
Directory.CreateDirectory(outputFolder)
End If
Dim slidesCount As Integer = pptPresentation.Slides.Count
For i As Integer = 1 To slidesCount
Dim outputPath As String = Path.Combine(outputFolder, $"Slide{i}.png")
pptPresentation.Slides (i).Export(outputPath, "png", 1024, 768)
'saving the presentation slides into png images
Next i
pptPresentation.Close()
pptApplication.Quit()
End Sub
End Class
使用 Microsoft.Office.Interop.PowerPoint; 聲明導入需要用於處理 PowerPoint 應用程式的 C# 命名空間。程式的進入點是 Main 方法。它指定輸出資料夾。 (輸出資料夾), 這是保存創建的照片的位置,還有 PowerPoint 文件的路徑 (pptFilePath)實際將 PowerPoint 簡報轉換為照片是透過此方法處理的。以下是用於上述示例代碼的 PowerPoint ppt。
應用程序 pptApplication = new Application()"; 用於啟動 PowerPoint 程式的新實例。這能夠提供與 PowerPoint 的程式化互動。使用 pptApplication.Presentations,我們打開由 pptFilePath.Open 指定的 PowerPoint 演示文稿文件。"()function。此函數返回表示已打開簡報的 Presentation 物件。我們判斷輸出資料夾 "outputFolder" 是否存在。如果不存在,我們使用方法 Directory.CreateDirectory() 來創建它。
我們使用 for 迴圈來遍歷演示文稿中的每張幻燈片。pptPresentation 使用 Slides.Count 屬性提供幻燈片的總數。我們使用輸出資料夾路徑和幻燈片索引來為每張幻燈片的圖片創建輸出路徑。 (作為 幻燈片{我}.png)接下來,我們使用pptPresentation將PowerPoint投影片匯出為圖片。 (在此範例中,以 JPG 影像、PNG 影像格式). 使用 匯出() 函數。參數是圖片格式 (“png” 格式) 和大小 (寬度:1024,高度:768)最後,我們使用pptPresentation來結束報告。关闭() 並使用 pptApplication 結束 PowerPoint 會話。為了適當釋放系統資源,請使用 Quit().
一個受歡迎的 .NET 框架,被稱為 IronXL 讓操縱 C# 中的 Excel 文件變得更容易。憑藉其廣泛的功能集用於讀取、創建和編輯 Excel 文件,它是一種適用於廣泛用途的靈活工具。我將在下面介紹一些 IronXL 的主要功能:
如需了解更多有關文檔的資訊,請參閱 這裡.
在繼續之前,讓我們先使用 NuGet 套件管理控制台安裝 IronXL:
Install-Package IronXL.Excel
安裝完成後,IronXL可以在我們的C#項目中使用。
讓我們來看看一個假設的情況,我們希望使用 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
首先,我們包含所需的命名空間。IronXL庫提供的類和方法包含在IronXL命名空間中。這是我們希望讀取的Excel文件的路徑。 (範例.xlsx) 被指定。WorkBook 用來載入 Excel 檔案。Excel 工作簿由 WorkBook 物件表示,這個物件由 Load 返回。()function. 使用 workbook,我們可以訪問 workbook.WorkSheets 中的第一張工作表 [0]使用嵌套的 foreach 循環,我們遍歷工作表的行和列。我們將每個單元格的值輸出到控制台。
要了解更多代碼,請參考 這裡.
在許多軟體應用程式中,可以必須使用 C# 將 PowerPoint 簡報轉換為照片。不論是否使用 Microsoft.Office.Interop.PowerPoint 命名空間,此過程都可以相對快速地完成。本文的程式碼範例將幫助您輕鬆地將 PowerPoint 到圖片的轉換功能整合到您的 C# 應用程式中,創造出許多資訊分發和修改的機會。
不需要在目標機器上安裝 Excel 或依賴 Interop 程式庫, IronXL 提供了一個在 C# 中快速有效執行 Excel 操作的方法。使用 IronXL 來處理 C# 應用程式中的 Excel 資料的開發人員會發現它很有用,因為它通過其用戶友好的 API 和廣泛的功能集來簡化操作,如讀取、寫入和更改 Excel 文件。IronXL 提供了一個可靠的解決方案,提高了與 Excel 相關的開發項目的效率和適應性,無論您是在生成報告、處理數據,還是自動化電子表格工作。 免費試用它們功能更完善,提供更多功能和支持。訪問 IronXL 網站 有關授權的全面和最新資訊,請訪問此 網站 了解更多關於 Iron Software 產品的資訊。