C#を使用して.NET MAUIでExcelファイルを読み取り作成する方法 | IronXL

Create, Read and Edit Excel Files in .NET MAUI

This article was translated from English: Does it need improvement?
Translated
View the article in English

はじめに

このガイドでは、Windows向けの.NET MAUIアプリケーションでIronXLを使用してExcelファイルを作成および読み取る方法を説明します。 さあ始めましょう。

IronXL: C# Excelライブラリ

IronXLは、Excelファイルを読み書きおよび操作するためのC# .NETライブラリです。 これにより、ユーザーはExcelのコンテンツと外観、タイトルや著者といったメタデータを含むExcelドキュメントをゼロから作成できます。 また、ユーザーインターフェイスのカスタマイズ機能をサポートしており、マージンの設定、向き、ページサイズ、画像などを設定できます。 Excelファイルを生成するには、外部フレームワーク、プラットフォーム統合、他のサードパーティライブラリを必要としません。 自己完結型のスタンドアロンです。

class="hsg-featured-snippet">

.NET MAUIでExcelファイルを読み取る方法

  1. C#ライブラリをインストールしてExcelファイルを読み取る
  2. MAUIアプリケーションを実行するために必要なすべてのパッケージをインストールする
  3. マウイで直感的なAPIを使用してExcelファイルを作成する
  4. ブラウザでExcelファイルを読み込み、表示する
  5. Excelファイルを保存し、エクスポートする

IronXLをインストール

今日あなたのプロジェクトでIronXLを無料トライアルで使用開始。

最初のステップ:
green arrow pointer


IronXLをインストールするには、Visual StudioのNuGetパッケージマネージャコンソールを使用できます。 コンソールを開き、IronXLライブラリをインストールするために次のコマンドを入力してください。

Install-Package IronXL.Excel

IronXLを使用したC#でのExcelファイルの作成

アプリケーションフロントエンドをデザインする

MainPage.xamlと名付けられた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="MAUI_IronXL.MainPage">

    <ScrollView>
        <VerticalStackLayout
            Spacing="25"
            Padding="30,0"
            VerticalOptions="Center">

            <Label
                Text="Welcome to .NET Multi-platform App UI"
                SemanticProperties.HeadingLevel="Level2"
                SemanticProperties.Description="Welcome Multi-platform App UI"
                FontSize="18"
                HorizontalOptions="Center" />

            <Button
                x:Name="createBtn"
                Text="Create Excel File"
                SemanticProperties.Hint="Click on the button to create Excel file"
                Clicked="CreateExcel"
                HorizontalOptions="Center" />

            <Button
                x:Name="readExcel"
                Text="Read and Modify Excel file"
                SemanticProperties.Hint="Click on the button to read Excel file"
                Clicked="ReadExcel"
                HorizontalOptions="Center" />

        </VerticalStackLayout>
    </ScrollView>

</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="MAUI_IronXL.MainPage">

    <ScrollView>
        <VerticalStackLayout
            Spacing="25"
            Padding="30,0"
            VerticalOptions="Center">

            <Label
                Text="Welcome to .NET Multi-platform App UI"
                SemanticProperties.HeadingLevel="Level2"
                SemanticProperties.Description="Welcome Multi-platform App UI"
                FontSize="18"
                HorizontalOptions="Center" />

            <Button
                x:Name="createBtn"
                Text="Create Excel File"
                SemanticProperties.Hint="Click on the button to create Excel file"
                Clicked="CreateExcel"
                HorizontalOptions="Center" />

            <Button
                x:Name="readExcel"
                Text="Read and Modify Excel file"
                SemanticProperties.Hint="Click on the button to read Excel file"
                Clicked="ReadExcel"
                HorizontalOptions="Center" />

        </VerticalStackLayout>
    </ScrollView>

</ContentPage>
XML

