USING IRONXL

Work with Excel in .NET Core

.NET Core Excel Overview

In this modern era, we need a better way to work with Excel Spreadsheets in our .NET Core applications. In the following tutorial, we will learn how to access spreadsheets in .NET Core Excel projects and modify their values using C#.


Step 1

1. Download IronXL Library

For an easy way to work with Excel files in .NET Core, try IronXL. Download IronXL DLL or install with NuGet for free use in development projects.

# Install IronXL using the .NET CLI
dotnet add package IronXL.Excel
# Install IronXL using the .NET CLI
dotnet add package IronXL.Excel
SHELL

How to Tutorial

2. .NET Core Excel Editing Project

Now that you've downloaded IronXL, let's get started. Load an Excel file in the project and access the WorkSheet where data needs to be edited and changes made.


3. Edit Specific Cell Value

For editing Excel files, add the reference IronXL to your project and import the library by using IronXL.

3.1. Load a Sample File

In the following case, our Excel file name is sample.xlsx and it exists in the bin> Debug> netcoreapp3.1 folder of the project. We will use this code to edit the value new value in cell A1 of sample.xlsx.

// Anchor: Load a sample file
using IronXL;

static void Main(string[] args)
{
    // Load the Excel workbook
    WorkBook wb = WorkBook.Load("sample.xlsx");

    // Get the first worksheet named "Sheet1"
    WorkSheet ws = wb.GetWorkSheet("Sheet1");

    // Access cell A1 and set its value to "new value"
    ws["A1"].Value = "new value";

    // Save the changes to the Excel workbook
    wb.SaveAs("sample.xlsx");
}
// Anchor: Load a sample file
using IronXL;

static void Main(string[] args)
{
    // Load the Excel workbook
    WorkBook wb = WorkBook.Load("sample.xlsx");

    // Get the first worksheet named "Sheet1"
    WorkSheet ws = wb.GetWorkSheet("Sheet1");

    // Access cell A1 and set its value to "new value"
    ws["A1"].Value = "new value";

    // Save the changes to the Excel workbook
    wb.SaveAs("sample.xlsx");
}
' Anchor: Load a sample file
Imports IronXL

Shared Sub Main(ByVal args() As String)
	' Load the Excel workbook
	Dim wb As WorkBook = WorkBook.Load("sample.xlsx")

	' Get the first worksheet named "Sheet1"
	Dim ws As WorkSheet = wb.GetWorkSheet("Sheet1")

	' Access cell A1 and set its value to "new value"
	ws("A1").Value = "new value"

	' Save the changes to the Excel workbook
	wb.SaveAs("sample.xlsx")
End Sub
$vbLabelText   $csharpLabel

4. Assign Value to Multiple Cells

It is very easy to edit multiple cells and assign static values at a time by using a colon :. Its left side indicates the starting cell and the right side indicates the last cell of a specific column.

sheet[From:To]

This will edit new value from cell A1 to A9 of column A.

// Anchor: Assign Value to Multiple Cells
using IronXL;

static void Main(string[] args)
{
    WorkBook wb = WorkBook.Load("sample.xlsx");
    WorkSheet ws = wb.GetWorkSheet("Sheet1");

    // Set the value "new value" for cells from A1 to A9
    ws["A1:A9"].Value = "new value";

    wb.SaveAs("sample.xlsx");
}
// Anchor: Assign Value to Multiple Cells
using IronXL;

static void Main(string[] args)
{
    WorkBook wb = WorkBook.Load("sample.xlsx");
    WorkSheet ws = wb.GetWorkSheet("Sheet1");

    // Set the value "new value" for cells from A1 to A9
    ws["A1:A9"].Value = "new value";

    wb.SaveAs("sample.xlsx");
}
' Anchor: Assign Value to Multiple Cells
Imports IronXL

Shared Sub Main(ByVal args() As String)
	Dim wb As WorkBook = WorkBook.Load("sample.xlsx")
	Dim ws As WorkSheet = wb.GetWorkSheet("Sheet1")

	' Set the value "new value" for cells from A1 to A9
	ws("A1:A9").Value = "new value"

	wb.SaveAs("sample.xlsx")
