IronWord 开始 在 Android 上使用 How to Read and Write Word Documents on Android in .NET MAUI Kye Stuart 已更新:八月 20, 2025 Download IronWord NuGet 下载 Start Free Trial Copy for LLMs Copy for LLMs Copy page as Markdown for LLMs Open in ChatGPT Ask ChatGPT about this page Open in Gemini Ask Gemini about this page Open in Grok Ask Grok about this page Open in Perplexity Ask Perplexity about this page Share Share on Facebook Share on X (Twitter) Share on LinkedIn Copy URL Email article This article was translated from English: Does it need improvement? Translated View the article in English .NET MAUI (Multi-platform App UI) lets developers build native apps for Android, iOS, macOS, and Windows using a single C# codebase. This approach simplifies development and delivers native performance on all platforms. Although IronWord doesn’t have a dedicated Android package, it works smoothly on Android through .NET MAUI. This means you can build apps that read and write Word docs on Android devices with ease. How to Use IronWord on Android in .NET MAUI Create a .NET MAUI App Project Download the C# library to manipulate Word documents on Android Design the UI in MainPage.xaml Implement Document Handling in MainPage.xaml.cs Run Your App on Android Step 1: Create your .NET MAUI App Project Begin by opening Visual Studio and creating a new .NET MAUI App project. This project type supports building apps for multiple platforms from one codebase. Choose a clear project name like IronWordMauiAndroid to stay organized. This setup lays the foundation to build a native Android app with minimal platform-specific code. Step 2: Add the IronWord NuGet Package IronWord can be seamlessly integrated into your .NET projects through its NuGet package. To add IronWord via NuGet, right-click your project and select Manage NuGet Packages. Search for "IronWord" and install the latest stable version. Alternatively, you can easily add it using the NuGet Package Manager Console by running this line: Install-Package IronWord Step 3: Design the UI in MainPage.xaml To let users create and save Word documents on Android, you'll design a simple and clean UI using XAML. This interface includes: A multi-line text editor where users can type or edit content. A "Save as Word Document" button that triggers saving the current text to a .docx file. A status label to provide feedback or error messages to the user. Here’s the XAML markup that defines this layout: <?xml version="1.0" encoding="utf-8" ?> <ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="IronWordAndroid.MainPage"> <VerticalStackLayout Padding="20" Spacing="15"> <Editor x:Name="WordEditor" Placeholder="Enter your text..." AutoSize="TextChanges" HeightRequest="300"/> <Button Text="Save as Word Document" Clicked="OnSaveWordClicked"/> <Label x:Name="StatusLabel" FontSize="12" TextColor="Gray"/> </VerticalStackLayout> </ContentPage> <?xml version="1.0" encoding="utf-8" ?> <ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="IronWordAndroid.MainPage"> <VerticalStackLayout Padding="20" Spacing="15"> <Editor x:Name="WordEditor" Placeholder="Enter your text..." AutoSize="TextChanges" HeightRequest="300"/> <Button Text="Save as Word Document" Clicked="OnSaveWordClicked"/> <Label x:Name="StatusLabel" FontSize="12" TextColor="Gray"/> </VerticalStackLayout> </ContentPage> XML Step 4: Implement Document Handling in MainPage.xaml.cs With the UI set up, you can now add logic to handle saving documents. In MainPage.xaml.cs, set your IronWord license key in the constructor, then implement the OnSaveWordClicked method. When the button is tapped, the app creates a new WordDocument, adds the text from the Editor as a paragraph, and saves the file. On Android, it's saved to the Downloads folder; on other platforms, it uses the app’s cache directory. A try-catch block ensures any errors are caught and shown in the status label. Here’s the full working code: using IronWord; using IronWord.Models; using Microsoft.Maui.Storage; using System.Text; namespace IronWordAndroid; public partial class MainPage : ContentPage { public MainPage() { InitializeComponent(); // Set your IronWord license key License.LicenseKey = "YOUR-LICENSE-KEY"; } private async void OnSaveWordClicked(object sender, EventArgs e) { try { // Create new Word document var document = new WordDocument(); Paragraph paragraph = new Paragraph(new TextContent(WordEditor.Text)); // Add basic text content document.AddParagraph(paragraph); // Generate a filename string fileName = $"MyWordDoc_{DateTime.Now:yyyyMMddHHmmss}.docx"; #if ANDROID string filePath = Path.Combine("/storage/emulated/0/Download", fileName); #else string filePath = Path.Combine(FileSystem.CacheDirectory, fileName); #endif // Save to path document.SaveAs(filePath); StatusLabel.Text = $"Saved to: {filePath}"; await DisplayAlert("Success", $"File saved: {filePath}", "OK"); } catch (Exception ex) { StatusLabel.Text = $"Error: {ex.Message}"; } } } using IronWord; using IronWord.Models; using Microsoft.Maui.Storage; using System.Text; namespace IronWordAndroid; public partial class MainPage : ContentPage { public MainPage() { InitializeComponent(); // Set your IronWord license key License.LicenseKey = "YOUR-LICENSE-KEY"; } private async void OnSaveWordClicked(object sender, EventArgs e) { try { // Create new Word document var document = new WordDocument(); Paragraph paragraph = new Paragraph(new TextContent(WordEditor.Text)); // Add basic text content document.AddParagraph(paragraph); // Generate a filename string fileName = $"MyWordDoc_{DateTime.Now:yyyyMMddHHmmss}.docx"; #if ANDROID string filePath = Path.Combine("/storage/emulated/0/Download", fileName); #else string filePath = Path.Combine(FileSystem.CacheDirectory, fileName); #endif // Save to path document.SaveAs(filePath); StatusLabel.Text = $"Saved to: {filePath}"; await DisplayAlert("Success", $"File saved: {filePath}", "OK"); } catch (Exception ex) { StatusLabel.Text = $"Error: {ex.Message}"; } } } Imports IronWord Imports IronWord.Models Imports Microsoft.Maui.Storage Imports System.Text Namespace IronWordAndroid Partial Public Class MainPage Inherits ContentPage Public Sub New() InitializeComponent() ' Set your IronWord license key License.LicenseKey = "YOUR-LICENSE-KEY" End Sub Private Async Sub OnSaveWordClicked(ByVal sender As Object, ByVal e As EventArgs) Try ' Create new Word document Dim document = New WordDocument() Dim paragraph As New Paragraph(New TextContent(WordEditor.Text)) ' Add basic text content document.AddParagraph(paragraph) ' Generate a filename Dim fileName As String = $"MyWordDoc_{DateTime.Now:yyyyMMddHHmmss}.docx" #If ANDROID Then Dim filePath As String = Path.Combine("/storage/emulated/0/Download", fileName) #Else Dim filePath As String = Path.Combine(FileSystem.CacheDirectory, fileName) #End If ' Save to path document.SaveAs(filePath) StatusLabel.Text = $"Saved to: {filePath}" Await DisplayAlert("Success", $"File saved: {filePath}", "OK") Catch ex As Exception StatusLabel.Text = $"Error: {ex.Message}" End Try End Sub End Class End Namespace $vbLabelText $csharpLabel Step 5: Run Your App on Android Deploy your application effortlessly to an Android emulator or physical device straight from Visual Studio. This allows you to quickly test essential features such as opening Word documents, editing their content, and saving any changes. The integration of the native Android file picker offers a seamless and familiar experience for users, improving overall usability. Since IronWord runs entirely within the .NET environment, there’s no need for additional SDKs or platform-specific dependencies—making your app simpler to develop, maintain, and distribute. 常见问题解答 什么是 IronWord? IronWord 是 Iron Software 开发的一个库,允许开发人员在其应用程序中处理 Word 文档,包括使用 .NET MAUI 的 Android 应用。 如何将 IronWord 集成到 Android 应用程序中? 您可以通过遵循 Iron Software 网站上的设置说明,将库添加到您的 .NET MAUI 项目中来将 IronWord 集成到您的 Android 应用程序。 使用 IronWord 进行 Android 开发有什么好处? IronWord 提供了一种简单高效的方法来在 Android 应用程序中读取、写入和操作 Word 文档,利用 .NET MAUI 的强大功能实现无缝的跨平台开发。 IronWord 能处理复杂的 Word 文档吗? 是的,IronWord 旨在处理复杂的 Word 文档,包括具有高级格式和嵌入式元素的文档,使其成为开发人员的理想选择。 IronWord 与 .NET MAUI 兼容吗? 是的,IronWord 完全兼容 .NET MAUI,允许开发人员构建包含 Word 文档处理功能的跨平台应用程序。 IronWord 能处理哪些文件格式? IronWord 主要处理 Word 文档格式,例如 DOCX 和 DOC,提供全面的读取和写入这些文件的功能。 IronWord 是否支持 Android 上的文档转换? IronWord 支持文档转换功能,允许开发人员根据需要在 Android 应用程序中将 Word 文档转换为其他格式。 有没有 IronWord 的试用版可用? 是的,Iron Software 提供 IronWord 的免费试用版,允许开发人员在购买前探索其功能。 IronWord 用户可以获得什么样的支持? Iron Software 为 IronWord 用户提供全面的支持,包括文档、教程和专门的支持团队以协助解决任何问题。 IronWord 可以用于在 Android 上创建 Word 模板吗? 是的,IronWord 可用于创建和处理 Word 模板,非常适合需要在 Android 设备上生成模板化文档的应用程序。 Kye Stuart 立即与工程团队聊天 技术作家 Kye Stuart 在 Iron Software 中将编码热情与写作技能结合在一起。他在 Yoobee 学院接受软件部署教育,现在将复杂的技术概念转化为清晰的教育内容。Kye 重视终身学习,接受新的技术挑战。工作之余,他们喜欢 PC 游戏、Twitch 上的直播,以及户外活动如园艺和带狗 Jaiya 散步。Kye 的直截了当的方法使他们成为 Iron Software 使命的关键,即为全球开发者解密技术。 准备开始了吗? Nuget 下载 25,807 | 版本: 2025.11 刚刚发布 免费 NuGet 下载 总下载量:25,807 查看许可证