上記のコードは、基本的な.NET MAUIアプリケーションのレイアウトを作成します。 1つのラベルと2つのボタンを作成します。 1つのボタンはExcelファイルの作成用で、もう1つのボタンはExcelファイルの読み取りと編集をサポートしています。両方の要素はVerticalStackLayoutの親要素にネストされているため、サポートされているすべてのデバイスで縦に配置されて表示されます。

Excelファイルを作成する

IronXLを使用してExcelファイルを作成する時が来ました。 MainPage.xaml.csファイルを開き、次のメソッドをファイルに記述します。

private void CreateExcel(object sender, EventArgs e)
{
    // Create a new Workbook
    WorkBook workbook = WorkBook.Create(ExcelFileFormat.XLSX);

    // Create a Worksheet
    var sheet = workbook.CreateWorkSheet("2022 Budget");

    // Set cell headers
    sheet["A1"].Value = "January";
    sheet["B1"].Value = "February";
    sheet["C1"].Value = "March";
    sheet["D1"].Value = "April";
    sheet["E1"].Value = "May";
    sheet["F1"].Value = "June";
    sheet["G1"].Value = "July";
    sheet["H1"].Value = "August";

    // Fill worksheet cells with random values
    Random r = new Random();
    for (int i = 2; i <= 11; i++)
    {
        sheet["A" + i].Value = r.Next(1, 1000);
        sheet["B" + i].Value = r.Next(1000, 2000);
        sheet["C" + i].Value = r.Next(2000, 3000);
        sheet["D" + i].Value = r.Next(3000, 4000);
        sheet["E" + i].Value = r.Next(4000, 5000);
        sheet["F" + i].Value = r.Next(5000, 6000);
        sheet["G" + i].Value = r.Next(6000, 7000);
        sheet["H" + i].Value = r.Next(7000, 8000);
    }

    // Apply formatting (background and border)
    sheet["A1:H1"].Style.SetBackgroundColor("#d3d3d3");
    sheet["A1:H1"].Style.TopBorder.SetColor("#000000");
    sheet["A1:H1"].Style.BottomBorder.SetColor("#000000");
    sheet["H2:H11"].Style.RightBorder.SetColor("#000000");
    sheet["H2:H11"].Style.RightBorder.Type = IronXL.Styles.BorderType.Medium;
    sheet["A11:H11"].Style.BottomBorder.SetColor("#000000");
    sheet["A11:H11"].Style.BottomBorder.Type = IronXL.Styles.BorderType.Medium;

    // Apply formulas
    decimal sum = sheet["A2:A11"].Sum();
    decimal avg = sheet["B2:B11"].Avg();
    decimal max = sheet["C2:C11"].Max();
    decimal min = sheet["D2:D11"].Min();

    sheet["A12"].Value = "Sum";
    sheet["B12"].Value = sum;

    sheet["C12"].Value = "Avg";
    sheet["D12"].Value = avg;

    sheet["E12"].Value = "Max";
    sheet["F12"].Value = max;

    sheet["G12"].Value = "Min";
    sheet["H12"].Value = min;

    // Save and open the Excel file
    SaveService saveService = new SaveService();
    saveService.SaveAndView("Budget.xlsx", "application/octet-stream", workbook.ToStream());
}
private void CreateExcel(object sender, EventArgs e)
{
    // Create a new Workbook
    WorkBook workbook = WorkBook.Create(ExcelFileFormat.XLSX);

    // Create a Worksheet
    var sheet = workbook.CreateWorkSheet("2022 Budget");

    // Set cell headers
    sheet["A1"].Value = "January";
    sheet["B1"].Value = "February";
    sheet["C1"].Value = "March";
    sheet["D1"].Value = "April";
    sheet["E1"].Value = "May";
    sheet["F1"].Value = "June";
    sheet["G1"].Value = "July";
    sheet["H1"].Value = "August";

    // Fill worksheet cells with random values
    Random r = new Random();
    for (int i = 2; i <= 11; i++)
    {
        sheet["A" + i].Value = r.Next(1, 1000);
        sheet["B" + i].Value = r.Next(1000, 2000);
        sheet["C" + i].Value = r.Next(2000, 3000);
        sheet["D" + i].Value = r.Next(3000, 4000);
        sheet["E" + i].Value = r.Next(4000, 5000);
        sheet["F" + i].Value = r.Next(5000, 6000);
        sheet["G" + i].Value = r.Next(6000, 7000);
        sheet["H" + i].Value = r.Next(7000, 8000);
    }

    // Apply formatting (background and border)
    sheet["A1:H1"].Style.SetBackgroundColor("#d3d3d3");
    sheet["A1:H1"].Style.TopBorder.SetColor("#000000");
    sheet["A1:H1"].Style.BottomBorder.SetColor("#000000");
    sheet["H2:H11"].Style.RightBorder.SetColor("#000000");
    sheet["H2:H11"].Style.RightBorder.Type = IronXL.Styles.BorderType.Medium;
    sheet["A11:H11"].Style.BottomBorder.SetColor("#000000");
    sheet["A11:H11"].Style.BottomBorder.Type = IronXL.Styles.BorderType.Medium;

    // Apply formulas
    decimal sum = sheet["A2:A11"].Sum();
    decimal avg = sheet["B2:B11"].Avg();
    decimal max = sheet["C2:C11"].Max();
    decimal min = sheet["D2:D11"].Min();

    sheet["A12"].Value = "Sum";
    sheet["B12"].Value = sum;

    sheet["C12"].Value = "Avg";
    sheet["D12"].Value = avg;

    sheet["E12"].Value = "Max";
    sheet["F12"].Value = max;

    sheet["G12"].Value = "Min";
    sheet["H12"].Value = min;

    // Save and open the Excel file
    SaveService saveService = new SaveService();
    saveService.SaveAndView("Budget.xlsx", "application/octet-stream", workbook.ToStream());
}
Private Sub CreateExcel(ByVal sender As Object, ByVal e As EventArgs)
	' Create a new Workbook
	Dim workbook As WorkBook = WorkBook.Create(ExcelFileFormat.XLSX)

	' Create a Worksheet
	Dim sheet = workbook.CreateWorkSheet("2022 Budget")

	' Set cell headers
	sheet("A1").Value = "January"
	sheet("B1").Value = "February"
	sheet("C1").Value = "March"
	sheet("D1").Value = "April"
	sheet("E1").Value = "May"
	sheet("F1").Value = "June"
	sheet("G1").Value = "July"
	sheet("H1").Value = "August"

	' Fill worksheet cells with random values
	Dim r As New Random()
	For i As Integer = 2 To 11
		sheet("A" & i).Value = r.Next(1, 1000)
		sheet("B" & i).Value = r.Next(1000, 2000)
		sheet("C" & i).Value = r.Next(2000, 3000)
		sheet("D" & i).Value = r.Next(3000, 4000)
		sheet("E" & i).Value = r.Next(4000, 5000)
		sheet("F" & i).Value = r.Next(5000, 6000)
		sheet("G" & i).Value = r.Next(6000, 7000)
		sheet("H" & i).Value = r.Next(7000, 8000)
	Next i

	' Apply formatting (background and border)
	sheet("A1:H1").Style.SetBackgroundColor("#d3d3d3")
	sheet("A1:H1").Style.TopBorder.SetColor("#000000")
	sheet("A1:H1").Style.BottomBorder.SetColor("#000000")
	sheet("H2:H11").Style.RightBorder.SetColor("#000000")
	sheet("H2:H11").Style.RightBorder.Type = IronXL.Styles.BorderType.Medium
	sheet("A11:H11").Style.BottomBorder.SetColor("#000000")
	sheet("A11:H11").Style.BottomBorder.Type = IronXL.Styles.BorderType.Medium

	' Apply formulas
	Dim sum As Decimal = sheet("A2:A11").Sum()
	Dim avg As Decimal = sheet("B2:B11").Avg()
	Dim max As Decimal = sheet("C2:C11").Max()
	Dim min As Decimal = sheet("D2:D11").Min()

	sheet("A12").Value = "Sum"
	sheet("B12").Value = sum

	sheet("C12").Value = "Avg"
	sheet("D12").Value = avg

	sheet("E12").Value = "Max"
	sheet("F12").Value = max

	sheet("G12").Value = "Min"
	sheet("H12").Value = min

	' Save and open the Excel file
	Dim saveService As New SaveService()
	saveService.SaveAndView("Budget.xlsx", "application/octet-stream", workbook.ToStream())
