EXCEL 工具 如何使用 C# 從模板創建 PowerPoint Jordi Bardia 更新日期:6月 22, 2025 Download IronXL NuGet 下載 DLL 下載 Start Free Trial Copy for LLMs Copy for LLMs Copy page as Markdown for LLMs Open in ChatGPT Ask ChatGPT about this page Open in Gemini Ask Gemini about this page Open in Grok Ask Grok about this page Open in Perplexity Ask Perplexity about this page Share Share on Facebook Share on X (Twitter) Share on LinkedIn Copy URL Email article PowerPoint 簡報仍然是有效傳遞商業、教育及其他領域資訊的標準。 PowerPoint 是一個靈活的工具,無論是教學、向團隊更新項目進度,或向潛在投資者進行簡報,它都能提升您的溝通效果。 然而,從頭製作簡報可能需要大量時間,而設計的效率和一致性可能不如預期。 這時 PowerPoint 範本派上用場,提供預製的幻燈片,具有可根據需求修改的主題和版面。 role="alert"> 推出 IronPPT: 來自 Iron Software 的 .NET PowerPoint 庫 IronPPT 無縫讀取和保存 PPTX 文件 - 不需要 Microsoft Office。非常適合在任何 .NET 應用程序中自動化幻燈片、文本、形狀和圖像。立即開始使用 IronPPT! 在這篇文章中,我們將看看如何使用 C# 從範本創建一個 PowerPoint。 我們可以通過使用 Microsoft [PowerPoint Interop library](https://learn.microsoft.com/en-us/previous-versions/office/office-12/ff764034(v=office.12) 來加速過程,並使客戶能夠輕鬆創建精美的簡報。 如何在 C# 中使用範本創建 PowerPoint 創建一個新的 C# 專案。 在一個新的實例中啟動 PowerPoint 程式。 將範本中的幻燈片插入到新創建的簡報中。 創建一個新文件並保存簡報。 關閉 PowerPoint 程式並結束簡報。 理解從範本製作 PowerPoint 在進入程式碼之前,首先回顧一下程式化撰寫 PowerPoint 簡報的基本概念。 通過 Microsoft 的 PowerPoint Interop library,開發人員可以使用 .NET 語言如 C# 與 PowerPoint 應用程序進行溝通。 此包公開了許多功能,包括創建幻燈片、添加文本、插入圖片和應用格式等功能。 創建一個新的 Visual Studio 專案 要開始新的 Visual Studio 專案,請按照以下指示操作。 啟動 Visual Studio 應用程式。 在使用 Visual Studio 之前,請確保已在計算機上安裝它。 選擇檔案,然後是新的,最後是專案。 從 "創建新專案" 方框的左側,選擇您希望的編程語言(例如 C#)。 接下來,從可用的專案範本清單中選擇 "控制台應用程序" 或 "控制台應用程序 (.NET Core)" 範本。 請填寫 "名稱" 欄位以給您的專案命名。 決定專案將儲存的位置。 點擊 "下一步" 以開始在新的控制台應用程序專案上工作。 然後選擇相應的 .NET Framework 並點擊 "創建"。 在新建立的 C# 專案中,添加 Microsoft PowerPoint Interop library 的參考。 現在,由於這個庫,開發和與簡報互動變得更容易,它使得 C# 程式碼和 PowerPoint 應用程序之間的溝通得以實現。 使用 C# 從範本創建 PowerPoint 加載PowerPoint範本作為我們新簡報的基礎是第一步。範本中經常可以看到預定義的幻燈片布局、主題和內容占位符。 Microsoft.Office.PowerPoint.Interop library 的功能允許我們從 C# 源碼中打開範本文件並檢索其內容。 using PowerPoint = Microsoft.Office.Interop.PowerPoint; using Microsoft.Office.Core; // Make sure to include this for MsoTriState class Program { static void Main(string[] args) { // Initialize PowerPoint application PowerPoint.Application powerpointApp = new PowerPoint.Application(); // Open an existing PowerPoint file PowerPoint.Presentation presentation = powerpointApp.Presentations.Open( @"C:\Path\To\Existing\output.pptx", MsoTriState.msoFalse, MsoTriState.msoFalse, MsoTriState.msoTrue); // Insert slides from a template file into the presentation presentation.Slides.InsertFromFile( @"C:\Path\To\External\demo.pptx", presentation.Slides.Count + 1, 1, 0); // Save the updated presentation presentation.Save(); // Close the presentation and quit PowerPoint application presentation.Close(); powerpointApp.Quit(); } } using PowerPoint = Microsoft.Office.Interop.PowerPoint; using Microsoft.Office.Core; // Make sure to include this for MsoTriState class Program { static void Main(string[] args) { // Initialize PowerPoint application PowerPoint.Application powerpointApp = new PowerPoint.Application(); // Open an existing PowerPoint file PowerPoint.Presentation presentation = powerpointApp.Presentations.Open( @"C:\Path\To\Existing\output.pptx", MsoTriState.msoFalse, MsoTriState.msoFalse, MsoTriState.msoTrue); // Insert slides from a template file into the presentation presentation.Slides.InsertFromFile( @"C:\Path\To\External\demo.pptx", presentation.Slides.Count + 1, 1, 0); // Save the updated presentation presentation.Save(); // Close the presentation and quit PowerPoint application presentation.Close(); powerpointApp.Quit(); } } Imports PowerPoint = Microsoft.Office.Interop.PowerPoint Imports Microsoft.Office.Core ' Make sure to include this for MsoTriState Friend Class Program Shared Sub Main(ByVal args() As String) ' Initialize PowerPoint application Dim powerpointApp As New PowerPoint.Application() ' Open an existing PowerPoint file Dim presentation As PowerPoint.Presentation = powerpointApp.Presentations.Open("C:\Path\To\Existing\output.pptx", MsoTriState.msoFalse, MsoTriState.msoFalse, MsoTriState.msoTrue) ' Insert slides from a template file into the presentation presentation.Slides.InsertFromFile("C:\Path\To\External\demo.pptx", presentation.Slides.Count + 1, 1, 0) ' Save the updated presentation presentation.Save() ' Close the presentation and quit PowerPoint application presentation.Close() powerpointApp.Quit() End Sub End Class $vbLabelText $csharpLabel 加載範本後,我們使用它的佈局創建一個新的簡報。 我們使用 PowerPoint 演示文件(output.pptx),這可以幫助我們創建一個 PowerPoint 演示物件,這將成為我們材料的畫布。 範本的佈局和主題將被帶入這個簡報中,使它具有統一的視覺風格。 presentation.Slides.InsertFromFile() 方法需要四個參數:FileName、Index、SlideStart 和 SlideEnd。 指定您希望包含在您的演示中的幻燈片的 PowerPoint 文件的路徑作為第一個參數(demo.pptx)。 下一個參數是您希望插入幻燈片在目標呈現中的位置。 其他兩個參數是可選的。 這些參數允許我們指定開始和結束幻燈片索引。 如果我們需要提取特定範圍內的幻燈片,我們可以使用這些參數。 在使用 InsertFromFile() 方法插入幻燈片後,可以使用presentation.Save() 方法保存演示。 保存後,使用 Close() 方法關閉 PowerPoint 程序,然後使用 Quit() 方法退出演示。 通過從外部文件動態添加幻燈片的方式,您可以改善 PowerPoint 簡報的內容和靈活性。 輸出 IronXL 介紹一個功能豐富的用於處理 Excel 文件的 C# 庫,稱為 IronXL。 Iron Software 的 IronXL 提供了多種工具,可動態創建、填充和格式化 Excel 文件。 憑藉其易用的API和詳細的文檔,IronXL 簡化了 Excel 在 C# 中的交互,為開發者提供了無縫的 Excel 相關任務體驗。 IronXL 的特點 廣泛的 Excel 支持: IronXL 能夠以各種 Excel 格式開啟和操作大量的 Excel 文件,如 CSV、XLS 和 XLSX 文件格式。 開發者可以通過 IronXL 的強大解析功能,有效地從舊版和現代 Excel 文件中提取數據。 高速度: IronXL 優先考慮性能優化。 使用高效的算法和內存管理策略,確保 Excel 的交互可靠且快速。 IronXL 提供減少的內存負擔和優化的處理速度,因此開發者可以輕鬆管理大型 Excel 文件。 簡單和用戶友好的API: IronXL 的用戶友好API使其適合各種技能水平的開發人员。 IronXL 提供簡單的方法來讀取 Excel 文件、存取 Excel 工作簿和從單元格中獲取數據,降低了 C# 開發者的學習曲線,加速了創建和讀取現有文件的過程。 要了解更多關於 IronXL 文檔的信息,請參考這裡。 安裝 IronXL 要安裝 IronXL,請使用下面步驟提供的指示和命令: 在 Visual Studio 中,導航到工具 -> NuGet 程式包管理器 -> 程式包管理器控制台。 在程式包管理器的控制台標籤中輸入以下命令: Install-Package IronXL.Excel IronXL 程式包在下載並安裝到當前專案後,即可使用。 使用 IronXL 創建 Excel 现在,讓我們查看一個實際的代碼示例,展示如何使用 IronXL 在 C# 中寫入數據到 Excel 文檔中。 我們將演示如何啟動一個新的 Excel 工作簿,填充一個工作表,然後將信息保存到一個文件中: using IronXL; class Program { static void Main(string[] args) { // Create a new WorkBook object WorkBook workBook = WorkBook.Create(ExcelFileFormat.XLSX); // Add a new WorkSheet to the workbook WorkSheet workSheet = workBook.CreateWorkSheet("Sheet1"); // Define sample data string[,] data = { { "Name", "Age", "City" }, { "Superman", "35", "Metropolis" }, { "Batman", "34", "Gotham City" }, { "Flash", "30", "Central City" } }; // Populate the worksheet with data for (int row = 0; row < data.GetLength(0); row++) { for (int col = 0; col < data.GetLength(1); col++) { workSheet.SetCellValue(row, col, data[row, col]); } } // Save the workbook to a .xlsx file workBook.SaveAs("Demo.xlsx"); // Close the workbook workBook.Close(); } } using IronXL; class Program { static void Main(string[] args) { // Create a new WorkBook object WorkBook workBook = WorkBook.Create(ExcelFileFormat.XLSX); // Add a new WorkSheet to the workbook WorkSheet workSheet = workBook.CreateWorkSheet("Sheet1"); // Define sample data string[,] data = { { "Name", "Age", "City" }, { "Superman", "35", "Metropolis" }, { "Batman", "34", "Gotham City" }, { "Flash", "30", "Central City" } }; // Populate the worksheet with data for (int row = 0; row < data.GetLength(0); row++) { for (int col = 0; col < data.GetLength(1); col++) { workSheet.SetCellValue(row, col, data[row, col]); } } // Save the workbook to a .xlsx file workBook.SaveAs("Demo.xlsx"); // Close the workbook workBook.Close(); } } Imports IronXL Friend Class Program Shared Sub Main(ByVal args() As String) ' Create a new WorkBook object Dim workBook As WorkBook = WorkBook.Create(ExcelFileFormat.XLSX) ' Add a new WorkSheet to the workbook Dim workSheet As WorkSheet = workBook.CreateWorkSheet("Sheet1") ' Define sample data Dim data(,) As String = { { "Name", "Age", "City" }, { "Superman", "35", "Metropolis" }, { "Batman", "34", "Gotham City" }, { "Flash", "30", "Central City" } } ' Populate the worksheet with data For row As Integer = 0 To data.GetLength(0) - 1 For col As Integer = 0 To data.GetLength(1) - 1 workSheet.SetCellValue(row, col, data(row, col)) Next col Next row ' Save the workbook to a .xlsx file workBook.SaveAs("Demo.xlsx") ' Close the workbook workBook.Close() End Sub End Class $vbLabelText $csharpLabel 在這個代碼示例中,我們首先使用 IronXL 的 Create() 函數創建一個新的 WorkBook 物件,指定所需的 Excel 文件格式。 下一步是在工作簿中創建一個新的工作表,並定義一些樣本數據在一個二維數組中。 接下來,我們利用嵌套循環來訪問和設置不同單元格的值,以填充工作表的樣本數據。 為了釋放系統資源,我們在使用 SaveAs() 方法將其保存到一個名為 "Demo.xlsx" 的文件後關閉工作簿。 同樣,我們可以為目標文件生成多個工作表。 下面顯示的是作為輸出的 Excel 文件。 要獲取有關寫入 Excel 文件的更多信息,請參考程式碼範例頁面。 結論 總而言之,利用 C#從範本創建 PowerPoint 簡報使用户能提高設計的一致性,加速工作流程,並創造出具有影響的内容。 開發者可以透過自動化創建過程並使用 Microsoft PowerPoint Interop library 的功能來節省時間和精力,並确保專業效果。 學習這種方法將幫助你作為演講者、商業專業人士或教育者提高演示技巧,並通過引人注目的幻燈片吸引您的觀眾。 對於 C# 開發者, IronXL 是一個 Microsoft Excel 的強大替代品,提供對 Excel 的完整支持、優秀的性能以及與 .NET 框架的無縫整合。 IronXL 的用戶友好API和對 Excel 文檔的精細控制簡化了 C# 中的 Excel 寫作。 這促進開發者自動化 Excel 相關任務、導出數據和生成動態報告的過程。 無論是為桌面、 Web 還是移動應用創建 Excel 文件, C#開發者都可以依靠 IronXL 簡化 Excel 相關任務並在他們的 C# 程序中發揮 Excel 的全部潛力。 IronXL 在首次啟動時價格為 $799。 另外,用户可以選擇付費一年會員費以獲取產品支援和更新。 IronXL 提供保障以防止未經授權的擴散,收費詳情請造訪此授權頁面。 點擊此網站連結查看更多 Iron Software。 View more about Iron Software by clicking this website link. Jordi Bardia 立即與工程團隊聊天 軟體工程師 Jordi 在 Python、C# 和 C++ 上最得心應手,當他不在 Iron Software 展現技術時,便在做遊戲編程。在分担产品测测试,产品开发和研究的责任时,Jordi 为持续的产品改进增值。他说这种多样化的经验使他受到挑战并保持参与, 而这也是他与 Iron Software 中工作一大乐趣。Jordi 在佛罗里达州迈阿密长大,曾在佛罗里达大学学习计算机科学和统计学。 相關文章 更新日期 9月 10, 2025 如何在 C# 中查看 PowerPoint 文件 在本文中,我們將創建不需安裝 MS PowerPoint 檢視器的 C# PowerPoint 檢視器。 閱讀更多 更新日期 6月 22, 2025 .NET Regex 測試器測試您的正則表達式模式 本文深入探討 .NET Regex Tester 的功能,提供見解和使用示例,並與 IronXL 完美結合 閱讀更多 更新日期 6月 22, 2025 在 Java 中讀取 Excel 文件(教程) 读取 Excel 文件有时可能很复杂。在 Java 中读取 Excel 文件与在 Java 中读取 Word 文件稍有不同,因为 Excel 的单元格。 閱讀更多 如何在 C# 中查看 PowerPoint 文件.NET Regex 測試器測試您的正...
更新日期 6月 22, 2025 .NET Regex 測試器測試您的正則表達式模式 本文深入探討 .NET Regex Tester 的功能,提供見解和使用示例,並與 IronXL 完美結合 閱讀更多
更新日期 6月 22, 2025 在 Java 中讀取 Excel 文件(教程) 读取 Excel 文件有时可能很复杂。在 Java 中读取 Excel 文件与在 Java 中读取 Word 文件稍有不同,因为 Excel 的单元格。 閱讀更多