跳過到頁腳內容
產品比較

使用 IronPPT 的 C# Microsoft Office Interop Application C# 替代方案

在建立與 PowerPoint 簡報檔案搭配使用的 .NET 應用程式時,開發人員通常會採用以下兩種方法之一:傳統的[Microsoft Office Interop PowerPoint](https://learn.microsoft.com/en-us/previous-versions/office/office-12/ff761925(v=office.12) ,或像IronPPT這樣的現代 .NET 程式庫。

雖然兩種方案都能操控PowerPoint幻燈片,但它們在易用性、效能和可擴充性方面卻存在巨大差異。如果您曾經為在伺服器上安裝 Microsoft Office 而苦惱,或者在部署過程中遇到過晦澀難懂的 COM 錯誤,您就會明白 IronPPT 的優勢所在。

在本指南中,我們將詳細比較這兩種方法,展示實際用例,並示範IronPPT 如何為您提供 Interop 的所有功能,而無需任何痛苦。

什麼是 Microsoft Office Interop PowerPoint?

! C# Microsoft Office Interop 應用程式 C# 替代方案(使用 IronPPT):圖 1 - Microsoft Office Interop PowerPoint NuGet 頁面

Microsoft Office Interop PowerPoint 是 Microsoft Office Interop 套件的一部分,該套件是一組基於 COM 的 API,可讓 C# 應用程式與 PowerPoint、Word 和 Excel 等 Office 應用程式進行互動。 它的工作原理是在背景啟動一個不可見的 PowerPoint 實例,並透過程式碼對其進行操作。

雖然互通功能齊全,但它有嚴重的限制:

微軟互通 PowerPoint 的主要局限性

*需要安裝 Microsoft Office* :Interop 需要在主機上安裝 PowerPoint。這對於 Web 應用、雲端系統或 Docker 容器來說是一個主要障礙。 僅限 Windows 系統:它只能在 Windows 系統上運作。 不支援Linux或macOS。 伺服器端相容性差:在背景服務、CI/CD 管道或 Web 伺服器中執行 Interop 不可靠,並且經常導致 HRESULT: 0x800706B5 之類的錯誤。 非線程安全:COM 物件本質上不是線程安全的,這使得並發變得棘手。 部署困難:由於 Office 安裝是執行時間依賴項,因此發布獨立應用程式變得困難。 更難的錯誤處理**:COM InterOp 拋出的錯誤通常很模糊,難以調試。

以下範例說明了互通性可能變得多麼笨拙:

using PowerPoint = Microsoft.Office.Interop.PowerPoint;
var app = new PowerPoint.Application();
// Create a new presentation
var presentation = app.Presentations.Add(MsoTriState.msoTrue);
// Add a slide to the presentation
var slide = presentation.Slides.Add(1, PowerPoint.PpSlideLayout.ppLayoutText);
// Add some text to the slide
slide.Shapes[1].TextFrame.TextRange.Text = "Hello from Interop!";
// Save the presentation to a file
presentation.SaveAs(@"C:\TestInterop.pptx");
// Close the presentation and quit the application
presentation.Close();
app.Quit();
using PowerPoint = Microsoft.Office.Interop.PowerPoint;
var app = new PowerPoint.Application();
// Create a new presentation
var presentation = app.Presentations.Add(MsoTriState.msoTrue);
// Add a slide to the presentation
var slide = presentation.Slides.Add(1, PowerPoint.PpSlideLayout.ppLayoutText);
// Add some text to the slide
slide.Shapes[1].TextFrame.TextRange.Text = "Hello from Interop!";
// Save the presentation to a file
presentation.SaveAs(@"C:\TestInterop.pptx");
// Close the presentation and quit the application
presentation.Close();
app.Quit();
Imports PowerPoint = Microsoft.Office.Interop.PowerPoint
Private app = New PowerPoint.Application()
' Create a new presentation
Private presentation = app.Presentations.Add(MsoTriState.msoTrue)
' Add a slide to the presentation
Private slide = presentation.Slides.Add(1, PowerPoint.PpSlideLayout.ppLayoutText)
' Add some text to the slide
Private slide.Shapes(1).TextFrame.TextRange.Text = "Hello from Interop!"
' Save the presentation to a file
presentation.SaveAs("C:\TestInterop.pptx")
' Close the presentation and quit the application
presentation.Close()
app.Quit()
$vbLabelText   $csharpLabel

理論上看起來沒問題。但在實際生產環境中,你必須確保安裝了 PowerPoint,處理 Office 許可問題,手動管理資源,並且祈禱在無頭環境下一切正常。

隆重介紹 IronPPT:一款現代、強大的替代方案

IronPPT是一個功能強大的 .NET 程式庫,無需 Microsoft Office 即可建立、讀取、編輯和轉換 PowerPoint 文件。無論您是想創建引人注目的報告,還是想透過編寫程式碼實現簡報的自動化創建,亦或是僅需要一個無需安裝 Microsoft PowerPoint 即可創建 PowerPoint 簡報的工具,IronPPT 都能滿足您的需求。

它是專為需要以下功能的開發者而設計的:

  • 簡潔明了的語法
  • 支援現代 .NET Framework / .NET Core / .NET 6/7+ 平台 輕量級、快速的 PowerPoint 處理

是的-你完全不需要安裝 Office 或 PowerPoint。 IronPPT 是 100% 獨立運作的。

安裝

您可以透過 NuGet 套件管理器控制台執行以下命令,將 IronPPT 新增至您的 C# 專案:

Install-Package IronPPT

在今天的示範中,我們將在 Visual Studio 中建立一個新專案。 如果你想跟著做,不妨自己也做一個! 只需在 Visual Studio 中建立新項目,然後選擇控制台應用程式即可開始。

IronPPT的優勢

! C# Microsoft Office 互通應用程式 C# 替代方案(使用 IronPPT):圖 2 - IronPPT

無需依賴辦公室

使用 IronPPT,您的應用程式將真正獨立。 您可以將其部署到任何環境——Azure、AWS Lambda、Docker 容器或 Linux 伺服器——而無需安裝或取得 Microsoft Office 的許可。

用於建立簡報文件的簡潔 API

使用 IronPPT,只需幾行程式碼即可建立新的簡報文件,並輕鬆地在空白簡報中新增文字。 使用 IronPPT 建立新檔案時,您將從一張投影片開始,可以根據需要進行編輯。 想增加更多投影片嗎? 只需幾行程式碼,即可使用簡單的 AddSlide 方法,在簡報中新增所需數量的幻燈片。

using IronPPT;
using IronPPT.Models;

// Create a new empty presentation
var document = new PresentationDocument();

// Add text to the first slide
document.Slides[0].TextBoxes[0].AddText("Hello, World!");
document.Slides[0].TextBoxes[1].AddText("Welcome to IronPPT!");

// Save the presentation to a file
document.Save("presentation.pptx");
using IronPPT;
using IronPPT.Models;

// Create a new empty presentation
var document = new PresentationDocument();

// Add text to the first slide
document.Slides[0].TextBoxes[0].AddText("Hello, World!");
document.Slides[0].TextBoxes[1].AddText("Welcome to IronPPT!");

// Save the presentation to a file
document.Save("presentation.pptx");
Imports IronPPT
Imports IronPPT.Models

' Create a new empty presentation
Private document = New PresentationDocument()

' Add text to the first slide
document.Slides(0).TextBoxes(0).AddText("Hello, World!")
document.Slides(0).TextBoxes(1).AddText("Welcome to IronPPT!")

' Save the presentation to a file
document.Save("presentation.pptx")
$vbLabelText   $csharpLabel

Output

! C# Microsoft Office 互通應用程式 C# 替代方案(使用 IronPPT):圖 3 - 帶有自訂文字的新簡報

相比之下,Interop 方法冗長且容易出錯。 IronPPT 介面簡潔、易讀,可直接用於生產環境。

新增自訂形狀和圖像

想為您的簡報創建吸引眼球的元素嗎? IronPPT 支援在投影片中新增自訂形狀影像,讓您可以完全控制簡報的最終外觀。

using IronPPT;
using IronPPT.Models;
using IronPPT.Enums;
using IronPPT.Models.Styles;

// Load an existing presentation
var document = new PresentationDocument("presentation.pptx");
Slide slide = new Slide();

// Add a rectangle shape
Shape shape = new Shape
{
    Type = ShapeType.Rectangle,
    FillColor = Color.LightBlue,
    OutlineColor = Color.Black,
    Width = 200,
    Height = 100,
    Position = (200, 50)
};
slide.AddShape(shape);

// Add an Image
Image image = new Image();
image.LoadFromFile("IronPPT.png");
var img = slide.AddImage(image);
img.Position = (100, 200);
img.Width = 400;
img.Height = 200;

// Add the slide to the document and save
document.AddSlide(slide);
document.Save("presentation.pptx");
using IronPPT;
using IronPPT.Models;
using IronPPT.Enums;
using IronPPT.Models.Styles;

// Load an existing presentation
var document = new PresentationDocument("presentation.pptx");
Slide slide = new Slide();

// Add a rectangle shape
Shape shape = new Shape
{
    Type = ShapeType.Rectangle,
    FillColor = Color.LightBlue,
    OutlineColor = Color.Black,
    Width = 200,
    Height = 100,
    Position = (200, 50)
};
slide.AddShape(shape);

// Add an Image
Image image = new Image();
image.LoadFromFile("IronPPT.png");
var img = slide.AddImage(image);
img.Position = (100, 200);
img.Width = 400;
img.Height = 200;

// Add the slide to the document and save
document.AddSlide(slide);
document.Save("presentation.pptx");
Imports IronPPT
Imports IronPPT.Models
Imports IronPPT.Enums
Imports IronPPT.Models.Styles

' Load an existing presentation
Private document = New PresentationDocument("presentation.pptx")
Private slide As New Slide()

' Add a rectangle shape
Private shape As New Shape With {
	.Type = ShapeType.Rectangle,
	.FillColor = Color.LightBlue,
	.OutlineColor = Color.Black,
	.Width = 200,
	.Height = 100,
	.Position = (200, 50)
}
slide.AddShape(shape)

' Add an Image
Dim image As New Image()
image.LoadFromFile("IronPPT.png")
Dim img = slide.AddImage(image)
img.Position = (100, 200)
img.Width = 400
img.Height = 200

' Add the slide to the document and save
document.AddSlide(slide)
document.Save("presentation.pptx")
$vbLabelText   $csharpLabel

Output

! C# Microsoft Office 互通應用程式 C# 替代方案(使用 IronPPT):圖 4 - 為簡報新增了自訂形狀和影像

新增樣式段落

您希望文本既能提供有關演示主題的資訊和細節,又能起到視覺吸引力的作用嗎? 使用 IronPPT 建立並向簡報檔案中添加樣式段落,真正讓觀眾對您的投影片簡報保持興趣。

using IronPPT;
using IronPPT.Models;
using IronPPT.Enums;
using IronPPT.Models.Styles;

// Create a new presentation
var document = new PresentationDocument();
Slide slide = new Slide();

// Define the paragraph style
var style = new ParagraphStyle()
{
    NoBullet = true,
    RightToLeft = true,
    Indent = 10.00,
    Alignment = TextAlignmentTypeValues.Center,
};

// Create a paragraph with the style
var paragraph = new Paragraph();
paragraph.Style = style;
paragraph.AddText("This is a sample paragraph with custom styles applied.");

// Add the paragraph to the slide
document.AddSlide(slide);
slide.AddParagraph(paragraph);

// Save the presentation to a file
document.Save("presentation.pptx");
using IronPPT;
using IronPPT.Models;
using IronPPT.Enums;
using IronPPT.Models.Styles;

// Create a new presentation
var document = new PresentationDocument();
Slide slide = new Slide();

// Define the paragraph style
var style = new ParagraphStyle()
{
    NoBullet = true,
    RightToLeft = true,
    Indent = 10.00,
    Alignment = TextAlignmentTypeValues.Center,
};

// Create a paragraph with the style
var paragraph = new Paragraph();
paragraph.Style = style;
paragraph.AddText("This is a sample paragraph with custom styles applied.");

// Add the paragraph to the slide
document.AddSlide(slide);
slide.AddParagraph(paragraph);

// Save the presentation to a file
document.Save("presentation.pptx");
Imports IronPPT
Imports IronPPT.Models
Imports IronPPT.Enums
Imports IronPPT.Models.Styles

' Create a new presentation
Private document = New PresentationDocument()
Private slide As New Slide()

' Define the paragraph style
Private style = New ParagraphStyle() With {
	.NoBullet = True,
	.RightToLeft = True,
	.Indent = 10.00,
	.Alignment = TextAlignmentTypeValues.Center
}

' Create a paragraph with the style
Private paragraph = New Paragraph()
paragraph.Style = style
paragraph.AddText("This is a sample paragraph with custom styles applied.")

' Add the paragraph to the slide
document.AddSlide(slide)
slide.AddParagraph(paragraph)

' Save the presentation to a file
document.Save("presentation.pptx")
$vbLabelText   $csharpLabel

Output

! C# Microsoft Office 互通應用程式 C# 替代方案(使用 IronPPT):圖 5 - 樣式化段落輸出

微軟 PowerPoint 互通性的缺點

1. 需要安裝 PowerPoint

如果未安裝 Microsoft PowerPoint,您的應用程式將會崩潰:

using Microsoft.Office.Interop.PowerPoint;

// Attempt to open an existing PowerPoint file
var app = new Application();
var presentation = app.Presentations.Open(@"C:\Slides\Deck.pptx");
using Microsoft.Office.Interop.PowerPoint;

// Attempt to open an existing PowerPoint file
var app = new Application();
var presentation = app.Presentations.Open(@"C:\Slides\Deck.pptx");
Imports Microsoft.Office.Interop.PowerPoint

' Attempt to open an existing PowerPoint file
Private app = New Application()
Private presentation = app.Presentations.Open("C:\Slides\Deck.pptx")
$vbLabelText   $csharpLabel

Problem:

如果電腦上未安裝 PowerPoint(例如雲端伺服器或 Docker 容器),則會拋出 COMException 異常,通常如下:

檢索 CLSID 為 80040154 的組件的 COM 類別工廠失敗,錯誤訊息如下:類別未註冊。

2. STA線程要求

Interop 必須在單執行緒單元 (STA) 執行緒上執行,否則會當機:

// This will crash if called from a background thread in a web app or service
var app = new Application();
// This will crash if called from a background thread in a web app or service
var app = new Application();
' This will crash if called from a background thread in a web app or service
Dim app = New Application()
$vbLabelText   $csharpLabel

解決方法:您必須手動將呼叫包裝在 STA 執行緒中:

Thread thread = new Thread(() =>
{
    // Create a new PowerPoint application
    var app = new Application();

    // Add a presentation and slide
    var pres = app.Presentations.Add();
    pres.Slides.Add(1, PpSlideLayout.ppLayoutText);

    // Save and close the presentation
    pres.SaveAs(@"C:\output.pptx");
    app.Quit();
});

// Set thread apartment state and start
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
thread.Join();
Thread thread = new Thread(() =>
{
    // Create a new PowerPoint application
    var app = new Application();

    // Add a presentation and slide
    var pres = app.Presentations.Add();
    pres.Slides.Add(1, PpSlideLayout.ppLayoutText);

    // Save and close the presentation
    pres.SaveAs(@"C:\output.pptx");
    app.Quit();
});

// Set thread apartment state and start
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
thread.Join();
Dim thread As New Thread(Sub()
	' Create a new PowerPoint application
	Dim app = New Application()

	' Add a presentation and slide
	Dim pres = app.Presentations.Add()
	pres.Slides.Add(1, PpSlideLayout.ppLayoutText)

	' Save and close the presentation
	pres.SaveAs("C:\output.pptx")
	app.Quit()
End Sub)

' Set thread apartment state and start
thread.SetApartmentState(ApartmentState.STA)
thread.Start()
thread.Join()
$vbLabelText   $csharpLabel

這很麻煩也很脆弱——尤其是在 ASP.NET 或後台服務中。

3. 非託管 COM 物件和記憶體洩漏

未能釋放 COM 物件會導致記憶體洩漏和應用程式崩潰:

var app = new Application();
var presentation = app.Presentations.Open(@"C:\Slides\Deck.pptx");
// Forgetting to release COM objects correctly can lead to memory leaks
presentation.Close();
app.Quit();
// Forgot to release COM objects!
var app = new Application();
var presentation = app.Presentations.Open(@"C:\Slides\Deck.pptx");
// Forgetting to release COM objects correctly can lead to memory leaks
presentation.Close();
app.Quit();
// Forgot to release COM objects!
Dim app = New Application()
Dim presentation = app.Presentations.Open("C:\Slides\Deck.pptx")
' Forgetting to release COM objects correctly can lead to memory leaks
presentation.Close()
app.Quit()
' Forgot to release COM objects!
$vbLabelText   $csharpLabel

4. 複雜冗長的句法

添加一個簡單的文字幻燈片需要編寫大量的樣板程式碼:

var app = new Application();
var presentation = app.Presentations.Add(MsoTriState.msoTrue);
var slide = presentation.Slides.Add(1, PpSlideLayout.ppLayoutText);
slide.Shapes[1].TextFrame.TextRange.Text = "Hello from Interop!";
presentation.SaveAs(@"C:\test.pptx");
presentation.Close();
app.Quit();
var app = new Application();
var presentation = app.Presentations.Add(MsoTriState.msoTrue);
var slide = presentation.Slides.Add(1, PpSlideLayout.ppLayoutText);
slide.Shapes[1].TextFrame.TextRange.Text = "Hello from Interop!";
presentation.SaveAs(@"C:\test.pptx");
presentation.Close();
app.Quit();
Dim app = New Application()
Dim presentation = app.Presentations.Add(MsoTriState.msoTrue)
Dim slide = presentation.Slides.Add(1, PpSlideLayout.ppLayoutText)
slide.Shapes(1).TextFrame.TextRange.Text = "Hello from Interop!"
presentation.SaveAs("C:\test.pptx")
presentation.Close()
app.Quit()
$vbLabelText   $csharpLabel

相比之下,IronPPT 的語法簡潔明了,易於管理:

using IronPPT;
using IronPPT.Models;

// Create and save a simple presentation with IronPPT
var document = new PresentationDocument();
document.Save("presentation.pptx");
using IronPPT;
using IronPPT.Models;

// Create and save a simple presentation with IronPPT
var document = new PresentationDocument();
document.Save("presentation.pptx");
Imports IronPPT
Imports IronPPT.Models

' Create and save a simple presentation with IronPPT
Private document = New PresentationDocument()
document.Save("presentation.pptx")
$vbLabelText   $csharpLabel

結論:現代 .NET 專案的最佳選擇

當需要在 C# 應用程式中建立 PowerPoint 自動化功能時,[Microsoft Office Interop PowerPoint](https://learn.microsoft.com/en-us/previous-versions/office/office-12/ff761925(v=office.12)和[**IronPPT**](https://ironsoftware.com/csharp/ppt/)之間的選擇再清楚不過了

在本文中,我們探討了這兩個函式庫之間的根本差異:

  • Interop功能強大但又很僵化——雖然它可以處理諸如創建演示文稿和將演示文稿轉換為其他格式之類的任務,但它要求安裝 PowerPoint,強制執行 STA 線程限制,如果不小心會導致內存洩漏,而且根本不適合現代的雲端原生 .NET 工作流程。
  • IronPPT則是專為當今的開發環境而設計的。 它輕巧便捷,無需安裝 Office,可在 Web 伺服器和 CI/CD 管道中無縫運行,並提供簡潔、現代且易於使用和維護的 API。

我們還查看了現實世界的程式碼範例,這些範例突出了 Interop 的常見陷阱——從線程異常和 COM 錯誤到部署難題——並將它們與 IronPPT 流暢、直觀的語法進行了比較。

如果您真心想要簡化應用程式中 PowerPoint 投影片的建立、編輯和匯出,而無需 Interop 的遺留問題,那麼 IronPPT 無疑是最佳選擇

想親眼看看差異嗎? 下載免費的 IronPPT 試用版,只需幾行 C# 程式碼即可開始建立專業品質的 PowerPoint 檔案——無需安裝 Office。

別再跟 COM 物件較勁了。 使用 IronPPT,開始交付現代化、快速、可靠的 .NET 解決方案。

常見問題解答

在 .NET 中使用 Microsoft Office Interop for PowerPoint 常見的缺點有哪些?

Microsoft Office Interop 需要安裝 Microsoft Office,僅支援 Windows,伺服器端相容性差,缺乏線程安全,並涉及複雜的錯誤處理。IronPPT 透過提供獨立、跨平台的解決方案與簡化的 API 來解決這些問題。

IronPPT 如何增強 .NET 應用程式中的 PowerPoint 自動化?

IronPPT for .NET 提供現代化的 .NET 函式庫,可讓開發人員在不需要 Microsoft Office 的情況下建立、讀取、編輯和轉換 PowerPoint 檔案,藉此增強自動化功能。它支援各種平台,並提供簡潔的語法,非常適合雲端系統。

使用 .NET PowerPoint 函式庫的安裝需求為何?

IronPPT 可透過 NuGet 套件管理員控制台,使用 Install-Package IronPPT 指令安裝在 C# 專案中,無須安裝 Microsoft Office。

IronPPT 可以部署在雲端環境嗎?

是的,IronPPT 可以無縫部署在雲環境中,包括 AWS Lambda、Azure、Docker 容器和 Linux 伺服器,所有這些都不需要安裝 Office。

為什麼 IronPPT 被認為是比 Interop 更好的 PowerPoint 自動化替代方案?

IronPPT 因其輕量級設計、不需安裝 Office、支援多種平台,以及易於使用的現代化 API(可簡化 .NET 專案中的 PowerPoint 自動化)而成為優先選擇。

IronPPT 如何簡化以 C# 製作 PowerPoint 簡報的過程?

IronPPT 簡化了這個過程,讓開發人員可以使用直接的 API,輕鬆地在簡報中加入文字、自訂形狀、圖片和樣式段落,避免 Interop 的複雜性。

IronPPT 是否需要在系統上安裝 Microsoft Office 或 PowerPoint?

不,IronPPT 是一個獨立的函式庫,不需要安裝 Microsoft Office 或 PowerPoint,因此在伺服器端和雲端應用程式上有很高的通用性。

IronPPT 為什麼適合現代 .NET 工作流程?

IronPPT 適用於現代的 .NET 工作流程,因為它具有輕量、獨立、跨平台支援的特性,並且能夠在伺服器和雲端環境中有效運作,而不需要 Interop 的依賴性和冗長的文字。

Jordi Bardia
軟體工程師
Jordi 在 Python、C# 和 C++ 上最得心應手,當他不在 Iron Software 展現技術時,便在做遊戲編程。在分担产品测测试,产品开发和研究的责任时,Jordi 为持续的产品改进增值。他说这种多样化的经验使他受到挑战并保持参与, 而这也是他与 Iron Software 中工作一大乐趣。Jordi 在佛罗里达州迈阿密长大,曾在佛罗里达大学学习计算机科学和统计学。