End Sub
$vbLabelText   $csharpLabel

このソースコードは、IronXLを使用してワークブックとワークシートを作成し、セル値を設定し、セルをフォーマットします。 また、IronXLを使用したExcelの数式の使い方も示しています。

ブラウザでExcelファイルを表示

MainPage.xaml.csファイルを開き、次のコードを記述します。

private void ReadExcel(object sender, EventArgs e)
{
    // Store the path of the file
    string filepath = @"C:\Files\Customer Data.xlsx";
    WorkBook workbook = WorkBook.Load(filepath);
    WorkSheet sheet = workbook.WorkSheets.First();

    // Calculate the sum of a range
    decimal sum = sheet["B2:B10"].Sum();

    // Modify a cell value and apply styles
    sheet["B11"].Value = sum;
    sheet["B11"].Style.SetBackgroundColor("#808080");
    sheet["B11"].Style.Font.SetColor("#ffffff");

    // Save and open the Excel file
    SaveService saveService = new SaveService();
    saveService.SaveAndView("Modified Data.xlsx", "application/octet-stream", workbook.ToStream());

    DisplayAlert("Notification", "Excel file has been modified!", "OK");
}
private void ReadExcel(object sender, EventArgs e)
{
    // Store the path of the file
    string filepath = @"C:\Files\Customer Data.xlsx";
    WorkBook workbook = WorkBook.Load(filepath);
    WorkSheet sheet = workbook.WorkSheets.First();

    // Calculate the sum of a range
    decimal sum = sheet["B2:B10"].Sum();

    // Modify a cell value and apply styles
    sheet["B11"].Value = sum;
    sheet["B11"].Style.SetBackgroundColor("#808080");
    sheet["B11"].Style.Font.SetColor("#ffffff");

    // Save and open the Excel file
    SaveService saveService = new SaveService();
    saveService.SaveAndView("Modified Data.xlsx", "application/octet-stream", workbook.ToStream());

    DisplayAlert("Notification", "Excel file has been modified!", "OK");
}
Private Sub ReadExcel(ByVal sender As Object, ByVal e As EventArgs)
	' Store the path of the file
	Dim filepath As String = "C:\Files\Customer Data.xlsx"
	Dim workbook As WorkBook = WorkBook.Load(filepath)
	Dim sheet As WorkSheet = workbook.WorkSheets.First()

	' Calculate the sum of a range
	Dim sum As Decimal = sheet("B2:B10").Sum()

	' Modify a cell value and apply styles
	sheet("B11").Value = sum
	sheet("B11").Style.SetBackgroundColor("#808080")
	sheet("B11").Style.Font.SetColor("#ffffff")

	' Save and open the Excel file
	Dim saveService As New SaveService()
	saveService.SaveAndView("Modified Data.xlsx", "application/octet-stream", workbook.ToStream())

	DisplayAlert("Notification", "Excel file has been modified!", "OK")
