如何在 Android 上使用 .NET MAUI 讀取和寫入 Word 文檔
.NET MAUI(多平台應用程式 UI)允許開發人員使用單一 C# 程式碼庫為 Android、iOS、macOS 和 Windows 建立原生應用程式。 這種方法簡化了開發,並在所有平台上實現了原生效能。
雖然 IronWord 沒有專門的 Android 軟體包,但它可以透過 .NET MAUI 在 Android 上流暢運行。 這意味著您可以輕鬆地在 Android 裝置上建立讀取和寫入 Word 文件的應用程式。
如何在 Android 上使用 .NET MAUI 中的 IronWord
- 建立一個 .NET MAUI 應用程式項目
- 下載 C# 庫,以便在 Android 上操作 Word 文件。
- 在 MainPage.xaml 中設計 UI
- 在 MainPage.xaml.cs 中實作文件處理
- 在安卓系統上運行您的應用
步驟 1:建立您的 .NET MAUI 應用程式專案
首先開啟 Visual Studio 並建立一個新的 .NET MAUI 應用程式專案。 此類專案支援從同一程式碼庫建立適用於多個平台的應用程式。
為了方便管理,請選擇一個清晰明了的專案名稱,例如 IronWordMauiAndroid。 這種設定為建構原生 Android 應用程式奠定了基礎,最大限度地減少了平台特定的程式碼。
步驟 2:新增 IronWord NuGet 套件
IronWord 可透過其 NuGet 套件無縫整合到您的 .NET 專案中。 若要透過 NuGet 新增 IronWord,請在您的專案中按一下滑鼠右鍵並選擇"管理 NuGet 套件"。 搜尋"IronWord"並安裝最新穩定版本。
或者,您也可以透過執行以下命令,使用 NuGet 套件管理器控制台輕鬆新增它:
Install-Package IronWord
步驟 3:在 MainPage.xaml 中設計使用者介面
為了讓使用者在 Android 上建立和儲存 Word 文檔,您將使用 XAML 設計一個簡潔明了的使用者介面。 此介麵包括:
- 多行文字編輯器,使用者可以在其中輸入或編輯內容。
- 新增一個"另存為 Word 文件"按鈕,點擊後可將目前文字儲存為 .docx 檔案。
- 用於向使用者提供回饋或錯誤訊息的狀態標籤。
以下是定義此佈局的 XAML 標記:
<?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>步驟 4:在 MainPage.xaml.cs 中實作文件處理
使用者介面設定完成後,現在可以新增邏輯來處理文件保存。 在 MainPage.xaml.cs 中,在建構函式中設定您的 IronWord 授權金鑰,然後實作 OnSaveWordClicked 方法。
點擊按鈕後,應用程式會建立一個新的 Word 文檔,將編輯器中的文字作為段落新增到文檔中,然後儲存文件。在 Android 系統上,檔案會儲存到"下載"資料夾中; 在其他平台上,它使用應用程式的快取目錄。
try-catch 區塊可確保擷取所有錯誤並在狀態標籤中顯示。
以下是完整的可運行程式碼:
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步驟五:在安卓裝置上運行你的應用
直接從 Visual Studio 輕鬆將應用程式部署到 Android 模擬器或實體裝置。 這樣一來,您就可以快速測試一些基本功能,例如開啟 Word 文件、編輯其內容以及儲存任何變更。
整合原生 Android 檔案選擇器為使用者提供流暢熟悉的體驗,提高了整體可用性。
由於 IronWord 完全在 .NET 環境中運行,因此無需額外的 SDK 或特定於平台的依賴項,從而使您的應用程式更易於開發、維護和分發。
常見問題解答
什麼是 IronWord?
IronWord 是由 Iron Software 開發的函式庫,允許開發者在其應用程式中包括 Android 應用使用 .NET MAUI 處理 Word 文件。
我如何能在 Android 應用程式中整合 IronWord?
您可以依照 Iron Software 網站上的設置說明將 IronWord 整合到您的 Android 應用程式中,此指南將引導您如何將函式庫添加到您的 .NET MAUI 專案。
使用 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 設備上製作範本文件生成的應用程式。






