如何在C#中编辑Excel文件

C# Edit Excel File

This article was translated from English: Does it need improvement?
Translated
View the article in English

Developers have to be careful when they set out to modify and edit Excel files in C# because it can be easy for one misstep to change the whole document. Being able to rely on simple and efficient lines of code helps reduce the risk of error, and makes it easier for us to edit or delete Excel files programmatically. Today we'll walk through the steps necessary to edit Excel files in C# correctly and quickly using tested functions.

Quickstart: Edit a Specific Cell Value with IronXL

This example shows how easy it is to load an existing Excel file, update a single cell, and save the changes using IronXL. Get started in under 5 lines, no Interop required.

Nuget IconGet started making PDFs with NuGet now:

  1. Install IronXL with NuGet Package Manager

    PM > Install-Package IronXL.Excel

  2. Copy and run this code snippet.

    IronXL.WorkBook.Load("file.xlsx").GetWorkSheet("Sheet1")["C3"].Value = "Hello IronXL";
    // then save your workbook
    iroExcelWorkBook.SaveAs("file.xlsx");
  3. Deploy to test on your live environment

    Start using IronXL in your project today with a free trial
    arrow pointer

Step 1

1. C# Edit Excel Files using the IronXL Library

For this tutorial, we'll be using the functions defined by IronXL, a C# Excel library. To use these functions you'll need to first download and install it into your project (free for development).

You can either Download IronXL.zip or read more and install via the NuGet package page.

Once you've installed it, let's get started!


Install-Package IronXL.Excel

Replace x.x.x with the appropriate version number as needed.


How to Tutorial

2. Edit Specific Cell Values

First, we will look at how to edit specific cell values of an Excel SpreadSheet.

For this purpose, we import the Excel SpreadSheet which is to be modified, and then access its WorkSheet. Then we can apply the modifications as shown below.

:path=/static-assets/excel/content-code-examples/how-to/csharp-edit-excel-file-specific-cell-value.cs
using IronXL;

// Load the Excel workbook
WorkBook wb = WorkBook.Load("sample.xlsx");
// Access a specific worksheet
WorkSheet ws = wb.GetWorkSheet("Sheet1");
// Access specific cell by identifying its row and column, then modify its value
ws.Rows[3].Columns[1].Value = "New Value";
// Save changes to the workbook
wb.SaveAs("sample.xlsx");
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

Here are before and after screenshots of Excel SpreadSheet sample.xlsx:

Before After
before after

We can see how simple it is to modify the Excel SpreadSheet value.

If needed, there is also an alternative way to edit the specific cell value by cell address:

// Alternative way to access specific cell and apply changes
ws["B4"].Value = "New Value";
// Alternative way to access specific cell and apply changes
ws["B4"].Value = "New Value";
' Alternative way to access specific cell and apply changes
ws("B4").Value = "New Value"
$vbLabelText   $csharpLabel

3. Edit Full Row Values

It is pretty simple to edit full row values of an Excel SpreadSheet with a static value.

:path=/static-assets/excel/content-code-examples/how-to/csharp-edit-excel-file-row-value.cs
using IronXL;

WorkBook wb = WorkBook.Load("sample.xlsx");
WorkSheet ws = wb.GetWorkSheet("Sheet1");
// Setting a static value for the entire row
ws.Rows[3].Value = "New Value";
wb.SaveAs("sample.xlsx");
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

See the screenshots of sample.xlsx below:

Before After
before after

For this, we also can edit the value of a specific range of the row, by using range function:

// Editing a specific range of a row
ws["A3:E3"].Value = "New Value";
// Editing a specific range of a row
ws["A3:E3"].Value = "New Value";
' Editing a specific range of a row
ws("A3:E3").Value = "New Value"
$vbLabelText   $csharpLabel

4. Edit Full Column Values

In the same way as above, we can easily edit full columns of Excel SpreadSheet values with a single value.

:path=/static-assets/excel/content-code-examples/how-to/csharp-edit-excel-file-full-column.cs
using IronXL;

WorkBook wb = WorkBook.Load("sample.xlsx");
WorkSheet ws = wb.GetWorkSheet("Sheet1");
// Setting a static value for the entire column
ws.Columns[1].Value = "New Value";
wb.SaveAs("sample.xlsx");
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

Which will produce our sample.xlsx spreadsheet as such:

Before After
before after

5. Edit Full Row with Dynamic Values

Using IronXL, it is also possible to edit specific rows with dynamic values. This means we can edit a full row by assigning dynamic values for each cell. Let's see the example:

:path=/static-assets/excel/content-code-examples/how-to/csharp-edit-excel-file-full-row-dynamic.cs
using IronXL;
using System.Linq;