End Sub
$vbLabelText   $csharpLabel

ソースコードはExcelファイルを読み込み、セル範囲に数式を適用し、カスタムの背景色とテキスト色でフォーマットします。 その後、変更されたExcelファイルを保存し、通知が表示されます。

Excelファイルを保存する

このセクションでは、ローカルストレージにExcelファイルを保存するSaveServiceクラスを定義します。

"SaveService.cs"クラスを作成し、次のコードを書いてください:

using System;
using System.IO;

namespace MAUI_IronXL
{
    public partial class SaveService
    {
        public partial void SaveAndView(string fileName, string contentType, MemoryStream stream);
    }
}
using System;
using System.IO;

namespace MAUI_IronXL
{
    public partial class SaveService
    {
        public partial void SaveAndView(string fileName, string contentType, MemoryStream stream);
    }
}
Imports System
Imports System.IO

Namespace MAUI_IronXL
	Partial Public Class SaveService
		Public Partial Private Sub SaveAndView(ByVal fileName As String, ByVal contentType As String, ByVal stream As MemoryStream)
		End Sub
	End Class
End Namespace
$vbLabelText   $csharpLabel

次に、「Platforms > Windows」フォルダー内に「SaveWindows.cs」と名付けたクラスを作成し、以下のコードを追加してください:

