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, you will learn how to read and write Excel files using IronXL in C# with Visual Studio 2022. The tutorial starts by ensuring you have the latest version of IronXL installed via the NuGet package manager. After setting up, the process begins by including the IronXL namespace in your project. You will learn how to load an Excel file using the workbook.load
function and assign the default worksheet to a variable. The tutorial guides you through selecting a range of cells, iterating over them, and printing their values to the console, while also calculating a running total of numeric data. The next step is to validate if the sum of the range's values matches a specific cell's value. If it does, confirmation is printed to the console. You will then modify a cell value (B2) by setting it to 11.54 and save the changes to a new Excel file named 'modified.xlsx'. The console output will display the cell contents and validation results, and the tutorial concludes by showing the modified Excel file with updated data. This tutorial is an excellent resource for those looking to enhance their skills in manipulating Excel files programmatically using C# and IronXL.
// Ensure you have installed the IronXL library via NuGet package manager
using IronXL;
using System;
class ExcelManipulation
{
static void Main()
{
// Load the existing Excel file
var workbook = WorkBook.Load("path_to_your_excel_file.xlsx");
// Get the first worksheet
var sheet = workbook.WorkSheets.First();
// Select a range of cells
var range = sheet["A1:A10"];
double total = 0;
// Iterate through the range and print values
foreach (var cell in range)
{
Console.WriteLine(cell.Value);
// Add cell value to total if it's numeric
if (double.TryParse(cell.Value.ToString(), out double number))
{
total += number;
}
}
// Validate if the sum of range matches a specific cell's value
var specificCell = sheet["B1"];
if (total == Convert.ToDouble(specificCell.Value))
{
Console.WriteLine("The sum matches the value in B1.");
}
else
{
Console.WriteLine("The sum does not match the value in B1.");
}
// Modify cell B2 value and save the workbook
sheet["B2"].Value = 11.54;
// Save the workbook to a new file
workbook.SaveAs("modified.xlsx");
Console.WriteLine("Excel file has been modified and saved as 'modified.xlsx'.");
}
}
// Ensure you have installed the IronXL library via NuGet package manager
using IronXL;
using System;
class ExcelManipulation
{
static void Main()
{
// Load the existing Excel file
var workbook = WorkBook.Load("path_to_your_excel_file.xlsx");
// Get the first worksheet
var sheet = workbook.WorkSheets.First();
// Select a range of cells
var range = sheet["A1:A10"];
double total = 0;
// Iterate through the range and print values
foreach (var cell in range)
{
Console.WriteLine(cell.Value);
// Add cell value to total if it's numeric
if (double.TryParse(cell.Value.ToString(), out double number))
{
total += number;
}
}
// Validate if the sum of range matches a specific cell's value
var specificCell = sheet["B1"];
if (total == Convert.ToDouble(specificCell.Value))
{
Console.WriteLine("The sum matches the value in B1.");
}
else
{
Console.WriteLine("The sum does not match the value in B1.");
}
// Modify cell B2 value and save the workbook
sheet["B2"].Value = 11.54;
// Save the workbook to a new file
workbook.SaveAs("modified.xlsx");
Console.WriteLine("Excel file has been modified and saved as 'modified.xlsx'.");
}
}
' Ensure you have installed the IronXL library via NuGet package manager
Imports IronXL
Imports System
Friend Class ExcelManipulation
Shared Sub Main()
' Load the existing Excel file
Dim workbook = WorkBook.Load("path_to_your_excel_file.xlsx")
' Get the first worksheet
Dim sheet = workbook.WorkSheets.First()
' Select a range of cells
Dim range = sheet("A1:A10")
Dim total As Double = 0
' Iterate through the range and print values
For Each cell In range
Console.WriteLine(cell.Value)
' Add cell value to total if it's numeric
Dim number As Double
If Double.TryParse(cell.Value.ToString(), number) Then
total += number
End If
Next cell
' Validate if the sum of range matches a specific cell's value
Dim specificCell = sheet("B1")
If total = Convert.ToDouble(specificCell.Value) Then
Console.WriteLine("The sum matches the value in B1.")
Else
Console.WriteLine("The sum does not match the value in B1.")
End If
' Modify cell B2 value and save the workbook
sheet("B2").Value = 11.54
' Save the workbook to a new file
workbook.SaveAs("modified.xlsx")
Console.WriteLine("Excel file has been modified and saved as 'modified.xlsx'.")
End Sub
End Class
Further Reading: C# Write to Excel [Without Using Interop] Code Example Tutorial