如何使用範本在 C# 中建立 PowerPoint
PowerPoint簡報繼續成為商業、教育和其他領域中有效傳達資訊的標準。 無論您是在教課、向團隊提供專案更新,還是向潛在投資者做簡報,PowerPoint都是可以提高您的溝通能力的靈活工具。 然而,從零開始製作簡報可能會花費大量時間,而且設計的效率和統一性可能並不總是符合需求。 這時,PowerPoint範本就派上用場了。它提供了可用主題和版式預製的幻燈片,您可以根據需求進行調整。
IronPPT無縫載入和保存PPTX檔案 - 無需Microsoft Office。在任何.NET應用中完美自動化幻燈片、文字、形狀和圖片。立即開始使用IronPPT!
在本文章中,我們將探討如何使用C#從範本建立PowerPoint。 透過使用Microsoft [PowerPoint互操作程式庫](https://learn.microsoft.com/en-us/previous-versions/office/office-12/ff764034(v=office.12),我們可以加速流程,讓客戶輕鬆建立精緻的簡報。
如何在C#中使用範本建立PowerPoint
- 建立一個新的C#專案。
- 在新實例中啟動PowerPoint程式。
- 從範本中插入幻燈片到新建立的簡報中。
- 建立一個新檔案並保存簡報。
- 關閉PowerPoint程式並結束簡報。
理解從範本建立PowerPoint
在開始編寫程式碼之前,先回顧下程式化編寫PowerPoint簡報的基本概念。 使用Microsoft的PowerPoint互操作程式庫,開發者可以使用.NET語言如C#與PowerPoint應用溝通。 此包提供許多功能,包括建立幻燈片、新增文字、插入圖片和應用格式的能力。
建立一個新的Visual Studio專案
要啟動一個新的Visual Studio專案,請按照以下步驟進行。
啟動Visual Studio應用程式。 在使用Visual Studio之前,請確保已在電腦上安裝它。
選擇檔案,然後選擇新建,最後選擇專案。

在"建立新專案"框的左側從選單選擇您的首選程式語言,例如C#。 接下來,從可用的專案範本列表中選擇"Console App"或"Console App (.NET Core)"範本。 為您的專案命名,請填寫"名稱"欄位。

決定專案存放的位置。 要開始一個新的Console應用專案,點擊"下一步"。 然後選擇適當的.NET Framework,並點擊"建立"。

在新建立的C#專案中,新增對Microsoft PowerPoint互操作程式庫的引用。 這個程式庫使得開發和與簡報互動變得更容易,它可實現您的C#程式碼與PowerPoint應用的通訊。
使用C#從範本建立PowerPoint
載入構成我們新簡報基礎的PowerPoint範本是第一步。範本中通常可見預定義的幻燈片佈局、主題和內容佔位符。 Microsoft.Office.PowerPoint.Interop程式庫的功能允許我們從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
載入範本後,我們使用其佈局構建一個新簡報。 我們使用PowerPoint簡報文件(output.pptx),這有助於我們建立一個PowerPoint簡報物件,成為我們素材的畫布。 範本的佈局和主題將被保留在這個簡報中,使其具有統一的視覺風格。

presentation.Slides.InsertFromFile()方法需要四個參數:FileName、Index、SlideStart和SlideEnd。
- 第一個參數(demo.pptx)指定您希望在簡報中包含的幻燈片的PowerPoint文件路徑。
- 下一個參數是您希望插入幻燈片在目標簡報中的位置。
- 另外兩個參數是可選的。 這些參數允許我們指定開始和結束幻燈片索引。 如果我們需要在特定範圍內提取幻燈片,可以使用這些參數。
您可以使用presentation.Save()方法在使用InsertFromFile()方法插入幻燈片後保存簡報。 在保存後,使用Close()方法關閉PowerPoint程式,然後使用Quit()方法退出簡報。 這樣,您可以通過從外部文件動態新增幻燈片來提高PowerPoint簡報的內容和靈活性。
輸出

IronXL
介紹一個名為IronXL的功能豐富的C#程式庫,用於處理Excel文件。 Iron Software的IronXL提供了廣泛的工具,用於動態建立、填充和格式化Excel文件。 憑藉其直觀的API和豐富的文件,IronXL簡化了C#中的Excel交互,為開發者提供了無縫的Excel相關任務體驗。
IronXL的特點
- 廣泛的Excel支持:IronXL能夠以多種Excel格式(如CSV、XLS和XLSX文件格式)打開和操作大量Excel文件。 開發者可以利用IronXL強大的解析功能,有效地從現有的Excel文件中提取資料,包括舊版和現代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
在這個程式碼範例中,我們首先使用IronXL的Create()函式建立一個新的WorkBook物件,指定所需的Excel文件格式。 下一步是在工作簿中建立一個新的工作表,並在二維陣列中定義一些樣本資料。 接下來,為了使用樣本資料填充試算表,我們使用巢狀迴圈來存取和設置不同單元格的值。 為了釋放系統資源,我們在使用SaveAs()方法將其保存到名為"Demo.xlsx"的文件後關閉工作簿。 同樣,我們可以為目標文件生成多個工作表。
作為輸出建立的Excel文件如下所示。 要了解更多關於寫入Excel文件的資訊,請參考程式碼範例頁面。

結論
總之,利用C#從範本建立PowerPoint簡報可以讓使用者提高設計一致性、加快工作流程並建立有影響力的內容。 開發者可以在保證專業效果的同時節省時間和精力,通過自動化建立過程並利用Microsoft PowerPoint互操作程式庫的功能。 學習這種方法將幫助您作為一名演講者、商業專業人士或教育者提高演講技巧,並用引人注目的幻燈片吸引您的觀眾。
對於C#開發者來說,IronXL是Microsoft Excel的強大替代品,提供全面的Excel支持、卓越性能以及與.NET框架的無縫互動。 IronXL的使用者友好API和對Excel文件的細粒度控制簡化了C#中的Excel寫入。 這使開發者能夠輕鬆自動化Excel相關任務、導出資料和生成動態報告。 無論他們是在為桌面、Web還是移動應用建立Excel文件,C#開發者都可以依靠IronXL簡化Excel相關任務並在其C#程式中發揮Excel的全部潛能。
IronXL首次啟動時的費用為$999。 此外,使用者可以選擇支付一年期的會員費來獲得產品支持和更新。 IronXL提供針對不受限制的再分發的保護,但需收費。 要了解更多關於大致成本的資訊,請存取此授權頁面。 點擊此網站連結查看更多Iron Software的資訊。