using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Storage;
using Windows.Storage.Pickers;
using Windows.Storage.Streams;
using Windows.UI.Popups;

namespace MAUI_IronXL
{
    public partial class SaveService
    {
        public async partial void SaveAndView(string fileName, string contentType, MemoryStream stream)
        {
            StorageFile stFile;
            string extension = Path.GetExtension(fileName);
            IntPtr windowHandle = System.Diagnostics.Process.GetCurrentProcess().MainWindowHandle;

            if (!Windows.Foundation.Metadata.ApiInformation.IsTypePresent("Windows.Phone.UI.Input.HardwareButtons"))
            {
                FileSavePicker savePicker = new FileSavePicker();
                savePicker.DefaultFileExtension = ".xlsx";
                savePicker.SuggestedFileName = fileName;
                savePicker.FileTypeChoices.Add("XLSX", new List<string> { ".xlsx" });

                WinRT.Interop.InitializeWithWindow.Initialize(savePicker, windowHandle);
                stFile = await savePicker.PickSaveFileAsync();
            }
            else
            {
                StorageFolder local = ApplicationData.Current.LocalFolder;
                stFile = await local.CreateFileAsync(fileName, CreationCollisionOption.ReplaceExisting);
            }

            if (stFile != null)
            {
                using (IRandomAccessStream zipStream = await stFile.OpenAsync(FileAccessMode.ReadWrite))
                {
                    using (Stream outputStream = zipStream.AsStreamForWrite())
                    {
                        outputStream.SetLength(0);
                        stream.WriteTo(outputStream);
                        await outputStream.FlushAsync();
                    }
                }

                MessageDialog msgDialog = new("Do you want to view the document?", "File has been created successfully");
                UICommand yesCmd = new("Yes");
                msgDialog.Commands.Add(yesCmd);
                UICommand noCmd = new("No");
                msgDialog.Commands.Add(noCmd);

                WinRT.Interop.InitializeWithWindow.Initialize(msgDialog, windowHandle);

                IUICommand cmd = await msgDialog.ShowAsync();
                if (cmd.Label == yesCmd.Label)
                {
                    await Windows.System.Launcher.LaunchFileAsync(stFile);
                }
            }
        }
    }
}
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Storage;
using Windows.Storage.Pickers;
using Windows.Storage.Streams;
using Windows.UI.Popups;

