IronWord 開始使用 在 Azure 上部署 如何在 Azure 上使用 .NET 執行 IronWord Kye Stuart 更新:8月 20, 2025 下載 IronWord NuGet 下載 開始免費試用 法學碩士副本 法學碩士副本 將頁面複製為 Markdown 格式,用於 LLMs 在 ChatGPT 中打開 請向 ChatGPT 諮詢此頁面 在雙子座打開 請向 Gemini 詢問此頁面 在雙子座打開 請向 Gemini 詢問此頁面 打開困惑 向 Perplexity 詢問有關此頁面的信息 分享 在 Facebook 上分享 分享到 X(Twitter) 在 LinkedIn 上分享 複製連結 電子郵件文章 This article was translated from English: Does it need improvement? Translated View the article in English IronWord 是一個功能強大的 .NET 程式庫,用於以程式設計方式建立、編輯和讀取 Word 文件。 它可與各種 Azure 服務無縫協作,包括 Azure 應用程式服務、Azure 函數和 Azure 容器執行個體。 安裝 IronWord 首先從官方 NuGet 儲存庫安裝 IronWord NuGet 套件: Install-Package IronWord Azure 的託管注意事項 選擇適當的 Azure 服務層 IronWord 在提供一致運算能力的 Azure 服務計畫上表現最佳。 可用性。 對於大多數中小規模的使用場景,基礎(B1)應用服務計劃是 充足的。 如果您的應用程式處理大量 Word 文件或執行某些操作 對於複雜的格式化任務,請考慮升級至標準版 (S1) 或更高版本。 避免效能瓶頸。 支援的 .NET 執行時期和相容性 IronWord 可與 Azure 託管解決方案中常用的以下框架無縫整合: .NET 6+(建議使用 LTS 版本) .NET Core 3.1 .NET Standard 2.1 這樣,您就可以靈活地在各種 Azure 服務(如應用程式服務、Azure 函數和 Docker 容器)中部署 IronWord,而無需擔心相容性問題。 在 Azure 上使用 Docker 進行部署 使用 IronWord 進行容器化部署 如果您希望最大程度地控制執行時間環境,請考慮將 IronWord 部署在 Azure 容器執行個體 (ACI) 或 Azure Kubernetes 服務 (AKS) 上的 Docker 容器中。 這使您能夠: 預先載入模板或靜態資源 配置文件處理設定 在作業系統層面微調效能 首先,使用基礎映像,例如 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 函數範例:按需產生 Word 文檔 以下是一個 Azure 函數的實際範例,該函數會回應 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 $vbLabelText $csharpLabel 代碼解釋: 我們定義一個名為"GenerateWordDoc"的 Azure 函式。 此函數由 HTTP GET 或 POST 要求觸發,並在開始處理時記錄一則訊息。 我們透過設定 IronWord.License.LicenseKey 來指定 IronWord 的授權金鑰(將"YOUR-LICENSE-KEY"替換為您的實際金鑰)。 使用 IronWord 的 API 建立一個新的 Word 文件。 文件中新增了兩個段落-一個段落包含靜態文本,另一個段落顯示目前的 UTC 時間戳記。 使用 doc.SaveAs(tempPath) 將文件儲存到伺服器上的臨時 .docx 檔案中。 使用 File.ReadAllBytes 將已儲存的檔案讀入位元組數組,以便下載。 臨時檔案讀取後立即刪除,以保持系統清潔。 建立一個 HttpResponseMessage,其中包含文件的位元組內容作為可下載的附件。 Content-Disposition 標頭使用目前日期和時間設定下載檔名。 Content-Type 標頭設定為"application/vnd.openxmlformats-officedocument.wordprocessingml.document",以指示 Word 檔案格式。 常見問題解答 設置 IronWord 在 Azure 上的第一步是什麼? 設置 IronWord 在 Azure 上的第一步是確保您有一個 Azure 帳戶。如果您尚無帳戶,則需要創建一個。接下來,您將需要設置一個新 Azure 應用服務,以便使用 IronWord 部署您的 .NET 應用程序。 如何使用 IronWord 在 Azure 上部署 .NET 應用程序? 要使用 IronWord 在 Azure 上部署 .NET 應用程序,您應該將應用程序及其依賴項打包,包括 IronWord 庫,並將其上傳到您的 Azure 應用服務。您可以使用諸如 Visual Studio Publish 或 Azure DevOps 管道之類的工具完成這一過程。 運行 IronWord 需要任何特定的 Azure 服務嗎? IronWord 可以在標準的 Azure 應用服務上運行。但是,為了獲得最佳性能,建議使用根據應用需求提供足夠資源的計劃。 IronWord 可以與 Azure Functions 一起使用嗎? 可以,IronWord 可以與 Azure Functions 整合,在無伺服器架構中處理 Word 文檔。確保 Azure 函數環境具有 IronWord 所需的依賴項。 IronWord 如何增強 Azure 上的 Word 文件處理? IronWord 通過提供強大的 .NET 庫來增強 Azure 上的 Word 文件處理,這些庫可以輕鬆地與 Azure 服務集成,從而實現高效的創建、操縱和轉換 Word 文件。 有沒有方法可以在 Azure 上自動化 IronWord 任務? 可以,您可以使用 Azure Logic Apps 或 Azure Functions 基於特定事件或計劃觸發 Word 文檔處理來自動化 IronWord 任務。 在 Azure 上使用 IronWord 有什麼好處? 在 Azure 上使用 IronWord 可以進行可擴展且可靠的 Word 文件處理,利用 Azure 的雲基礎設施處理大量文檔,同時保持高性能。 我可以將 IronWord 與 Azure Blob 儲存整合嗎? 可以,您可以將 IronWord 與 Azure Blob 儲存整合,以便儲存和檢索 Word 文檔,實現 Azure 環境內無縫的文檔處理和儲存管理。 如何確保 IronWord 在 Azure 上高效運行? 要確保 IronWord 在 Azure 上高效運行,選擇符合您工作負載的合適服務計劃,優化您的應用程序代碼,並實施適當的錯誤處理和日誌記錄,以進行故障排除。 在 Azure 上使用 IronWord 的前提條件有哪些? 在 Azure 上使用 IronWord 的前提條件包括設置 .NET 環境、擁有必要權限的 Azure 帳戶以及在項目中包含 IronWord 庫。 Kye Stuart 立即與工程團隊聊天 技術作家 Kye Stuart 在 Iron Software 將編碼熱情與寫作技能相結合。接受過 Yoobee 學院的软件部署教育,他現在將複雜的技術概念轉化為清晰的教育內容。Kye 重視終身學習,並接受新技術挑戰。在工作之外,他喜歡 PC 遊戲,並在 Twitch 上進行直播,以及喜好戶外活動如園藝和遛狗 (Jaiya)。Kye 的直截了當風格,使他成為 Iron Software 全球解密技術使命的關鍵人物。 準備好開始了嗎? Nuget 下載 27,129 | Version: 2025.11 剛發表 免費下載 NuGet 下載總數:27,129 檢視授權