Test in a live environment
Test in production without watermarks.
Works wherever you need it to.
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#.
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
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.
For editing Excel files, add the reference IronXL
to your project and import the library by using IronXL
.
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
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
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
The above code will display the following output and take inputs from user:
Console Application UI with user input
Values changed from B4 to B9 in ExcelSheet, we can see:
The new value is filled from B4 to B9
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
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.
Documentation is provided for IronXL, featuring all namespaces, feature sets, methods fields, classes, and enums.
API Reference9 .NET API products for your office documents