namespace MAUI_IronXL
{
    public partial class SaveService
    {
        public async partial void SaveAndView(string fileName, string contentType, MemoryStream stream)
        {
            StorageFile stFile;
            string extension = Path.GetExtension(fileName);
            IntPtr windowHandle = System.Diagnostics.Process.GetCurrentProcess().MainWindowHandle;

            if (!Windows.Foundation.Metadata.ApiInformation.IsTypePresent("Windows.Phone.UI.Input.HardwareButtons"))
            {
                FileSavePicker savePicker = new FileSavePicker();
                savePicker.DefaultFileExtension = ".xlsx";
                savePicker.SuggestedFileName = fileName;
                savePicker.FileTypeChoices.Add("XLSX", new List<string> { ".xlsx" });

                WinRT.Interop.InitializeWithWindow.Initialize(savePicker, windowHandle);
                stFile = await savePicker.PickSaveFileAsync();
            }
            else
            {
                StorageFolder local = ApplicationData.Current.LocalFolder;
                stFile = await local.CreateFileAsync(fileName, CreationCollisionOption.ReplaceExisting);
            }

            if (stFile != null)
            {
                using (IRandomAccessStream zipStream = await stFile.OpenAsync(FileAccessMode.ReadWrite))
                {
                    using (Stream outputStream = zipStream.AsStreamForWrite())
                    {
                        outputStream.SetLength(0);
                        stream.WriteTo(outputStream);
                        await outputStream.FlushAsync();
                    }
                }

                MessageDialog msgDialog = new("Do you want to view the document?", "File has been created successfully");
                UICommand yesCmd = new("Yes");
                msgDialog.Commands.Add(yesCmd);
                UICommand noCmd = new("No");
                msgDialog.Commands.Add(noCmd);

                WinRT.Interop.InitializeWithWindow.Initialize(msgDialog, windowHandle);

                IUICommand cmd = await msgDialog.ShowAsync();
                if (cmd.Label == yesCmd.Label)
                {
                    await Windows.System.Launcher.LaunchFileAsync(stFile);
                }
            }
        }
    }
}
Imports System
Imports System.Collections.Generic
Imports System.IO
Imports System.Runtime.InteropServices.WindowsRuntime
Imports Windows.Storage
Imports Windows.Storage.Pickers
Imports Windows.Storage.Streams
Imports Windows.UI.Popups

Namespace MAUI_IronXL
	Partial Public Class SaveService
		Public Async Sub SaveAndView(ByVal fileName As String, ByVal contentType As String, ByVal stream As MemoryStream)
			Dim stFile As StorageFile
			Dim extension As String = Path.GetExtension(fileName)
			Dim windowHandle As IntPtr = System.Diagnostics.Process.GetCurrentProcess().MainWindowHandle

			If Not Windows.Foundation.Metadata.ApiInformation.IsTypePresent("Windows.Phone.UI.Input.HardwareButtons") Then
				Dim savePicker As New FileSavePicker()
				savePicker.DefaultFileExtension = ".xlsx"
				savePicker.SuggestedFileName = fileName
				savePicker.FileTypeChoices.Add("XLSX", New List(Of String) From {".xlsx"})

				WinRT.Interop.InitializeWithWindow.Initialize(savePicker, windowHandle)
				stFile = Await savePicker.PickSaveFileAsync()
			Else
				Dim local As StorageFolder = ApplicationData.Current.LocalFolder
				stFile = Await local.CreateFileAsync(fileName, CreationCollisionOption.ReplaceExisting)
			End If

			If stFile IsNot Nothing Then
				Using zipStream As IRandomAccessStream = Await stFile.OpenAsync(FileAccessMode.ReadWrite)
					Using outputStream As Stream = zipStream.AsStreamForWrite()
						outputStream.SetLength(0)
						stream.WriteTo(outputStream)
						Await outputStream.FlushAsync()
					End Using
				End Using

				Dim msgDialog As New MessageDialog("Do you want to view the document?", "File has been created successfully")
				Dim yesCmd As New UICommand("Yes")
				msgDialog.Commands.Add(yesCmd)
				Dim noCmd As New UICommand("No")
				msgDialog.Commands.Add(noCmd)

				WinRT.Interop.InitializeWithWindow.Initialize(msgDialog, windowHandle)

				Dim cmd As IUICommand = Await msgDialog.ShowAsync()
				If cmd.Label = yesCmd.Label Then
					Await Windows.System.Launcher.LaunchFileAsync(stFile)
				End If
			End If
		End Sub
	End Class
