如何在 Azure 上使用 .NET 執行 IronWord
IronWord 是一款強大的 .NET 程式庫,用於透過程式化方式建立、編輯及讀取 Word 文件。 它能無縫運作於各種 Azure 服務上,包括 Azure App Services、Azure Functions 以及 Azure Container Instances。
安裝 IronWord
首先,請從官方 NuGet 儲存庫安裝 IronWord NuGet 套件:
Install-Package IronWord
Azure 的託管考量事項
選擇合適的 Azure 服務層級
IronWord 在能提供穩定運算可用性的 Azure 服務方案上表現最佳。 對於大多數中小型應用場景,Basic (B1) App Service 方案已足夠。 若您的應用程式需處理大量 WORD 文件或執行複雜的格式設定任務,請考慮升級至 Standard (S1) 或更高階方案,以避免效能瓶頸。
支援的 .NET 執行環境與相容性
IronWord 可立即與以下常見於 Azure 託管解決方案的框架配合使用:
- .NET 6+(建議使用 LTS 版本)
- .NET Core 3.1
- .NET Standard 2.1
這讓您能夠靈活地在各種 Azure 服務(如 App Services、Azure Functions 和 Docker 容器)中部署 IronWord,無需擔心相容性問題。
在 Azure 上透過 Docker 部署
使用 IronWord 進行容器化部署
若您希望對執行環境擁有最大程度的控制權,建議考慮在 Azure Container Instances (ACI) 或 Azure Kubernetes Service (AKS) 上的 Docker 容器中部署 IronWord。 這讓您能夠:
- 預載範本或靜態資源
- 設定文件處理選項
- 在作業系統層級微調效能
開始使用時,請選用 mcr.microsoft.com/dotnet/aspnet:6.0 或 7.0 等基礎映像,並透過 NuGet 或手動加入 DLL 來安裝 IronWord。
使用 Azure Functions 實現無伺服器架構
在 Azure Functions 中使用 IronWord
IronWord 完全相容於在 .NET 6 或更高版本上執行的 Azure Functions v4。 這能實現輕量級、事件驅動的文件生成——非常適合以下情境:
- 透過 HTTP 建立按需報告
- 根據表單提交內容產生 WORD 文件
- 將結構化資料轉換為 .docx 格式
Azure Function 範例:依要求產生 WORD 文件
以下是一個實際案例,展示如何透過 Azure Function 響應 HTTP 請求來建立並回傳 WORD 文件:
using System.Net;
using System.Net.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.Extensions.Logging;
using System.Net.Http.Headers;
using IronWord;
using IronWord.Models;
using System.IO;
using System.Threading.Tasks;
public static class WordFunction
{
[FunctionName("GenerateWordDoc")]
public static HttpResponseMessage Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
ILogger log)
{
log.LogInformation("Processing request to generate Word document...");
// Set your IronWord license key
IronWord.License.LicenseKey = "YOUR-LICENSE-KEY";
// Create and populate Word document
var doc = new WordDocument();
Paragraph para1 = new Paragraph(new TextContent("This Word document was generated by IronWord in an Azure Function."));
Paragraph para2 = new Paragraph(new TextContent($"Timestamp: {System.DateTime.UtcNow}"));
doc.AddParagraph(para1);
doc.AddParagraph(para2);
// Save to temporary file
string tempPath = Path.GetTempFileName().Replace(".tmp", ".docx");
doc.SaveAs(tempPath);
// Read the file bytes
byte[] fileBytes = File.ReadAllBytes(tempPath);
// Optionally delete the temp file
File.Delete(tempPath);
// Build the response with the document as an attachment
var response = new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new ByteArrayContent(fileBytes)
};
response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
{
FileName = $"IronWord_{System.DateTime.UtcNow:yyyyMMdd_HHmmss}.docx"
};
response.Content.Headers.ContentType = new MediaTypeHeaderValue(
"application/vnd.openxmlformats-officedocument.wordprocessingml.document");
return response;
}
}
using System.Net;
using System.Net.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.Extensions.Logging;
using System.Net.Http.Headers;
using IronWord;
using IronWord.Models;
using System.IO;
using System.Threading.Tasks;
public static class WordFunction
{
[FunctionName("GenerateWordDoc")]
public static HttpResponseMessage Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
ILogger log)
{
log.LogInformation("Processing request to generate Word document...");
// Set your IronWord license key
IronWord.License.LicenseKey = "YOUR-LICENSE-KEY";
// Create and populate Word document
var doc = new WordDocument();
Paragraph para1 = new Paragraph(new TextContent("This Word document was generated by IronWord in an Azure Function."));
Paragraph para2 = new Paragraph(new TextContent($"Timestamp: {System.DateTime.UtcNow}"));
doc.AddParagraph(para1);
doc.AddParagraph(para2);
// Save to temporary file
string tempPath = Path.GetTempFileName().Replace(".tmp", ".docx");
doc.SaveAs(tempPath);
// Read the file bytes
byte[] fileBytes = File.ReadAllBytes(tempPath);
// Optionally delete the temp file
File.Delete(tempPath);
// Build the response with the document as an attachment
var response = new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new ByteArrayContent(fileBytes)
};
response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
{
FileName = $"IronWord_{System.DateTime.UtcNow:yyyyMMdd_HHmmss}.docx"
};
response.Content.Headers.ContentType = new MediaTypeHeaderValue(
"application/vnd.openxmlformats-officedocument.wordprocessingml.document");
return response;
}
}
Imports System
Imports System.Net
Imports System.Net.Http
Imports Microsoft.AspNetCore.Http
Imports Microsoft.Azure.WebJobs
Imports Microsoft.Azure.WebJobs.Extensions.Http
Imports Microsoft.Extensions.Logging
Imports System.Net.Http.Headers
Imports IronWord
Imports IronWord.Models
Imports System.IO
Imports System.Threading.Tasks
Public Module WordFunction
<FunctionName("GenerateWordDoc")>
Public Function Run(<HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route := Nothing)> ByVal req As HttpRequest, ByVal log As ILogger) As HttpResponseMessage
log.LogInformation("Processing request to generate Word document...")
' Set your IronWord license key
IronWord.License.LicenseKey = "YOUR-LICENSE-KEY"
' Create and populate Word document
Dim doc = New WordDocument()
Dim para1 As New Paragraph(New TextContent("This Word document was generated by IronWord in an Azure Function."))
Dim para2 As New Paragraph(New TextContent($"Timestamp: {DateTime.UtcNow}"))
doc.AddParagraph(para1)
doc.AddParagraph(para2)
' Save to temporary file
Dim tempPath As String = Path.GetTempFileName().Replace(".tmp", ".docx")
doc.SaveAs(tempPath)
' Read the file bytes
Dim fileBytes() As Byte = File.ReadAllBytes(tempPath)
' Optionally delete the temp file
File.Delete(tempPath)
' Build the response with the document as an attachment
Dim response = New HttpResponseMessage(HttpStatusCode.OK) With {.Content = New ByteArrayContent(fileBytes)}
response.Content.Headers.ContentDisposition = New ContentDispositionHeaderValue("attachment") With {.FileName = $"IronWord_{DateTime.UtcNow:yyyyMMdd_HHmmss}.docx"}
response.Content.Headers.ContentType = New MediaTypeHeaderValue("application/vnd.openxmlformats-officedocument.wordprocessingml.document")
Return response
End Function
End Module
程式碼說明:
- 我們定義一個名為"GenerateWordDoc"的 Azure Function。
- 此函式由 HTTP GET 或 POST 請求觸發,並在開始處理時記錄一則訊息。
- 我們透過設定 IronWord.License.LicenseKey 來指定 IronWord 的授權金鑰(請將 "YOUR-LICENSE-KEY" 替換為您的實際金鑰)。
- 使用 IronWord 的 API 建立一個新的 WordDocument。
- 文件中新增了兩段文字——一段為靜態文字,另一段則顯示當前的 UTC 時間戳記。
- 文件會透過 doc.SaveAs(tempPath) 指令,儲存至伺服器上的臨時 .docx 檔案中。
- 透過 File.ReadAllBytes 將儲存的檔案讀取至位元組陣列中,以準備進行下載。
- 讀取完畢後,系統會立即刪除暫存檔,以保持系統整潔。
- 建立一個 HttpResponseMessage,其中包含文件作為可下載附件的位元組內容。
- Content-Disposition 標頭會根據當前日期和時間設定下載檔案名稱。
- Content-Type 標頭設定為 "application/vnd.openxmlformats-officedocument.wordprocessingml.document",以標示為 WORD 文件格式。
常見問題
在 Azure 上設定 IronWord 的第一步是什麼?
在 Azure 上設定 IronWord 的第一步,是建立一個 Azure 帳戶(若您尚未擁有)。接著,您需要建立一個新的 Azure App Service,並透過 IronWord 将您的 .NET 應用程式部署至該服務中。
如何在 Azure 上使用 IronWord 部署 .NET 應用程式?
若要在 Azure 上使用 IronWord 部署 .NET 應用程式,您應將應用程式及其依賴項(包括 IronWord程式庫)打包,並上傳至您的 Azure App Service。您可以使用 Visual Studio Publish 或 Azure DevOps 管線等工具來執行此流程。
我需要任何特定的 Azure 服務才能執行 IronWord 嗎?
IronWord 可在標準的 Azure App Services 上運行。然而,為獲得最佳效能,建議根據您的應用程式需求,選用能提供充足資源的方案。
IronWord 能否與 Azure Functions 搭配使用?
是的,IronWord 可與 Azure Functions 整合,作為無伺服器架構的一部分來處理 Word 文件。請確保 Azure Function 環境具備 IronWord 所需的必要依賴項。
IronWord 如何強化 Azure 上的 WORD 文件處理功能?
IronWord 透過提供可輕鬆與 Azure 服務整合的強大 .NET 程式庫,強化 Azure 上的 WORD 文件處理能力,從而實現 WORD 文件的高效建立、編輯與轉換。
是否有方法能在 Azure 上自動化執行 IronWord 的任務?
是的,您可以透過 Azure Logic Apps 或 Azure Functions 在 Azure 上自動化 IronWord 的任務,根據特定事件或排程觸發 Word 文件處理。
在 Azure 上使用 IronWord 有什麼好處?
在 Azure 上使用 IronWord 可實現可擴展且可靠的 WORD 文件處理,透過 Azure 的雲端基礎架構處理大量文件,同時維持高效能。
我可以將 IronWord 與 Azure Blob Storage 整合嗎?
是的,您可以將 IronWord 與 Azure Blob Storage 整合,用以儲存和檢索 WORD 文件,從而實現 Azure 環境內無縫的文件處理與儲存管理。
如何確保 IronWord 在 Azure 上高效運行?
為確保 IronWord 能在 Azure 上高效運行,請選擇與您的工作負載相符的服務方案,優化應用程式程式碼,並實作適當的錯誤處理與記錄機制以利疑難排解。
在 Azure 上使用 IronWord 是否有任何先決條件?
在 Azure 上使用 IronWord 的先決條件包括:已設定好 .NET 環境、擁有具備必要權限的 Azure 帳戶,以及將 IronWord程式庫納入您的專案中。

