Test in production without watermarks.
Works wherever you need it to.
Get 30 days of fully functional product.
Have it up and running in minutes.
Full access to our support engineering team during your product trial
In this tutorial, we delve into creating, reading, and editing Excel files in .NET MAUI using IronXL. The guide begins with designing the application's front-end, including setting up a simple layout using XAML with a label and two buttons for creating and reading Excel files. We then explore the creation of Excel files by defining methods that initialize a workbook, set cell values, apply formatting, and save the file using a save service class. This involves implementing formulas for calculations and visual enhancements like background colors and borders. Next, we discuss reading and modifying Excel files by loading them, applying formulas, and altering cell formatting, with changes saved and displayed to users. The tutorial also details the save service class, responsible for saving files using the file save picker dialogue, and platform-specific code for Windows to manage file-saving processes. By following these steps, users can effectively manipulate Excel files within a .NET MAUI application, expanding their application's capabilities. We encourage viewers to try out the trial subscription of IronXL for a hands-on experience.
XAML Front-End Setup
<!-- This XAML layout contains a Label and two Buttons for creating and reading Excel files -->
<VerticalStackLayout>
<Label
Text="IronXL .NET MAUI Excel Example"
VerticalOptions="Center"
HorizontalOptions="Center" />
<Button
Text="Create Excel File"
Clicked="OnCreateExcelFileClicked"
VerticalOptions="Center"
HorizontalOptions="Center" />
<Button
Text="Read Excel File"
Clicked="OnReadExcelFileClicked"
VerticalOptions="Center"
HorizontalOptions="Center" />
</VerticalStackLayout>
<!-- This XAML layout contains a Label and two Buttons for creating and reading Excel files -->
<VerticalStackLayout>
<Label
Text="IronXL .NET MAUI Excel Example"
VerticalOptions="Center"
HorizontalOptions="Center" />
<Button
Text="Create Excel File"
Clicked="OnCreateExcelFileClicked"
VerticalOptions="Center"
HorizontalOptions="Center" />
<Button
Text="Read Excel File"
Clicked="OnReadExcelFileClicked"
VerticalOptions="Center"
HorizontalOptions="Center" />
</VerticalStackLayout>
C# Code for Excel File Creation
// Import IronXL namespaces
using IronXL;
using System;
namespace MyExcelApp
{
public class ExcelFileService
{
/// <summary>
/// Creates an Excel file with example data and saves it.
/// </summary>
public void CreateExcelFile()
{
// Initialize a new workbook
var workbook = WorkBook.Create(ExcelFileFormat.XLSX);
// Add a new worksheet to the workbook
var sheet = workbook.CreateWorkSheet("SampleSheet");
// Set values in cells and apply basic formatting
sheet["A1"].Value = "Hello";
sheet["A2"].Value = "World";
sheet["B1"].Value = DateTime.Now;
// Apply some formatting
sheet["A1:A2"].Style.BackgroundColor = "#FFD700"; // Gold background
sheet["B1"].Style.Font.Bold = true;
// Save the workbook using a save service
var saveService = new SaveService();
saveService.Save(workbook, "SampleExcel.xlsx");
Console.WriteLine("Excel file created successfully.");
}
}
}
// Import IronXL namespaces
using IronXL;
using System;
namespace MyExcelApp
{
public class ExcelFileService
{
/// <summary>
/// Creates an Excel file with example data and saves it.
/// </summary>
public void CreateExcelFile()
{
// Initialize a new workbook
var workbook = WorkBook.Create(ExcelFileFormat.XLSX);
// Add a new worksheet to the workbook
var sheet = workbook.CreateWorkSheet("SampleSheet");
// Set values in cells and apply basic formatting
sheet["A1"].Value = "Hello";
sheet["A2"].Value = "World";
sheet["B1"].Value = DateTime.Now;
// Apply some formatting
sheet["A1:A2"].Style.BackgroundColor = "#FFD700"; // Gold background
sheet["B1"].Style.Font.Bold = true;
// Save the workbook using a save service
var saveService = new SaveService();
saveService.Save(workbook, "SampleExcel.xlsx");
Console.WriteLine("Excel file created successfully.");
}
}
}
' Import IronXL namespaces
Imports IronXL
Imports System
Namespace MyExcelApp
Public Class ExcelFileService
''' <summary>
''' Creates an Excel file with example data and saves it.
''' </summary>
Public Sub CreateExcelFile()
' Initialize a new workbook
Dim workbook = WorkBook.Create(ExcelFileFormat.XLSX)
' Add a new worksheet to the workbook
Dim sheet = workbook.CreateWorkSheet("SampleSheet")
' Set values in cells and apply basic formatting
sheet("A1").Value = "Hello"
sheet("A2").Value = "World"
sheet("B1").Value = DateTime.Now
' Apply some formatting
sheet("A1:A2").Style.BackgroundColor = "#FFD700" ' Gold background
sheet("B1").Style.Font.Bold = True
' Save the workbook using a save service
Dim saveService As New SaveService()
saveService.Save(workbook, "SampleExcel.xlsx")
Console.WriteLine("Excel file created successfully.")
End Sub
End Class
End Namespace
C# Code for Excel File Reading
// Import IronXL namespaces
using IronXL;
using System;
namespace MyExcelApp
{
public class ExcelFileService
{
/// <summary>
/// Reads an existing Excel file and displays data from it.
/// </summary>
public void ReadExcelFile()
{
// Load an existing workbook
var workbook = WorkBook.Load("SampleExcel.xlsx");
// Access a worksheet by name
var sheet = workbook.GetWorkSheet("SampleSheet");
// Read values from the worksheet
var value1 = sheet["A1"].StringValue;
var value2 = sheet["A2"].StringValue;
var dateValue = sheet["B1"].DateTimeValue;
Console.WriteLine($"Read values: A1 - {value1}, A2 - {value2}, B1 - {dateValue}");
}
}
}
// Import IronXL namespaces
using IronXL;
using System;
namespace MyExcelApp
{
public class ExcelFileService
{
/// <summary>
/// Reads an existing Excel file and displays data from it.
/// </summary>
public void ReadExcelFile()
{
// Load an existing workbook
var workbook = WorkBook.Load("SampleExcel.xlsx");
// Access a worksheet by name
var sheet = workbook.GetWorkSheet("SampleSheet");
// Read values from the worksheet
var value1 = sheet["A1"].StringValue;
var value2 = sheet["A2"].StringValue;
var dateValue = sheet["B1"].DateTimeValue;
Console.WriteLine($"Read values: A1 - {value1}, A2 - {value2}, B1 - {dateValue}");
}
}
}
' Import IronXL namespaces
Imports IronXL
Imports System
Namespace MyExcelApp
Public Class ExcelFileService
''' <summary>
''' Reads an existing Excel file and displays data from it.
''' </summary>
Public Sub ReadExcelFile()
' Load an existing workbook
Dim workbook = WorkBook.Load("SampleExcel.xlsx")
' Access a worksheet by name
Dim sheet = workbook.GetWorkSheet("SampleSheet")
' Read values from the worksheet
Dim value1 = sheet("A1").StringValue
Dim value2 = sheet("A2").StringValue
Dim dateValue = sheet("B1").DateTimeValue
Console.WriteLine($"Read values: A1 - {value1}, A2 - {value2}, B1 - {dateValue}")
End Sub
End Class
End Namespace
Save Service Implementation
using System;
using IronXL;
namespace MyExcelApp
{
public class SaveService
{
/// <summary>
/// Saves the given workbook to a specified file name.
/// </summary>
/// <param name="workbook">The workbook to be saved.</param>
/// <param name="fileName">The name of the file.</param>
public void Save(WorkBook workbook, string fileName)
{
try
{
workbook.SaveAs(fileName);
Console.WriteLine($"Workbook saved as {fileName}");
}
catch (Exception ex)
{
Console.WriteLine($"An error occurred while saving the file: {ex.Message}");
}
}
}
}
using System;
using IronXL;
namespace MyExcelApp
{
public class SaveService
{
/// <summary>
/// Saves the given workbook to a specified file name.
/// </summary>
/// <param name="workbook">The workbook to be saved.</param>
/// <param name="fileName">The name of the file.</param>
public void Save(WorkBook workbook, string fileName)
{
try
{
workbook.SaveAs(fileName);
Console.WriteLine($"Workbook saved as {fileName}");
}
catch (Exception ex)
{
Console.WriteLine($"An error occurred while saving the file: {ex.Message}");
}
}
}
}
Imports System
Imports IronXL
Namespace MyExcelApp
Public Class SaveService
''' <summary>
''' Saves the given workbook to a specified file name.
''' </summary>
''' <param name="workbook">The workbook to be saved.</param>
''' <param name="fileName">The name of the file.</param>
Public Sub Save(ByVal workbook As WorkBook, ByVal fileName As String)
Try
workbook.SaveAs(fileName)
Console.WriteLine($"Workbook saved as {fileName}")
Catch ex As Exception
Console.WriteLine($"An error occurred while saving the file: {ex.Message}")
End Try
End Sub
End Class
End Namespace
Further Reading: Create, Read and Edit Excel Files in .NET MAUI