在生产环境中测试,无水印。
随时随地满足您的需求。
获得30天的全功能产品。
几分钟内就能启动并运行。
在您的产品试用期间,全面访问我们的支持工程团队。
以编程方式重命名 Excel 文件是各种应用程序中的常见任务。 无论是整理文件、自动执行任务还是管理数据,拥有通过代码重命名 Excel 文件的能力都会让您受益匪浅。 在这篇文章中,我们将探讨如何使用IronXL库重命名 Excel 文件,该库来自Iron Software。
创建一个 Visual Studio 项目来重命名 Excel 表单。
从Iron Software安装IronXL库。
IronXL 是由 Iron Software 开发的强大 C# Excel 库。 它允许您在 .NET 项目中使用 Excel 文档,而无需 Microsoft Office 或 Excel Interop。
读取、编辑和创建 Excel 文件:IronXL 使您能够直接从 C# 或 VB.NET 代码读取、生成和编辑 Excel 电子表格文件(包括 XLSX、XLS、XLSM、XLTX、CSV 和 TSV 格式)。
无需Office互操作:您无需安装Microsoft Office或处理Office互操作的复杂性。IronXL提供了一种无忧的体验。
跨平台支持:IronXL 旨在为 .NET 8、7、6、Core、Framework 和 Azure 提供支持。 无论您是在构建控制台应用程序、网络应用程序还是桌面软件,IronXL 都能满足您的需求。
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
        }
    }IRON VB CONVERTER ERROR developers@ironsoftware.com无需麻烦的Excel功能:IronXL使您能够轻松创建、加载、保存和操作电子表格。 无论是处理元数据、权限、公式还是样式,IronXL 都能简化过程。
请记住,IronXL 因其准确性、易用性和快速性而受到全球数百万工程师的信赖。 如果您使用 C# 或 VB.NET 处理 Excel 文件,IronXL 是您的首选库!
在开始编码之前,请确保您已经安装了必要的工具:
Visual Studio:安装 Visual Studio 或其他首选的 C# IDE。
Microsoft Excel:确保您的系统上已安装 Microsoft Excel。
为了演示重命名 Excel 文件的实际例子,让我们编写一个程序,获取包含所有要重命名文件的文件夹,并使用 IronXL 对所有文件进行重命名,然后将它们存储到输出文件夹中。
打开 Visual Studio,为演示创建一个新项目。 从以下模板中选择控制台应用程序。

提供项目名称和文件存储路径。

选择所需的 .NET 版本。

IronXL 库可以按以下方式从 Visual Studio 包管理器中安装。

或可使用命令从 NuGet 软件包管理器中安装。
dotnet add package IronXL.Excel --version 2024.4.4dotnet 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
安装完成后,该项目就可以开始编码,对 Excel 工作表进行重命名。
以下是重命名商业应用程序目录中所有文件和工作表的程序。
输入:
!如何在 C# 中重命名 Excel 工作表:图 6 - 重命名的 Excel 工作表输入示例
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以文件夹路径重命名所有文件及其工作表
检查文件夹路径是否为空字符串
检查文件夹路径是否有效
获取扩展名为 xlsx 的文件夹中的所有文件
遍历文件并从 IronXL 加载到 WorkBook 对象,重命名工作表名称属性。
将文件保存到输出文件夹
输出
在下面的输出中,您可以看到所有 3 个文件都已重命名,其中的 Excel 表也重命名为 FinancialReport2024。

IronXL 是一个企业级库,与许可协议配合使用。 有关许可的更多信息可以在这里找到。 许可证密钥需要放在此处的 appsettings.json 文件中。
{
  "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使用 C# 重命名 Excel 文件是一个简单明了的过程。 通过利用IronXL库来自Iron Software,您可以在 C# 应用程序中轻松重命名 Excel 文件。 该库是开发人员进行所有 Excel 表操作(无论是读取、写入还是管理)的便捷工具。
现在,您已经学会了如何以编程方式重命名 Excel 文件,您可以将此功能纳入 C# 项目,以简化文件管理任务并提高自动化能力。