Test in a live environment
Test in production without watermarks.
Works wherever you need it to.
Renaming Excel files programmatically is a common task in various applications. Whether you're organizing files, automating tasks, or managing data, having the ability to rename Excel files through code can be highly beneficial. In this article, we'll explore how to rename Excel files using IronXL library from Iron Software.
IronXL is a powerful C# Excel library developed by Iron Software. It allows you to work with Excel documents in your .NET projects without the need for Microsoft Office or Excel Interop.
Cross-Platform Support: IronXL is designed for .NET 8, 7, 6, Core, Framework, and Azure. Whether you’re building console applications, web apps, or desktop software, IronXL has you covered.
using IronXL;
namespace RenameExcelSheets;
public class Program
{
public static void Main()
{
Console.WriteLine("Rename Excel Sheets Using IronXL");
// Load an existing Excel file to excel workbook object
WorkBook workBook = WorkBook.Load("sample.xlsx"); // sample excel file
// Select specified worksheet
WorkSheet workSheet = workBook.WorkSheets [0];
// Read a cell value from same workbook
int cellValue = workSheet ["A2"].IntValue;
// Iterate through a range of cells
foreach (var cell in workSheet ["A2:A10"])
{
Console.WriteLine($"Cell {cell.AddressString} has value '{cell.Text}'");
}
// Calculate aggregate values
decimal sum = workSheet ["A2:A10"].Sum();
decimal max = workSheet ["A2:A10"].Max(c => c.DecimalValue);
workBook.SaveAs("sampleResult.xlsx"); // save as new workbook
}
}
using IronXL;
namespace RenameExcelSheets;
public class Program
{
public static void Main()
{
Console.WriteLine("Rename Excel Sheets Using IronXL");
// Load an existing Excel file to excel workbook object
WorkBook workBook = WorkBook.Load("sample.xlsx"); // sample excel file
// Select specified worksheet
WorkSheet workSheet = workBook.WorkSheets [0];
// Read a cell value from same workbook
int cellValue = workSheet ["A2"].IntValue;
// Iterate through a range of cells
foreach (var cell in workSheet ["A2:A10"])
{
Console.WriteLine($"Cell {cell.AddressString} has value '{cell.Text}'");
}
// Calculate aggregate values
decimal sum = workSheet ["A2:A10"].Sum();
decimal max = workSheet ["A2:A10"].Max(c => c.DecimalValue);
workBook.SaveAs("sampleResult.xlsx"); // save as new workbook
}
}
Imports IronXL
Namespace RenameExcelSheets
Public Class Program
Public Shared Sub Main()
Console.WriteLine("Rename Excel Sheets Using IronXL")
' Load an existing Excel file to excel workbook object
Dim workBook As WorkBook = WorkBook.Load("sample.xlsx") ' sample excel file
' Select specified worksheet
Dim workSheet As WorkSheet = workBook.WorkSheets (0)
' Read a cell value from same workbook
Dim cellValue As Integer = workSheet ("A2").IntValue
' Iterate through a range of cells
For Each cell In workSheet ("A2:A10")
Console.WriteLine($"Cell {cell.AddressString} has value '{cell.Text}'")
Next cell
' Calculate aggregate values
Dim sum As Decimal = workSheet ("A2:A10").Sum()
Dim max As Decimal = workSheet ("A2:A10").Max(Function(c) c.DecimalValue)
workBook.SaveAs("sampleResult.xlsx") ' save as new workbook
End Sub
End Class
End Namespace
Remember, IronXL is trusted by millions of engineers worldwide for its accuracy, ease of use, and speed. If you’re working with Excel files in C# or VB.NET, IronXL is your go-to library!
Before diving into the coding part, make sure you have the necessary tools installed:
To demonstrate a real-world example of renaming an Excel file, let us write a program to take a folder containing all the files to rename and use IronXL to rename all the files, then store them in the output folder.
Open Visual Studio and create a new project for the demo. Select Console app from the below template.
Provide names to the Project and path to store the files.
Select the required .NET version.
IronXL library can be installed from Visual Studio Package manager as below.
Or can be installed from NuGet Package Manager with command.
dotnet add package IronXL.Excel --version 2024.4.4
dotnet add package IronXL.Excel --version 2024.4.4
'INSTANT VB TODO TASK: The following line uses invalid syntax:
'dotnet add package IronXL.Excel --version 2024.4.4
Once installed the project is ready to start coding for renaming Excel worksheets.
Below is the program to rename all the files and worksheets in a directory for business applications.
Input:
using IronXL;
namespace RenameExcelSheets;
public class Program
{
public static void Main()
{
Console.WriteLine("Demo Rename Excel Sheets Using IronXL");
Console.WriteLine("Enter Folder where Excel Files are present to rename to FinancialReport2024");
var folderPath = Console.ReadLine();
if (string.IsNullOrEmpty(folderPath)) // check for empty string
{
throw new AggregateException("Path is empty");
}
if (Directory.Exists(folderPath) == false)
{
throw new AggregateException("Path is Wrong");
}
var files = Directory.GetFiles(folderPath);
var outputPath = Path.Combine(folderPath, "output");
var index = 0;
foreach (var file in files)
{
// Load an existing Excel file
WorkBook workBook = WorkBook.Load(file);
// Select the first worksheet (index 0)
WorkSheet workSheet = workBook.WorkSheets [0];
// Rename the worksheet
workSheet.Name = "FinancialReport2024"; // change the name property
// Save the modified workbook
workBook.SaveAs(Path.Join(outputPath, $"FinancialReport2024_{index++}.xlsx"));
}
}
}
using IronXL;
namespace RenameExcelSheets;
public class Program
{
public static void Main()
{
Console.WriteLine("Demo Rename Excel Sheets Using IronXL");
Console.WriteLine("Enter Folder where Excel Files are present to rename to FinancialReport2024");
var folderPath = Console.ReadLine();
if (string.IsNullOrEmpty(folderPath)) // check for empty string
{
throw new AggregateException("Path is empty");
}
if (Directory.Exists(folderPath) == false)
{
throw new AggregateException("Path is Wrong");
}
var files = Directory.GetFiles(folderPath);
var outputPath = Path.Combine(folderPath, "output");
var index = 0;
foreach (var file in files)
{
// Load an existing Excel file
WorkBook workBook = WorkBook.Load(file);
// Select the first worksheet (index 0)
WorkSheet workSheet = workBook.WorkSheets [0];
// Rename the worksheet
workSheet.Name = "FinancialReport2024"; // change the name property
// Save the modified workbook
workBook.SaveAs(Path.Join(outputPath, $"FinancialReport2024_{index++}.xlsx"));
}
}
}
Imports IronXL
Namespace RenameExcelSheets
Public Class Program
Public Shared Sub Main()
Console.WriteLine("Demo Rename Excel Sheets Using IronXL")
Console.WriteLine("Enter Folder where Excel Files are present to rename to FinancialReport2024")
Dim folderPath = Console.ReadLine()
If String.IsNullOrEmpty(folderPath) Then ' check for empty string
Throw New AggregateException("Path is empty")
End If
If Directory.Exists(folderPath) = False Then
Throw New AggregateException("Path is Wrong")
End If
Dim files = Directory.GetFiles(folderPath)
Dim outputPath = Path.Combine(folderPath, "output")
Dim index = 0
For Each file In files
' Load an existing Excel file
Dim workBook As WorkBook = WorkBook.Load(file)
' Select the first worksheet (index 0)
Dim workSheet As WorkSheet = workBook.WorkSheets (0)
' Rename the worksheet
workSheet.Name = "FinancialReport2024" ' change the name property
' Save the modified workbook
'INSTANT VB WARNING: An assignment within expression was extracted from the following statement:
'ORIGINAL LINE: workBook.SaveAs(Path.Join(outputPath, string.Format("FinancialReport2024_{0}.xlsx", index++)));
workBook.SaveAs(Path.Join(outputPath, $"FinancialReport2024_{index}.xlsx"))
index += 1
Next file
End Sub
End Class
End Namespace
Output
In the below output, you can see all the 3 files are renamed and the Excel sheet inside them is also renamed to FinancialReport2024.
IronXL is an enterprise library that works with a license agreement. More on license can be found here. The license key needs to be placed in the appsettings.json file here.
{
"IronXL.License.LicenseKey" : "IRONXL-MYLICENSE-KEY-1EF01"
}
{
"IronXL.License.LicenseKey" : "IRONXL-MYLICENSE-KEY-1EF01"
}
If True Then
"IronXL.License.LicenseKey" : "IRONXL-MYLICENSE-KEY-1EF01"
End If
Renaming Excel files using C# is a straightforward process. By leveraging the IronXL library from Iron Software, you can easily rename Excel files within your C# applications. This library is a handy tool for developers for all Excel sheet operations, be it reading, writing, or managing.
Now that you've learned how to rename Excel files programmatically, you can incorporate this feature into your C# projects to streamline file management tasks and improve automation capabilities.
9 .NET API products for your office documents