使用 IRONXL 如何在 C# 中重命名 Excel 工作表 Jordi Bardia 更新日期:6月 22, 2025 Download IronXL NuGet 下載 DLL 下載 Start Free Trial Copy for LLMs Copy for LLMs Copy page as Markdown for LLMs Open in ChatGPT Ask ChatGPT about this page Open in Gemini Ask Gemini about this page Open in Grok Ask Grok about this page Open in Perplexity Ask Perplexity about this page Share Share on Facebook Share on X (Twitter) Share on LinkedIn Copy URL Email article 以程式方式重新命名 Excel 檔案是各種應用程式中的常見任務。 無論是組織檔案、自動化任務還是管理數據,透過程式碼重新命名 Excel 檔案的能力都非常有益。 In this article, we'll explore how to rename Excel files using IronXL library from Iron Software開發的強大 C# Excel 庫。 如何在 C&# 中重新命名 Excel 工作表 建立一個 Visual Studio 專案以重新命名 Excel 工作表。 Install IronXL library from Iron Software開發的強大 C# Excel 庫。 使用IronXL重新命名 Excel 工作表。 IronXL library from Iron Software IronXL is a powerful C# Excel library developed by Iron Software開發的強大 C# Excel 庫。 它可以讓你在 .NET 專案中使用 Excel 文件,而不需要 Microsoft Office 或 Excel Interop。 IronXL 的主要功能 讀取、編輯和創建 Excel 檔案:IronXL 允許你直接從你的 C# 或 VB.NET 程式碼讀取、生成和編輯 Excel 試算表文件(包括 XLSX、XLS、XLSM、XLTX、CSV 和 TSV 格式)。 無需 Office Interop:你不需要安裝 Microsoft Office 或處理 Office Interop 的複雜性。IronXL 提供了一個無障礙的體驗。 跨平台支持:IronXL 為 .NET 8、7、6、Core、Framework 和 Azure 設計。 無論你是在構建控制台應用程式、網頁應用程式還是桌面軟體,IronXL 都能滿足你的需求。 用戶友好型 API:直觀的 API 允許你執行如讀取單元格值、計算匯總值、處理公式、創建圖表等任務。 讓我們簡要查看一個例子: using IronXL; namespace RenameExcelSheets { public class Program { public static void Main() { Console.WriteLine("Rename Excel Sheets Using IronXL"); // Load an existing Excel file into a WorkBook object WorkBook workBook = WorkBook.Load("sample.xlsx"); // sample excel file // Select the specified worksheet (first sheet) WorkSheet workSheet = workBook.WorkSheets[0]; // Read a cell value from the workbook int cellValue = workSheet["A2"].IntValue; // Iterate through a range of cells and print their values 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); // Save as a new workbook workBook.SaveAs("sampleResult.xlsx"); } } } using IronXL; namespace RenameExcelSheets { public class Program { public static void Main() { Console.WriteLine("Rename Excel Sheets Using IronXL"); // Load an existing Excel file into a WorkBook object WorkBook workBook = WorkBook.Load("sample.xlsx"); // sample excel file // Select the specified worksheet (first sheet) WorkSheet workSheet = workBook.WorkSheets[0]; // Read a cell value from the workbook int cellValue = workSheet["A2"].IntValue; // Iterate through a range of cells and print their values 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); // Save as a new workbook workBook.SaveAs("sampleResult.xlsx"); } } } Imports IronXL Namespace RenameExcelSheets Public Class Program Public Shared Sub Main() Console.WriteLine("Rename Excel Sheets Using IronXL") ' Load an existing Excel file into a WorkBook object Dim workBook As WorkBook = WorkBook.Load("sample.xlsx") ' sample excel file ' Select the specified worksheet (first sheet) Dim workSheet As WorkSheet = workBook.WorkSheets(0) ' Read a cell value from the workbook Dim cellValue As Integer = workSheet("A2").IntValue ' Iterate through a range of cells and print their values 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) ' Save as a new workbook workBook.SaveAs("sampleResult.xlsx") End Sub End Class End Namespace $vbLabelText $csharpLabel 不費力的 Excel 功能:IronXL 可讓你輕鬆創建、載入、保存和操作試算表。 無論你是在處理元數據、權限、公式或樣式,IronXL 都能簡化過程。 請記住,IronXL 因其準確性、易用性和速度而受到全球數百萬工程師的信賴。 如果你在 C# 或 VB.NET 中使用 Excel 檔案,IronXL 是你的首選庫! 設置環境 在進入編碼部分之前,請確保已安裝必要的工具: Visual Studio:安裝 Visual Studio 或任何其他偏好的 C# IDE。 Microsoft Excel:確保 Microsoft Excel 已安裝在你的系統上。 為了展示一個重新命名 Excel 檔案的實際例子,讓我們編寫一個程式接受包含所有需要重新命名的檔案的資料夾,然後使用 IronXL 重新命名所有檔案,再將它們存儲在輸出資料夾中。 步驟 1:創建 Visual Studio 專案以重新命名 Excel 工作表。 打開 Visual Studio 並為演示創建一個新專案。 從以下模板中選擇控制台應用程式。 為專案提供名稱和存儲檔案的路徑。 選擇所需的 .NET 版本。 Step 2: Install IronXL library from Iron Software開發的強大 C# Excel 庫。 IronXL庫可以從 Visual Studio 套件管理器中安裝,如下所示。 或可以使用命令從 NuGet 套件管理器中安裝。 dotnet add package IronXL.Excel --version 2024.4.4 一旦安裝後,專案就可以開始編碼以重新命名 Excel 工作表。 步驟 3:使用IronXL重新命名 Excel 工作表 以下是使用 IronXL 庫重新命名目錄中所有文件和工作表的程式碼,用於商務應用程式。 輸入: using System; using System.IO; 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"); // Getting input folder path from user var folderPath = Console.ReadLine(); // Check if the provided path is not empty if (string.IsNullOrEmpty(folderPath)) { throw new ArgumentException("Path is empty"); } // Check if the given folder path exists if (!Directory.Exists(folderPath)) { throw new ArgumentException("Path is Wrong"); } // Get all files in the directory var files = Directory.GetFiles(folderPath); // Define output directory for renamed files var outputPath = Path.Combine(folderPath, "output"); Directory.CreateDirectory(outputPath); // Ensures the output directory exists 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 with a new name in the output folder workBook.SaveAs(Path.Combine(outputPath, $"FinancialReport2024_{index++}.xlsx")); } } } } using System; using System.IO; 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"); // Getting input folder path from user var folderPath = Console.ReadLine(); // Check if the provided path is not empty if (string.IsNullOrEmpty(folderPath)) { throw new ArgumentException("Path is empty"); } // Check if the given folder path exists if (!Directory.Exists(folderPath)) { throw new ArgumentException("Path is Wrong"); } // Get all files in the directory var files = Directory.GetFiles(folderPath); // Define output directory for renamed files var outputPath = Path.Combine(folderPath, "output"); Directory.CreateDirectory(outputPath); // Ensures the output directory exists 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 with a new name in the output folder workBook.SaveAs(Path.Combine(outputPath, $"FinancialReport2024_{index++}.xlsx")); } } } } Imports System Imports System.IO 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") ' Getting input folder path from user Dim folderPath = Console.ReadLine() ' Check if the provided path is not empty If String.IsNullOrEmpty(folderPath) Then Throw New ArgumentException("Path is empty") End If ' Check if the given folder path exists If Not Directory.Exists(folderPath) Then Throw New ArgumentException("Path is Wrong") End If ' Get all files in the directory Dim files = Directory.GetFiles(folderPath) ' Define output directory for renamed files Dim outputPath = Path.Combine(folderPath, "output") Directory.CreateDirectory(outputPath) ' Ensures the output directory exists 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 with a new name in the output folder 'INSTANT VB WARNING: An assignment within expression was extracted from the following statement: 'ORIGINAL LINE: workBook.SaveAs(Path.Combine(outputPath, string.Format("FinancialReport2024_{0}.xlsx", index++))); workBook.SaveAs(Path.Combine(outputPath, $"FinancialReport2024_{index}.xlsx")) index += 1 Next file End Sub End Class End Namespace $vbLabelText $csharpLabel 代碼解釋 程序提示用戶輸入 Excel 文件所在的資料夾路徑。 檢查資料夾路徑是否為空字串,以及資料夾是否實際存在。 檢索指定資料夾中的所有文件。 遍歷這些文件,將每個文件載入 IronXL 庫的 WorkBook 物件中。 對於每個工作簿,它會重新命名第一個工作表。 將每個修改後的工作簿保存到原資料夾中的 "output" 資料夾內。 輸出 在以下輸出中,你可以看到所有的 3 個文件均被重新命名,並且它們內部的 Excel 工作表也被重新命名為 FinancialReport2024。 以程式方式重新命名 Excel 工作表的優勢包括 效率:自動化減少了與手動重命名相關的人工努力和人為錯誤,節省時間和資源。 一致性:自動化重命名確保跨工作表的命名規則統一性和一致性,增強數據組織和可讀性。 可擴展性:以程式方式重命名工作表允許批量重命名和可擴展性,使其適合處理大型數據集或重複任務。 集成:與現有工作流程或應用程式整合使得數據處理無縫化並提高整體生產力。 自定義化:自動化提供了根據特定業務需求或標準自定義重命名邏輯的靈活性。 許可 IronXL 是一個依照許可協議工作的企業級庫。 關於許可的更多信息可以在這裡找到。 許可密鑰需要放在這裡的 appsettings.json 文件中。 { "IronXL.License.LicenseKey" : "IRONXL-MYLICENSE-KEY-1EF01" } 結論 使用 C# 重新命名 Excel 檔案是一個簡單的過程。 By leveraging the IronXL library from Iron Software, you can easily rename Excel files within your C# applications. 這個庫對於開發人員來說是一個方便的工具,無論是讀取、寫入還是管理 Excel 工作表。 既然你已經學會了如何以程式方式重新命名 Excel 檔案,你可以將此功能整合到你的 C# 專案中,以簡化檔案管理任務並提升自動化能力。 常見問題解答 如何在 C# 中重命名 Excel 工作表而不使用 Interop? 您可以通过利用 IronXL 库在 C# 中重命名 Excel 工作表,而无需使用 Interop。将您的 Excel 文件加载到 WorkBook 对象中,选择您希望重命名的工作表,然后通过 Name 属性设置其新名称。 使用IronXL進行Excel文件操作有哪些好處? IronXL 为 Excel 文件操作提供了强大的功能,例如读取、编辑和创建 Excel 文件,无需 Microsoft Excel 或 Interop。它支持 XLSX 和 CSV 等各种格式,提供用户友好的 API,并允许运用于 .NET 项目的自动化和集成。 在 C# 中可以自动化 Excel 任务吗? 是的,使用 IronXL 库,您可以在 C# 中自动化各种 Excel 任务,例如重命名工作表、读取单元格值、计算聚合等,所有这些都无需 Microsoft Excel。 设置 IronXL 用于 C# 项目涉及哪些步骤? 要在 C# 项目中设置 IronXL,首先创建一个 Visual Studio 项目,然后使用 NuGet 包管理器安装 IronXL 库,命令:dotnet add package IronXL.Excel --version 2024.4.4。然后您可以开始使用它的功能来操作 Excel 文件。 IronXL 可以处理多种 Excel 格式吗? 是的,IronXL 支持多种 Excel 格式,包括 XLSX、XLS、XLSM、XLTX、CSV 和 TSV,从而为 .NET 应用程序提供多种文件操作功能。 在我的项目中使用 IronXL 需要许可证吗? 是的,使用 IronXL 开发您的项目需要许可证。一旦您有了许可证密钥,应该将其放入您项目的 appsettings.json 文件中,以启用完整功能。 使用 IronXL 时如何排除常见问题? 使用 IronXL 时的常见问题通常可以通过确保库正确安装和配置、检查更新,以及查阅官方文档以获取特定功能的指导来解决。 以编程方式重命名 Excel 工作表有哪些优势? 以编程方式重命名 Excel 工作表可以提高效率和可扩展性,提供一致性,与现有工作流无缝集成,并根据特定的业务需求进行定制。 Jordi Bardia 立即與工程團隊聊天 軟體工程師 Jordi 在 Python、C# 和 C++ 上最得心應手,當他不在 Iron Software 展現技術時,便在做遊戲編程。在分担产品测测试,产品开发和研究的责任时,Jordi 为持续的产品改进增值。他说这种多样化的经验使他受到挑战并保持参与, 而这也是他与 Iron Software 中工作一大乐趣。Jordi 在佛罗里达州迈阿密长大,曾在佛罗里达大学学习计算机科学和统计学。 相關文章 發表日期 10月 27, 2025 如何在 C# 中創建 Excel 樞紐分析表 學習使用 C# Interop 和 IronXL 創建 Excel 中的樞紐分析表,這是一個清晰的分步指南。 閱讀更多 發表日期 10月 27, 2025 如何在 C# 中將 DataGridView 匯出為 Excel 並保留列標題 學習如何在 C# 教程中使用 IronXL library 將 DataGridView 資料匯出為 Excel 同時保留列標題。分步教學。 閱讀更多 發表日期 10月 27, 2025 如何使用 IronXL 的 .NET Core CSV 讀取器 學習使用 IronXL 作為 .NET Core CSV 讀取器的有效方法,提供實用範例。 閱讀更多 如何使用 C# Excel 字體樣式如何在 C# 控制台應用中讀...
發表日期 10月 27, 2025 如何在 C# 中將 DataGridView 匯出為 Excel 並保留列標題 學習如何在 C# 教程中使用 IronXL library 將 DataGridView 資料匯出為 Excel 同時保留列標題。分步教學。 閱讀更多