End Sub
$vbLabelText   $csharpLabel

5. Edit Cells with User Inputs

Here is an alternative case where we can take the values from the users and edit the Excel file.

// Anchor: Edit Cells with User Inputs
using System;
using IronXL;

static void Main(string[] args)
{
    string _from, _to, newValue;

    // Capture user inputs
    Console.Write("Enter Starting Cell: ");
    _from = Console.ReadLine();

    Console.Write("Enter Last Cell: ");
    _to = Console.ReadLine();

    Console.Write("Enter value: ");
    newValue = Console.ReadLine();

    // Load the Excel workbook and access the worksheet
    WorkBook wb = WorkBook.Load("sample.xlsx");
    WorkSheet ws = wb.GetWorkSheet("Sheet1");

    // Assign the user-entered value to the specified cell range
    ws[_from + ":" + _to].Value = newValue;

    // Save changes to the workbook
    wb.SaveAs("sample.xlsx");

    Console.WriteLine("Successfully Changed...!");
    Console.ReadKey();
}
// Anchor: Edit Cells with User Inputs
using System;
using IronXL;

static void Main(string[] args)
{
    string _from, _to, newValue;

    // Capture user inputs
    Console.Write("Enter Starting Cell: ");
    _from = Console.ReadLine();

    Console.Write("Enter Last Cell: ");
    _to = Console.ReadLine();

    Console.Write("Enter value: ");
    newValue = Console.ReadLine();

    // Load the Excel workbook and access the worksheet
    WorkBook wb = WorkBook.Load("sample.xlsx");
    WorkSheet ws = wb.GetWorkSheet("Sheet1");

    // Assign the user-entered value to the specified cell range
    ws[_from + ":" + _to].Value = newValue;

    // Save changes to the workbook
    wb.SaveAs("sample.xlsx");

    Console.WriteLine("Successfully Changed...!");
    Console.ReadKey();
}
' Anchor: Edit Cells with User Inputs
Imports System
Imports IronXL

Shared Sub Main(ByVal args() As String)
	Dim _from, _to, newValue As String

	' Capture user inputs
	Console.Write("Enter Starting Cell: ")
	_from = Console.ReadLine()

	Console.Write("Enter Last Cell: ")
	_to = Console.ReadLine()

	Console.Write("Enter value: ")
	newValue = Console.ReadLine()

	' Load the Excel workbook and access the worksheet
	Dim wb As WorkBook = WorkBook.Load("sample.xlsx")
	Dim ws As WorkSheet = wb.GetWorkSheet("Sheet1")

	' Assign the user-entered value to the specified cell range
	ws(_from & ":" & _to).Value = newValue

	' Save changes to the workbook
	wb.SaveAs("sample.xlsx")

	Console.WriteLine("Successfully Changed...!")
	Console.ReadKey()
End Sub
$vbLabelText   $csharpLabel

The above code will display a console for user inputs and then update the specified Excel cells with the entered value.

Work with Excel in .NET Core, Figure 1: Console Application UI with user input Console Application UI with user input

Values changed from B4 to B9 in ExcelSheet, as can be seen:

Work with Excel in .NET Core, Figure 2: The new value is filled from B4 to B9 The new value is filled from B4 to B9


6. Edit Multiple Cells with Static Value

It is very easy to edit multiple cells and assign dynamic values. Let's see the following example:

// Anchor: Edit Multiple Cells with Static Value
using IronXL;

static void Main(string[] args)
{
    WorkBook wb = WorkBook.Load("sample.xlsx");
    WorkSheet ws = wb.GetWorkSheet("Sheet1");

    // Ensure 'from' and 'to' are defined for the intended cell range
    int from = 1;
    int to = 9;

    // Iterate over a range of cells and update them with dynamic values
    for (int i = from; i <= to; i++)
    {
        ws["A" + i].Value = "Value" + i;
    }

    // Save the changes to the Excel file
    wb.SaveAs("sample.xlsx");
}
// Anchor: Edit Multiple Cells with Static Value
using IronXL;