WorkBook wb = WorkBook.Load("sample.xlsx");
WorkSheet ws = wb.GetWorkSheet("Sheet1");
for (int i = 0; i < ws.Columns.Count(); i++)
{
    // Assign dynamic values to each cell in the row
    ws.Rows[3].Columns[i].Value = "New Value " + i.ToString();
}
wb.SaveAs("sample.xlsx");
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

In the table below, we see the screenshots of Excel SpreadSheet sample.xlsx from this output:

Before After
before after

6. Edit Full Column with Dynamic Values

It is also simple to edit specific columns with dynamic values.

:path=/static-assets/excel/content-code-examples/how-to/csharp-edit-excel-file-full-column-dynamic.cs
using IronXL;
using System.Linq;

WorkBook wb = WorkBook.Load("sample.xlsx");
WorkSheet ws = wb.GetWorkSheet("Sheet1");
for (int i = 0; i < ws.Rows.Count(); i++)
{
    // Skip the first row if it's used as a header
    if (i == 0)
        continue;
    // Assign dynamic values to each cell in the column
    ws.Rows[i].Columns[1].Value = "New Value " + i.ToString();
}
wb.SaveAs("sample.xlsx");
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

With the table results of sample.xlsx below:

Before After
before after

7. Replace Spreadsheet Values

If we want to replace any type of value with an updated value in an Excel SpreadSheet, we can use the function named Replace. Using this function, we can replace the data of Excel SpreadSheet in any required situation.

7.1. Replace Specific Value of Complete WorkSheet

To replace a specific value of a complete Excel WorkSheet with an updated value, we just access the WorkSheet ws (same as in the above examples) and apply the Replace function like this.

// Replace a specific value in the entire worksheet
ws.Replace("old value", "new value");
// Replace a specific value in the entire worksheet
ws.Replace("old value", "new value");
' Replace a specific value in the entire worksheet
ws.Replace("old value", "new value")
$vbLabelText   $csharpLabel

This function will replace old value with new value in a complete Excel WorkSheet.

Don't forget to save the file after any change, as shown in the examples above.

7.2. Replace the Values of Specific Row

If you only want to make changes to a specific row instead of the whole worksheet, use this code.

// Replace a specific value in a specific row
ws.Rows[2].Replace("old value", "new value");
// Replace a specific value in a specific row
ws.Rows[2].Replace("old value", "new value");
' Replace a specific value in a specific row
ws.Rows(2).Replace("old value", "new value")
$vbLabelText   $csharpLabel

The above code will replace old value with new value only in row number 2. The rest of the WorkSheet remains the same.

7.3. Replace the Values of Row Range

We also can replace the values within a specific range as follows:

// Replace specific values in a row range
ws["From Cell Address : To Cell Address"].Replace("old value", "new value");
// Replace specific values in a row range
ws["From Cell Address : To Cell Address"].Replace("old value", "new value");
' Replace specific values in a row range
ws("From Cell Address : To Cell Address").Replace("old value", "new value")
$vbLabelText   $csharpLabel

Suppose, if we want to replace an old value with a new value, just in the range from B4 to E4 of row no 4, then we would write it like this:

ws["B4:E4"].Replace("old value", "new value");
ws["B4:E4"].Replace("old value", "new value");
ws("B4:E4").Replace("old value", "new value")
$vbLabelText   $csharpLabel

7.4. Replace the Values of Specific Column

We can also replace the values of a specific column, and the rest of the worksheet remains the same.

// Replace specific values in a column
ws.Columns[1].Replace("old value", "new value");
// Replace specific values in a column
ws.Columns[1].Replace("old value", "new value");
' Replace specific values in a column
ws.Columns(1).Replace("old value", "new value")
$vbLabelText   $csharpLabel

The above code will replace old value with new value just for column number 1.

7.5. Replace the Values of Column Range

By the following way, we also can use the range function to replace within a range of a specific column.

// Replace specific values in a column range
ws["B5:B10"].Replace("old value", "new value");
// Replace specific values in a column range
ws["B5:B10"].Replace("old value", "new value");
' Replace specific values in a column range
ws("B5:B10").Replace("old value", "new value")
$vbLabelText   $csharpLabel

The above code will replace old value with new value just within range from B5 to B10 for column B.


8. Remove Row from Excel WorkSheet

IronXL provides a very simple function to remove a specific row of an Excel WorkSheet. Let's see the example.

:path=/static-assets/excel/content-code-examples/how-to/csharp-edit-excel-file-row-value.cs
using IronXL;