End Namespace
$vbLabelText   $csharpLabel

出力

MAUIプロジェクトをビルドして実行してください。 正常に実行されると、下の画像に示されるコンテンツを表示するウィンドウが開きます。

class="content-img-align-center">
class="center-image-wrapper"> .NET MAUIでのExcelファイルの読み取り、作成、編集、図1: 出力 図1 - 出力

「Excelファイルを作成」ボタンをクリックすると、別のダイアログウィンドウが開きます。 このウィンドウでは、新しい(生成された)Excelファイルを保存する場所とファイル名を指定するようユーザーに求めます。指示に従って場所とファイル名を指定し、OKをクリックします。 その後、別のダイアログウィンドウが表示されます。

class="content-img-align-center">
class="center-image-wrapper"> .NET MAUIでのExcelファイルの読み取り、作成、編集、図2: Excel作成ポップアップ 図2 - Excel作成ポップアップ

案内ポップアップに従ってExcelファイルを開くと、以下のスクリーンショットに示すようなドキュメントが表示されます。

class="content-img-align-center">
class="center-image-wrapper"> .NET MAUIでのExcelファイルの読み取り、作成、編集、図3: 出力 図3 - 読み取りおよび編集ポップアップ

「Excelファイルを読み取りおよび編集」ボタンをクリックすると、先に生成されたExcelファイルが読み込まれ、以前のセクションで定義したカスタムの背景色とテキスト色で修正されます。

class="content-img-align-center">
class="center-image-wrapper"> .NET MAUIでのExcelファイルの読み取り、作成、編集、図4: Excel出力 図4 - Excel出力

修正されたファイルを開くと、以下の目次を持つ出力が表示されます。

class="content-img-align-center">
class="center-image-wrapper"> .NET MAUIでのExcelファイルの読み取り、作成、編集、図5: 修正されたExcel出力 図5 - 修正されたExcel出力

結論

これによって、IronXLライブラリを使用して.NET MAUIアプリケーションでExcelファイルを作成、読み取り、および修正する方法を説明しました。 IronXLは非常に優れた性能を発揮し、高速かつ正確にすべての操作を実行します。 これはMicrosoft Interopを必要とせず、マシンにMicrosoft Officeスイートをインストールすることなく、Excel操作に優れたライブラリです。加えて、IronXLはワークブックとワークシートの作成、セル範囲の操作、フォーマット、およびCSV、TSVなどのさまざまなドキュメント形式へのエクスポートなど、複数の操作をサポートします。

IronXLは、Windowsフォーム、WPF、ASP.NET Coreなどのすべてのプロジェクトテンプレートをサポートしています。 Refer to our tutorials for creating Excel files and reading Excel files for additional information about how to use IronXL.


class="tutorial-segment-title">クイックアクセスリンク

class="tutorial-section">
class="row">
class="col-sm-8">

GitHubでこのHow-Toガイドを探索する

このプロジェクトのソースコードはGitHubで利用可能です。

このコードを使用すると、数分で簡単に開始できます。プロジェクトはMicrosoft Visual Studio 2022プロジェクトとして保存されていますが、任意の.NET IDEと互換性があります。

.NET MAUIアプリでのExcelファイルの読み取り、作成、および編集方法
class="col-sm-4">
class="tutorial-image"> Github Icon related to class=クイックアクセスリンク" class="img-responsive add-shadow img-responsive img-popup" src="/img/svgs/github-icon.svg" loading="lazy">
class="tutorial-section">
class="row">
class="col-sm-4">
class="tutorial-image"> Documentation related to class=クイックアクセスリンク" class="img-responsive add-shadow img-responsive img-popup" src="/img/svgs/documentation.svg" loading="lazy">
class="col-sm-8">

