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-Package IronXL.Excel

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.

/**
Load WorkSheet
anchor-load-a-sample-file
**/
using IronXL;
static void Main(string[] args)
{          
    WorkBook wb = WorkBook.Load("sample.xlsx"); //load Excel file 
    WorkSheet ws = wb.GetWorkSheet("Sheet1"); //Get sheet1 of sample.xlsx
    ws["A1"].Value = "new value"; //access A1 cell and edit the value
    wb.SaveAs("sample.xlsx");   //save changes        
} 
/**
Load WorkSheet
anchor-load-a-sample-file
**/
using IronXL;
static void Main(string[] args)
{          
    WorkBook wb = WorkBook.Load("sample.xlsx"); //load Excel file 
    WorkSheet ws = wb.GetWorkSheet("Sheet1"); //Get sheet1 of sample.xlsx
    ws["A1"].Value = "new value"; //access A1 cell and edit the value
    wb.SaveAs("sample.xlsx");   //save changes        
} 
'''
'''Load WorkSheet
'''anchor-load-a-sample-file
'''*
Imports IronXL
Shared Sub Main(ByVal args() As String)
	Dim wb As WorkBook = WorkBook.Load("sample.xlsx") 'load Excel file
	Dim ws As WorkSheet = wb.GetWorkSheet("Sheet1") 'Get sheet1 of sample.xlsx
	ws("A1").Value = "new value" 'access A1 cell and edit the value
	wb.SaveAs("sample.xlsx") 'save changes
End Sub
VB   C#

4. Assign Value to Multiple Cells

It is very easy to edit multiple cells and assign static values at a time by using (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.

/**
Assign Value Multi Cells
anchor-assign-value-to-multiple-cells
**/
using IronXL;
static void Main(string[] args)
{
    WorkBook wb = WorkBook.Load("sample.xlsx");
    WorkSheet ws = wb.GetWorkSheet("Sheet1");
    ws["A1:A9"].Value = "new value";
    wb.SaveAs("sample.xlsx");
}
/**
Assign Value Multi Cells
anchor-assign-value-to-multiple-cells
**/
using IronXL;
static void Main(string[] args)
{
    WorkBook wb = WorkBook.Load("sample.xlsx");
    WorkSheet ws = wb.GetWorkSheet("Sheet1");
    ws["A1:A9"].Value = "new value";
    wb.SaveAs("sample.xlsx");
}
'''
'''Assign Value Multi Cells
'''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")
	ws("A1:A9").Value = "new value"
	wb.SaveAs("sample.xlsx")
End Sub
VB   C#

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.

/**
Edit Cells User Input
anchor-edit-cells-with-user-inputs
**/
using IronXL;
static void Main(string[] args)
{
    string _from, _to, newValue ;

    Console.Write("Enter Starting Cell :");
    _from = Console.ReadLine();

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

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

    WorkBook wb = WorkBook.Load("sample.xlsx");
    WorkSheet ws = wb.GetWorkSheet("Sheet1");
    ws[_from + ":" + _to].Value = newValue;
    wb.SaveAs("sample.xlsx"); 
    Console.WriteLine("Successfully Changed...!");
    Console.ReadKey();
}
/**
Edit Cells User Input
anchor-edit-cells-with-user-inputs
**/
using IronXL;
static void Main(string[] args)
{
    string _from, _to, newValue ;

    Console.Write("Enter Starting Cell :");
    _from = Console.ReadLine();

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

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

    WorkBook wb = WorkBook.Load("sample.xlsx");
    WorkSheet ws = wb.GetWorkSheet("Sheet1");
    ws[_from + ":" + _to].Value = newValue;
    wb.SaveAs("sample.xlsx"); 
    Console.WriteLine("Successfully Changed...!");
    Console.ReadKey();
}
'''
'''Edit Cells User Input
'''anchor-edit-cells-with-user-inputs
'''*
Imports IronXL
Shared Sub Main(ByVal args() As String)
	Dim _from, _to, newValue As String

	Console.Write("Enter Starting Cell :")
	_from = Console.ReadLine()

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

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

	Dim wb As WorkBook = WorkBook.Load("sample.xlsx")
	Dim ws As WorkSheet = wb.GetWorkSheet("Sheet1")
	ws(_from & ":" & _to).Value = newValue
	wb.SaveAs("sample.xlsx")
	Console.WriteLine("Successfully Changed...!")
	Console.ReadKey()
End Sub
VB   C#

The above code will display the following output and take inputs from user:

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, we can see:

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:

/**
Edit Multi Cells Static Value
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");
    for (int i = From; i <= To; i++) //Set cell range of column A to be edit.
    {
        ws["A" + i].Value = "Value"+i;
    }
    wb.SaveAs("sample.xlsx");  
}
/**
Edit Multi Cells Static Value
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");
    for (int i = From; i <= To; i++) //Set cell range of column A to be edit.
    {
        ws["A" + i].Value = "Value"+i;
    }
    wb.SaveAs("sample.xlsx");  
}
'''
'''Edit Multi Cells Static Value
'''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")
	For i As Integer = From To [To] 'Set cell range of column A to be edit.
		ws("A" & i).Value = "Value" & i
	Next i
	wb.SaveAs("sample.xlsx")
End Sub
VB   C#

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

Investigate the API Reference

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

API Reference