How to Edit Excel File in C#

C# Edit Excel File

Edit Excel files in C# using IronXL library which provides simple methods to modify cells, rows, columns, and worksheets programmatically without Excel Interop, reducing errors and complexity.

Developers need precision when modifying Excel files in C# because one error can corrupt entire documents. Simple, efficient code reduces error risk and simplifies programmatic editing or deletion of Excel files. IronXL provides a comprehensive solution for working with Excel files in C# that eliminates Microsoft Office dependencies. This guide covers the steps to edit Excel files in C# correctly and quickly using tested functions.

Quickstart: Edit a Specific Cell Value with IronXL

This example shows how to load an existing Excel file, update a single cell, and save changes using IronXL. Get started in under 5 lines, no Interop required. For complex operations, explore our comprehensive API Reference.

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

How Do I Set Up IronXL for C# Excel Editing?

This tutorial uses IronXL, a C# Excel library. Download and install it into your project (free for development) to use these functions. IronXL supports various platforms including Linux and macOS, making it versatile for cross-platform development.

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

Once installed, let's get started. New to IronXL? Check our Get Started Overview for a comprehensive introduction.


Install-Package IronXL.Excel

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


How to Tutorial

How Do I Edit Specific Cell Values in Excel?

First, learn how to edit specific cell values of an Excel SpreadSheet. This is one of the most common operations when editing Excel files in C#.

Import the Excel SpreadSheet to modify, then access its WorkSheet. Apply modifications as shown below. IronXL provides multiple ways to access cells, offering flexibility for different programming styles.

: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:

BeforeAfter
Excel spreadsheet with highlighted cell showing 'France' in Country column, demonstrating cell selection for editingExcel cell B4 showing 'New Value' with blue selection border after editing cell content

Notice how simple it is to modify the Excel SpreadSheet value. This approach works well when updating specific data points without affecting the rest of your spreadsheet.

An alternative way to edit specific cell values 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

How Do I Edit Full Row Values in Excel?

Edit full row values of an Excel SpreadSheet with a static value easily. This operation helps when updating entire records at once, such as resetting data or applying bulk updates. For complex row operations, explore adding rows and columns.

: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:

BeforeAfter
Excel spreadsheet with row 4 highlighted showing Government segment data for France with Montana productExcel table showing new row with 'New Value' entries added across all columns in a sales data spreadsheet

Edit values of a specific range within a row using the 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

When working with ranges, you might also need selecting ranges for advanced operations like sorting or applying formulas.


How Do I Edit Full Column Values in Excel?

Edit full columns of Excel SpreadSheet values with a single value easily. This works well for scenarios like updating currency columns, applying new tax rates, or standardizing data formats across an entire column.

: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

This produces our sample.xlsx spreadsheet as shown:

BeforeAfter
Excel spreadsheet with Country column highlighted in blue, showing business data across segments and countriesExcel spreadsheet showing column B filled with 'New Value' text after bulk column edit operation

How Do I Edit a Full Row with Dynamic Values?

IronXL allows editing specific rows with dynamic values. Edit a full row by assigning dynamic values for each cell. This approach works well when importing data from databases or other sources requiring unique values per cell. 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

The table below shows screenshots of Excel SpreadSheet sample.xlsx from this output:

BeforeAfter
Excel spreadsheet with row 4 highlighted showing Enterprise segment data before editing with dynamic valuesExcel sheet with row 4 showing 'New Value 0-4' placeholder text after dynamic row editing operation

How Do I Edit a Full Column with Dynamic Values?

Edit specific columns with dynamic values simply. This technique helps populate columns with sequential data like ID numbers, dates, or calculated values based on row position.

: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:

BeforeAfter
Excel spreadsheet with Country column highlighted, showing business data across segments and productsExcel sheet showing Country column filled with dynamic placeholder values 'New Value 1-9' after bulk edit operation

How Do I Replace Spreadsheet Values in Excel?

Replace any value type with an updated value in an Excel SpreadSheet using the Replace function. Use this function to replace Excel SpreadSheet data in any required situation. This works well for data cleaning, updating terminology, or correcting systematic errors across spreadsheets.

How Do I Replace Values in an Entire Worksheet?

To replace a specific value throughout a complete Excel WorkSheet with an updated value, access the WorkSheet ws (same as above examples) and apply the Replace function:

// 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 replaces old value with new value in a complete Excel WorkSheet. It's a powerful feature for bulk updates that would otherwise require manual find-and-replace operations in Excel.

