IronWord 开始 在Azure上部署 如何在 Azure 上使用 .NET 运行 IronWord Kye Stuart 已更新:八月 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上的第一步是什么? 在Azure上设置IronWord的第一步是创建Azure帐户(如果您还没有的话)。然后,您需要设置一个新的Azure应用服务,您将使用IronWord在此部署您的.NET应用程序。 如何在Azure上使用IronWord部署.NET应用程序? 要在Azure上使用IronWord部署.NET应用程序,您应该将应用程序及其依赖项(包括IronWord库)打包,并将它们上传到您的Azure应用服务。您可以使用Visual Studio发布或Azure DevOps流水线等工具完成此过程。 运行IronWord需要任何特定的Azure服务吗? IronWord可以在标准的Azure应用服务上运行。然而,为了获得最佳性能,建议使用根据应用程序的需求提供充足资源的计划。 可以在Azure Functions中使用IronWord吗? 可以,将IronWord与Azure Functions结合使用,以一种无服务器架构处理Word文档。确保Azure Functions环境具备IronWord所需的依赖项。 IronWord如何增强在Azure上的Word文档处理? IronWord通过提供强大的.NET库增强Azure上的Word文档处理,这些库能够轻松与Azure服务集成,实现Word文档的高效创建、操作和转换。 有没有办法在Azure上自动化IronWord任务? 可以,您可以使用Azure逻辑应用或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 查看许可证