APIリファレンスを表示する

IronXLのすべての機能、ネームスペース、クラス、メソッド、フィールド、列挙を詳述したAPIリファレンスを探索してください。

APIリファレンスを表示する

よくある質問

どうすれば.NET MAUIアプリケーションでExcelファイルを作成できますか?

.NET MAUIプロジェクトでExcelファイルを作成するには、IronXLライブラリを使用して新しいワークブックとワークシートを初期化します。その後、セルの値を設定し、Excelの数式を適用し、カスタムSaveServiceクラスを使用してファイルを保存する前にフォーマットをカスタマイズできます。

既存のExcelファイルを.NET MAUIアプリケーションで読み取ることはできますか?

はい、IronXLを使用して.NET MAUIアプリケーションで既存のExcelファイルを読み込んで読み取ることができます。このライブラリを使用すると、セルの値にアクセスして変更し、数式を適用し、カスタムフォーマットを実装できます。

IronXLを.NET MAUIでExcelファイル操作に使用する利点は何ですか?

IronXLは、.NET MAUIでExcelファイル操作のための自己完結型のソリューションを提供し、Microsoft OfficeやInteropを必要としません。効率的にExcelファイルを作成、読み取り、編集でき、CSVやTSVなどのさまざまな形式にエクスポートすることができます。

.NET MAUIプロジェクトにIronXLをインストールするにはどうすればよいですか?

Visual StudioのNuGetパッケージマネージャーコンソールを使用して.NET MAUIプロジェクトにIronXLをインストールできます。コマンドInstall-Package IronXL.Excelを実行して、ライブラリをプロジェクトに追加します。

.NET MAUIでプログラム的にExcelのセルをフォーマットすることは可能ですか?

はい、IronXLを使用すると、.NET MAUIでプログラム的にExcelのセルをフォーマットできます。これには、セルのスタイルや色を設定し、さまざまなフォーマットオプションを適用してExcelファイルの見た目を向上させることが含まれます。

.NET MAUIアプリでExcelファイルのためのSaveServiceクラスを実装するにはどうすればいいですか?

.NET MAUIでSaveServiceクラスを実装するには、IronXLの機能を利用してExcelファイルをローカルストレージに保存するクラスを作成できます。これには、ファイルパスを指定し、ファイルI/O操作を管理するメソッドを定義することが含まれます。

IronXLは.NETアプリケーションでどのプロジェクトテンプレートをサポートしていますか?

IronXLは、Windows Forms、WPF、ASP.NET Coreなど、さまざまな.NETプロジェクトテンプレートをサポートし、異なるアプリケーションタイプで作業する開発者に柔軟性を提供しています。

.NET MAUI Excelプロジェクトのソースコードはどこで見つけられますか?

IronXLを使用した.NET MAUI ExcelプロジェクトのソースコードはGitHubで入手可能です。これにより、開発者はVisual Studio 2022を使用して迅速にセットアップし、アプリケーションでのExcelファイル操作を試すことができます。

Curtis Chau
テクニカルライター

Curtis Chauは、カールトン大学でコンピュータサイエンスの学士号を取得し、Node.js、TypeScript、JavaScript、およびReactに精通したフロントエンド開発を専門としています。直感的で美しいユーザーインターフェースを作成することに情熱を持ち、Curtisは現代のフレームワークを用いた開発や、構造の良い視覚的に魅力的なマニュアルの作成を楽しんでいます。

開発以外にも、CurtisはIoT(Internet of Things)への強い関心を持ち、ハードウェアとソフトウェアの統合方法を模索しています。余暇には、ゲームをしたりDiscordボットを作成したりして、技術に対する愛情と創造性を組み合わせています。

準備はいいですか?
Nuget ダウンロード 1,686,155 | バージョン: 2025.11 ただ今リリースされました