Remember to save the file after any change, as shown in the examples above. For more information on saving and exporting Excel files, see our guide on converting spreadsheet file types.

How Do I Replace Values in a Specific Row?

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 replaces old value with new value only in row number 2. The rest of the WorkSheet remains unchanged.

How Do I Replace Values in a Row Range?

Replace 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

To replace an old value with a new value in the range from B4 to E4 of row number 4, write:

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

How Do I Replace Values in a Specific Column?

Replace values of a specific column while keeping the rest of the worksheet unchanged:

// 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 replaces old value with new value only for column number 1.

How Do I Replace Values in a Column Range?

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 replaces old value with new value only within the range from B5 to B10 for column B.


How Do I Remove a Row from Excel Worksheet?

IronXL provides a simple function to remove a specific row of an Excel WorkSheet. This functionality helps when cleaning data or removing outdated records from spreadsheets. 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 removes row number 3 of sample.xlsx as shown in the following table:

BeforeAfter
Excel worksheet with row 4 highlighted showing Government segment data for Canada before row removal operationExcel worksheet with row 4 highlighted in blue showing the result after row removal operation

How Do I Remove a Worksheet from Excel File?

To remove a complete WorkSheet of an Excel file, use the following method. This helps when consolidating data or removing temporary worksheets used for calculations. For more information on managing worksheets, see our guide on managing worksheets.

// 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. To remove a worksheet by 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
' Remove a worksheet by its name
wb.RemoveWorkSheet("Sheet1") 'by sheet name
$vbLabelText   $csharpLabel

IronXL includes many more functions for easily performing any type of editing and deletion in Excel SpreadSheets. Explore additional features like creating Excel charts, applying conditional formatting, or working with formulas. Contact our dev team with any questions about using IronXL 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 How Do I Remove a Worksheet from Excel File?

Frequently Asked Questions

How do I edit Excel files in C# without Microsoft Office installed?

IronXL allows you to edit Excel files in C# without requiring Microsoft Office or Excel Interop. It provides a standalone library that can modify cells, rows, columns, and worksheets programmatically, eliminating Office dependencies and working across platforms including Linux and macOS.

What is the simplest way to update a cell value in an Excel file using C#?

With IronXL, you can update a cell value in just a few lines of code: IronXL.WorkBook.Load("file.xlsx").GetWorkSheet("Sheet1")["C3"].Value = "Hello IronXL"; followed by saving the workbook. This approach requires no Excel Interop and provides direct cell access.

Can I edit entire rows and columns in Excel programmatically?

Yes, IronXL supports editing full rows and columns with either static or dynamic values. You can modify entire rows with a single value, populate columns programmatically, or update rows with dynamic data from various sources like databases or calculations.

How do I remove rows or worksheets from an Excel file in C#?

IronXL provides simple methods to remove rows from worksheets and delete entire worksheets from Excel files. These operations are performed programmatically without requiring Excel to be installed, making it ideal for server-side applications.

Is it possible to find and replace values throughout an Excel spreadsheet?

Yes, IronXL includes functionality to search and replace values across spreadsheets. You can programmatically find specific values and replace them with new data, similar to Excel's native find and replace feature but automated through C# code.

What platforms does the C# Excel editing library support?

IronXL supports multiple platforms including Windows, Linux, and macOS, making it versatile for cross-platform development. This allows you to edit Excel files in C# applications regardless of the deployment environment.

How do I get started with editing Excel files in C#?

To get started, download and install IronXL via NuGet package manager. The library is free for development use. Once installed, you can immediately begin loading Excel files, modifying cells, and saving changes with simple, intuitive code.

What are the advantages of programmatic Excel editing over manual methods?

IronXL enables programmatic Excel editing which reduces human error, automates repetitive tasks, and allows for bulk operations that would be time-consuming manually. It also eliminates the need for Excel Interop, reducing complexity and improving performance.

Curtis Chau
Technical Writer

Curtis Chau holds a Bachelor’s degree in Computer Science (Carleton University) and specializes in front-end development with expertise in Node.js, TypeScript, JavaScript, and React. Passionate about crafting intuitive and aesthetically pleasing user interfaces, Curtis enjoys working with modern frameworks and creating well-structured, visually appealing manuals.

...

Read More
Ready to Get Started?
Nuget Downloads 1,753,501 | Version: 2025.12 just released