IronXL 开始 C# Excel Interop C# Excel Interop Workaround Curtis Chau 已更新:八月 20, 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 This article was translated from English: Does it need improvement? Translated View the article in English Many projects use Excel to communicate clearly, but if you are using Microsoft.Office.Interop.Excel, then you have likely had to face many complicated lines of code. In this tutorial, we will use IronXL as a C# Excel Interop workaround, so you don't have to use Interop in order to finish your project. You can use Excel file data, create Excel files, edit, and manipulate all using C# programming. C# Excel No Interop Download Excel No Interop Library Access Excel file in C# Create a new Excel file and insert data programmatically Modify existing files, update and replace cell values, remove rows, and more How to Use Excel Interop Alternatively Install an Excel library to process Excel files. Open the Workbook and add the current Excel file. Set the default Worksheet. Read the value from the Excel Workbook. Process and show the value. Step 1 1. Download IronXL Library Download IronXL Library or install with NuGet to access the free library and then follow step by step to work through this tutorial on using Excel without Interop. Licenses available if you want to go live with your project. Install-Package IronXL.Excel How to Tutorial 2. Access Excel File Data To develop business applications, we need easy and perfect access to data from Excel files and the ability to manipulate them programmatically according to a variety of requirements. With IronXL, use the WorkBook.Load() function, which grants access to reading a specific Excel file. After accessing the WorkBook, you can then choose a specific WorkSheet using the WorkBook.GetWorkSheet() function. Now you have all Excel file data available. See the following example for how we used these functions to get Excel file data in a C# project. // Import the IronXL library to access its functionality using IronXL; static void Main(string[] args) { // Access Excel file WorkBook wb = WorkBook.Load("sample.xlsx"); // Access WorkSheet of Excel file WorkSheet ws = wb.GetWorkSheet("Sheet1"); // Get a specific cell value, convert it to a string, and print it string a = ws["A5"].Value.ToString(); Console.WriteLine("Getting Single Value:\n\n Value of Cell A5: {0}", a); Console.WriteLine("\nGetting Many Cells Value using Loop:\n"); // Get multiple cell values using range function and iterate through them foreach (var cell in ws["B2:B10"]) { Console.WriteLine(" Value is: {0}", cell.Text); } Console.ReadKey(); // Pause the console to view output } // Import the IronXL library to access its functionality using IronXL; static void Main(string[] args) { // Access Excel file WorkBook wb = WorkBook.Load("sample.xlsx"); // Access WorkSheet of Excel file WorkSheet ws = wb.GetWorkSheet("Sheet1"); // Get a specific cell value, convert it to a string, and print it string a = ws["A5"].Value.ToString(); Console.WriteLine("Getting Single Value:\n\n Value of Cell A5: {0}", a); Console.WriteLine("\nGetting Many Cells Value using Loop:\n"); // Get multiple cell values using range function and iterate through them foreach (var cell in ws["B2:B10"]) { Console.WriteLine(" Value is: {0}", cell.Text); } Console.ReadKey(); // Pause the console to view output } ' Import the IronXL library to access its functionality Imports Microsoft.VisualBasic Imports IronXL Shared Sub Main(ByVal args() As String) ' Access Excel file Dim wb As WorkBook = WorkBook.Load("sample.xlsx") ' Access WorkSheet of Excel file Dim ws As WorkSheet = wb.GetWorkSheet("Sheet1") ' Get a specific cell value, convert it to a string, and print it Dim a As String = ws("A5").Value.ToString() Console.WriteLine("Getting Single Value:" & vbLf & vbLf & " Value of Cell A5: {0}", a) Console.WriteLine(vbLf & "Getting Many Cells Value using Loop:" & vbLf) ' Get multiple cell values using range function and iterate through them For Each cell In ws("B2:B10") Console.WriteLine(" Value is: {0}", cell.Text) Next cell Console.ReadKey() ' Pause the console to view output End Sub $vbLabelText $csharpLabel This code will produce the following outcome: With the Excel file looking like this: We can see that our Excel file, sample.xlsx, has small business in the A5 cell. The other values from B2 to B10 are the same and displayed in the output. DataSet and DataTables We can also work with Excel files as a dataset and datatables using these directions. // Access WorkBook and WorkSheet WorkBook wb = WorkBook.Load("sample.xlsx"); WorkSheet ws = wb.GetWorkSheet("Sheet1"); // Convert workbook to DataSet DataSet ds = wb.ToDataSet(); // Convert worksheet to DataTable DataTable dt = ws.ToDataTable(true); // Access WorkBook and WorkSheet WorkBook wb = WorkBook.Load("sample.xlsx"); WorkSheet ws = wb.GetWorkSheet("Sheet1"); // Convert workbook to DataSet DataSet ds = wb.ToDataSet(); // Convert worksheet to DataTable DataTable dt = ws.ToDataTable(true); ' Access WorkBook and WorkSheet Dim wb As WorkBook = WorkBook.Load("sample.xlsx") Dim ws As WorkSheet = wb.GetWorkSheet("Sheet1") ' Convert workbook to DataSet Dim ds As DataSet = wb.ToDataSet() ' Convert worksheet to DataTable Dim dt As DataTable = ws.ToDataTable(True) $vbLabelText $csharpLabel You can read more about how to work with Excel DataSet and DataTables, which provides more code examples and explanations on the process. Now, we will see another aspect, which is about creating a new Excel file in our C# project. 3. Create New Excel File We can easily create a new Excel spreadsheet and insert data in it programmatically in our C# project. To accomplish this, IronXL provides the WorkBook.Create() function, which creates a new Excel file. We can then create as many WorkSheets as needed using the WorkBook.CreateWorkSheet() function. After that, we can also insert data, as shown in the example below: // Import the IronXL library using IronXL; static void Main(string[] args) { // Create a new WorkBook WorkBook wb = WorkBook.Create(); // Create a new WorkSheet in the workbook WorkSheet ws = wb.CreateWorkSheet("sheet1"); // Insert data into cells ws["A1"].Value = "New Value A1"; ws["B2"].Value = "New Value B2"; // Save the newly created Excel file wb.SaveAs("NewExcelFile.xlsx"); } // Import the IronXL library using IronXL; static void Main(string[] args) { // Create a new WorkBook WorkBook wb = WorkBook.Create(); // Create a new WorkSheet in the workbook WorkSheet ws = wb.CreateWorkSheet("sheet1"); // Insert data into cells ws["A1"].Value = "New Value A1"; ws["B2"].Value = "New Value B2"; // Save the newly created Excel file wb.SaveAs("NewExcelFile.xlsx"); } ' Import the IronXL library Imports IronXL Shared Sub Main(ByVal args() As String) ' Create a new WorkBook Dim wb As WorkBook = WorkBook.Create() ' Create a new WorkSheet in the workbook Dim ws As WorkSheet = wb.CreateWorkSheet("sheet1") ' Insert data into cells ws("A1").Value = "New Value A1" ws("B2").Value = "New Value B2" ' Save the newly created Excel file wb.SaveAs("NewExcelFile.xlsx") End Sub $vbLabelText $csharpLabel The above code will create a new Excel file with the name NewExcelFile.xlsx, and insert new data in cell addresses A1 and B2 with values New Value A1 and New Value B2 respectively. With this setup, you can insert data in the same way, as much as you need. Note: If you are creating a new Excel file or modifying an existing one, don't forget to save the file as shown in the example above. Dig deeper into how to Create new Excel SpreadSheets using C# and try the code in your project. 4. Modify Existing Excel File We can modify existing Excel files and insert updated data in them programmatically. In Excel file modifications, we will see the following aspects: Update the cell value Replace old values with new ones Remove rows or columns Let's see how to implement the above topics in our C# project. Update Cell Value It is very simple to update the cell value of an existing Excel spreadsheet. Just access the Excel file in the project and specify its WorkSheet, and then update its data as shown in the example below: // Import the IronXL library using IronXL; static void Main(string[] args) { // Access the WorkBook and WorkSheet WorkBook wb = WorkBook.Load("sample.xlsx"); WorkSheet ws = wb.GetWorkSheet("Sheet1"); // Update A3 cell value ws["A3"].Value = "New Value of A3"; // Save the updated Excel file wb.SaveAs("sample.xlsx"); } // Import the IronXL library using IronXL; static void Main(string[] args) { // Access the WorkBook and WorkSheet WorkBook wb = WorkBook.Load("sample.xlsx"); WorkSheet ws = wb.GetWorkSheet("Sheet1"); // Update A3 cell value ws["A3"].Value = "New Value of A3"; // Save the updated Excel file wb.SaveAs("sample.xlsx"); } ' Import the IronXL library Imports IronXL Shared Sub Main(ByVal args() As String) ' Access the WorkBook and WorkSheet Dim wb As WorkBook = WorkBook.Load("sample.xlsx") Dim ws As WorkSheet = wb.GetWorkSheet("Sheet1") ' Update A3 cell value ws("A3").Value = "New Value of A3" ' Save the updated Excel file wb.SaveAs("sample.xlsx") End Sub $vbLabelText $csharpLabel The above code will update the value of cell A3 with New Value of A3. We can also update multiple cells with a static value, using the range function: ws["A3:C3"].Value = "New Value"; ws["A3:C3"].Value = "New Value"; ws("A3:C3").Value = "New Value" $vbLabelText $csharpLabel This will update row number 3 from A3 to C3 cells with the New Value in the Excel file. Learn more about using the Range Function in C# with these examples. Replace Cell Values One of the strengths of IronXL is its ability to easily replace old values with new values in existing Excel files, covering all the following aspects: Replace values of a complete WorkSheet: ws.Replace("old value", "new value"); ws.Replace("old value", "new value"); ws.Replace("old value", "new value") $vbLabelText $csharpLabel Replace values of a specific row: ws.Rows[RowIndex].Replace("old value", "new value"); ws.Rows[RowIndex].Replace("old value", "new value"); ws.Rows(RowIndex).Replace("old value", "new value") $vbLabelText $csharpLabel Replace values of a specific column: ws.Columns[ColumnIndex].Replace("old value", "new Value"); ws.Columns[ColumnIndex].Replace("old value", "new Value"); ws.Columns(ColumnIndex).Replace("old value", "new Value") $vbLabelText $csharpLabel Replace values in a specific range: ws["From:To"].Replace("old value", "new value"); ws["From:To"].Replace("old value", "new value"); ws("From:To").Replace("old value", "new value") $vbLabelText $csharpLabel Let's view an example to clearly see how to use the above functions in our C# Project to replace the values. In this example, we'll use the replace function to replace a value in a specific range. // Import the IronXL library using IronXL; static void Main(string[] args) { // Access the WorkBook and WorkSheet WorkBook wb = WorkBook.Load("sample.xlsx"); WorkSheet ws = wb.GetWorkSheet("Sheet1"); // Specify a range from B5 to G5 and replace "Normal" value with "Good" ws["B5:G5"].Replace("Normal", "Good"); // Save the updated Excel file wb.SaveAs("sample.xlsx"); } // Import the IronXL library using IronXL; static void Main(string[] args) { // Access the WorkBook and WorkSheet WorkBook wb = WorkBook.Load("sample.xlsx"); WorkSheet ws = wb.GetWorkSheet("Sheet1"); // Specify a range from B5 to G5 and replace "Normal" value with "Good" ws["B5:G5"].Replace("Normal", "Good"); // Save the updated Excel file wb.SaveAs("sample.xlsx"); } ' Import the IronXL library Imports IronXL Shared Sub Main(ByVal args() As String) ' Access the WorkBook and WorkSheet Dim wb As WorkBook = WorkBook.Load("sample.xlsx") Dim ws As WorkSheet = wb.GetWorkSheet("Sheet1") ' Specify a range from B5 to G5 and replace "Normal" value with "Good" ws("B5:G5").Replace("Normal", "Good") ' Save the updated Excel file wb.SaveAs("sample.xlsx") End Sub $vbLabelText $csharpLabel The above code will replace the Normal value with Good from B5 to G5, and the rest of the WorkSheet remains the same. See more about how to Edit Excel Cell Values in a Range using this function of IronXL. Remove Rows of Excel File In application development, we sometimes need to remove whole rows of existing Excel files programmatically. For this task, we use the Remove() function of IronXL. Here's an example: // Import the IronXL library using IronXL; static void Main(string[] args) { // Access the WorkBook and WorkSheet WorkBook wb = WorkBook.Load("sample.xlsx"); WorkSheet ws = wb.GetWorkSheet("Sheet1"); // Remove the row number 2 ws.Rows[2].Remove(); // Save the updated Excel file wb.SaveAs("sample.xlsx"); } // Import the IronXL library using IronXL; static void Main(string[] args) { // Access the WorkBook and WorkSheet WorkBook wb = WorkBook.Load("sample.xlsx"); WorkSheet ws = wb.GetWorkSheet("Sheet1"); // Remove the row number 2 ws.Rows[2].Remove(); // Save the updated Excel file wb.SaveAs("sample.xlsx"); } ' Import the IronXL library Imports IronXL Shared Sub Main(ByVal args() As String) ' Access the WorkBook and WorkSheet Dim wb As WorkBook = WorkBook.Load("sample.xlsx") Dim ws As WorkSheet = wb.GetWorkSheet("Sheet1") ' Remove the row number 2 ws.Rows(2).Remove() ' Save the updated Excel file wb.SaveAs("sample.xlsx") End Sub $vbLabelText $csharpLabel The above code will remove row number 2 of sample.xlsx. Tutorial Quick Access IronXL Reference Read the API Reference for IronXL to read more about all the functions, features, classes, and namespaces. IronXL Reference 常见问题解答 如何在不使用 Interop 的情况下在 C# 中读取 Excel 文件? 您可以使用 IronXL 在 C# 中读取 Excel 文件而无需 Interop。使用 WorkBook.Load() 函数加载文件,并使用 WorkBook.GetWorkSheet() 访问工作表。 如何在 C# 中以编程方式创建一个新的 Excel 文件? 使用 IronXL,您可以通过使用 WorkBook.Create() 方法创建一个新的 Excel 文件,然后使用 WorkSheet.CreateWorkSheet() 添加数据。 是否可以在不使用 Interop 的情况下在 C# 中修改 Excel 文件? 是的,IronXL 提供了无需 Interop 便可修改 Excel 文件的功能,包括更新单元格值、替换旧值和直接移除行或列。 如何使用 IronXL 更新 Excel 表格中的特定单元格值? 要更新单元格值,请使用 WorkBook.GetWorkSheet() 加载您的工作表,为单元格指定新值,然后保存更改。 我可以使用 IronXL 替换一系列单元格中的值吗? IronXL 允许您通过在工作表或特定范围对象上使用 Replace() 函数来替换值。 使用 IronXL 比使用 Microsoft.Office.Interop.Excel 有何优势? IronXL 通过提供易于使用的 API 简化了 Excel 文件操作,减少了复杂的 Interop 代码需求,并提供了更好的性能和可靠性。 如何使用 IronXL 从 Excel 文件中删除一行? 要在 IronXL 中删除一行,请在工作表内的指定行对象上使用 Remove() 函数。 IronXL 是否支持将 Excel 文件转换为 DataSets 和 DataTables? 是的,IronXL 支持将工作簿转换为 DataSets 以及将工作表转换为 DataTables,允许更灵活的数据处理。 如何在 C# 项目中安装 IronXL? 您可以通过从 Iron Software 网站下载或使用 NuGet 包管理器并使用命令:Install-Package IronXL.Excel 在您的 C# 项目中安装 IronXL。 IronXL 适合大型 Excel 文件吗? IronXL 优化了对大型 Excel 文件的处理,提供了比传统 Interop 方法更快的处理速度和更小的内存占用。 Curtis Chau 立即与工程团队聊天 技术作家 Curtis Chau 拥有卡尔顿大学的计算机科学学士学位,专注于前端开发,精通 Node.js、TypeScript、JavaScript 和 React。他热衷于打造直观且美观的用户界面,喜欢使用现代框架并创建结构良好、视觉吸引力强的手册。除了开发之外,Curtis 对物联网 (IoT) 有浓厚的兴趣,探索将硬件和软件集成的新方法。在空闲时间,他喜欢玩游戏和构建 Discord 机器人,将他对技术的热爱与创造力相结合。 准备开始了吗? Nuget 下载 1,686,155 | 版本: 2025.11 刚刚发布 免费 NuGet 下载 总下载量:1,686,155 查看许可证