static void Main(string[] args)
{
    WorkBook wb = WorkBook.Load("sample.xlsx");
    WorkSheet ws = wb.GetWorkSheet("Sheet1");

    // Ensure 'from' and 'to' are defined for the intended cell range
    int from = 1;
    int to = 9;

    // Iterate over a range of cells and update them with dynamic values
    for (int i = from; i <= to; i++)
    {
        ws["A" + i].Value = "Value" + i;
    }

    // Save the changes to the Excel file
    wb.SaveAs("sample.xlsx");
}
' Anchor: Edit Multiple Cells with Static Value
Imports IronXL

Shared Sub Main(ByVal args() As String)
	Dim wb As WorkBook = WorkBook.Load("sample.xlsx")
	Dim ws As WorkSheet = wb.GetWorkSheet("Sheet1")

	' Ensure 'from' and 'to' are defined for the intended cell range
	Dim from As Integer = 1
	Dim [to] As Integer = 9

	' Iterate over a range of cells and update them with dynamic values
	For i As Integer = From To [to]
		ws("A" & i).Value = "Value" & i
	Next i

	' Save the changes to the Excel file
	wb.SaveAs("sample.xlsx")
End Sub
$vbLabelText   $csharpLabel

7. Read Excel Files In-Depth Tutorial

Dive in with further details and multiple projects and code examples if you want to learn more about how to Read Excel Files C# with this tutorial.


Tutorial Quick Access

Documentation related to Tutorial Quick Access

Investigate the API Reference

Documentation is provided for IronXL, featuring all namespaces, feature sets, methods fields, classes, and enums.

API Reference

Frequently Asked Questions

What is IronXL?

IronXL is a library that allows developers to work with Excel files in .NET Core applications. It provides tools to load, edit, and save Excel spreadsheets programmatically using C#.

How can I download the IronXL library?

You can download the IronXL library by visiting the IronXL website and downloading the DLL file, or you can install it using NuGet with the command: dotnet add package IronXL.Excel.

How do I load an Excel file using IronXL?

To load an Excel file using IronXL, use the WorkBook.Load method. For example, WorkBook wb = WorkBook.Load('sample.xlsx'); will load the Excel workbook named 'sample.xlsx'.

Can IronXL edit multiple cells at once?

Yes, IronXL can edit multiple cells simultaneously. You can assign a value to a range of cells using the syntax ws['A1:A9'].Value = 'new value'; where ws is a WorkSheet object.

How can I edit Excel cells with user inputs in IronXL?

IronXL allows you to edit Excel cells using user inputs by capturing input through the console, then using these inputs to define the cell range and value to be updated in the Excel spreadsheet.

What programming language is used with IronXL for Excel manipulation?

IronXL utilizes C# for programmatically manipulating Excel files within .NET Core applications.

Is there a tutorial available for reading Excel files using IronXL?

Yes, there is an in-depth tutorial available on how to read Excel files using C# with IronXL. Additional resources and example projects are also provided on the IronXL website.

What versions of .NET Core does IronXL support?

IronXL supports multiple versions of .NET Core. Specific compatibility details are available in the documentation and on the IronXL website.

Where can I find the API reference for IronXL?

The API reference for IronXL is available online, detailing all namespaces, feature sets, methods, fields, classes, and enums. You can access it through the IronXL website.

Regan Pun
Software Engineer
Regan graduated from the University of Reading, with a BA in Electronic Engineering. Before joining Iron Software, his previous job roles had him laser-focused on single tasks; and what he most enjoys at Iron Software is the spectrum of work he gets to undertake, whether it’s adding value to sales, technical support, product development or marketing. He enjoys understanding the way developers are using the Iron Software library, and using that knowledge to continually improve documentation and develop the products.
< PREVIOUS
Generate Excel Files in C#