WorkBook wb = WorkBook.Load("sample.xlsx");
WorkSheet ws = wb.GetWorkSheet("Sheet1");
// Setting a static value for the entire row
ws.Rows[3].Value = "New Value";
wb.SaveAs("sample.xlsx");
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

The above code will remove row number 3 of sample.xlsx as shown in the following table:

Before After
before after

9. Remove WorkSheet from Excel File

If we want to remove a complete WorkSheet of an Excel file, we can use the following method:

// Remove a worksheet by its index
wb.RemoveWorkSheet(1); // by sheet indexing
// Remove a worksheet by its index
wb.RemoveWorkSheet(1); // by sheet indexing
' Remove a worksheet by its index
wb.RemoveWorkSheet(1) ' by sheet indexing
$vbLabelText   $csharpLabel

wb is the WorkBook, same as in the above examples. If we want to remove a worksheet by name, then:

// Remove a worksheet by its name
wb.RemoveWorkSheet("Sheet1"); //by sheet name
// Remove a worksheet by its name
wb.RemoveWorkSheet("Sheet1"); //by sheet name
' Remove a worksheet by its name
wb.RemoveWorkSheet("Sheet1") 'by sheet name
$vbLabelText   $csharpLabel

IronXL is rich with many more functions by which we can easily perform any type of editing and deletion in Excel SpreadSheets. Please reach out to our dev team if you have any questions for use in your project.


Library Quick Access

IronXL Library Documentation

Explore the full capabilities of IronXL C# Library with various functions for editing, deleting, styling, and perfecting your Excel workbooks.

IronXL Library Documentation
Documentation related to 9. Remove WorkSheet from Excel File

常见问题解答

如何在不使用 Interop 的情况下在 C# 中编辑 Excel 文件?

您可以通过使用 IronXL 库在 C# 中编辑 Excel 文件,而无需使用 Interop。IronXL 提供多种修改 Excel 文档的方法,例如编辑单元格、行和列,甚至删除工作表。

如何使用 IronXL 修改 Excel 文件中的单元格值?

要使用 IronXL 修改 Excel 文件中的单元格值,请加载 Excel 工作簿并访问特定工作表。您可以通过指定行和列索引来更改单元格的值,然后保存工作簿。

在 C# Excel 应用程序中替换值的方法是什么?

在 C# Excel 应用程序中,您可以使用 IronXL 的 Replace 函数替换值。这允许您在整个工作表、特定行、列或范围内替换特定值。

如何在 C# 中以编程方式从 Excel 文件中删除工作表?

您可以使用 IronXL 的 RemoveWorkSheet 方法在 C# 中以编程方式从 Excel 文件中删除工作表。您可以通过名称或索引指定要删除的工作表。

我可以使用 C# 编辑 Excel 文件中的整行吗?

是的,使用 IronXL,您可以使用 C# 编辑 Excel 文件中的整行。您可以通过遍历列来为行中的单元格分配静态或动态值。

是否可以为 Excel 文件中的整列设置静态值?

是的,IronXL 允许您为 Excel 文件中的整列设置静态值。如果需要,您还可以通过遍历列中的每个单元格使用动态值。

如何使用 C# 从 Excel 工作表中删除特定行?

要使用 C# 从 Excel 工作表中删除特定行,请使用 IronXL 库的 RemoveRow 方法,指定要删除的行。

开始使用 C# 编辑 Excel 文件的初始步骤是什么?

要开始使用 C# 编辑 Excel 文件,首先下载并安装 IronXL 库。然后,加载您的 Excel 工作簿,选择您要编辑的工作表,并利用 IronXL 的功能修改单元格、行、列或工作表。

IronXL 适合开发环境吗?

是的,IronXL 非常适合开发环境。它可以免费用于开发目的,并提供全面的功能套件用于以编程方式编辑 Excel 文件。

如何使用 C# 将 Excel 文件转换为不同的格式?

IronXL 允许使用其导出功能将 Excel 文件转换为不同的格式。您可以使用 IronXL 的方法将已修改的 Excel 工作簿保存为 CSV、HTML 或 PDF 格式。

Curtis Chau
技术作家

Curtis Chau 拥有卡尔顿大学的计算机科学学士学位,专注于前端开发,精通 Node.js、TypeScript、JavaScript 和 React。他热衷于打造直观且美观的用户界面,喜欢使用现代框架并创建结构良好、视觉吸引力强的手册。

除了开发之外,Curtis 对物联网 (IoT) 有浓厚的兴趣,探索将硬件和软件集成的新方法。在空闲时间,他喜欢玩游戏和构建 Discord 机器人,将他对技术的热爱与创造力相结合。

准备开始了吗?
Nuget 下载 1,686,155 | 版本: 2025.11 刚刚发布