How to Edit Excel File in C#

This comprehensive tutorial explores editing Excel files using IronXL in C#. It begins with setting up the environment through the installation of IronXL via the NuGet package manager. The tutorial demonstrates how to load an Excel workbook, access specific worksheets, and modify cell values. Techniques for updating entire rows and columns, as well as applying dynamic values to cells, are discussed. The session also covers bulk updates using replace methods and showcases how to remove unwanted rows and worksheets. By following this guide, users can efficiently manage Excel files, ensuring data is updated and organized as needed. The tutorial concludes by highlighting the robust features of IronXL for seamless Excel file manipulation, encouraging viewers to explore further with Iron Software's tools.

Further Reading: C# Edit Excel File

// Import the necessary libraries
using IronXL;
using System;

class ExcelEditor
{
    static void Main()
    {
        // Load the Excel workbook
        WorkBook workbook = WorkBook.Load("example.xlsx");

        // Access the first worksheet of the workbook
        WorkSheet sheet = workbook.WorkSheets.First();

        // Modify a specific cell value
        sheet["A1"].Value = "Updated Value";

        // Update an entire row with new values
        sheet.Rows[0].SetValues(new object[] { "Row1-Col1", "Row1-Col2", "Row1-Col3" });

        // Update an entire column with new values
        sheet.Columns[0].SetValues(new object[] { "Col1-Row1", "Col1-Row2", "Col1-Row3" });

        // Apply dynamic values to a series of cells
        for (int i = 0; i < 10; i++)
        {
            sheet[$"B{i + 1}"].Value = $"Dynamic{i}";
        }

        // Use the FindAndReplace feature for bulk updates
        sheet["B1:B10"].FindAndReplace("Dynamic", "Replaced");

        // Remove unwanted rows
        sheet.DeleteRow(5); // Deletes the 6th row (0-indexed)

        // Remove unwanted worksheets
        workbook.WorkSheets.Remove(sheet); // Removes the accessed sheet from the workbook

        // Save changes to the workbook
        workbook.SaveAs("updated_example.xlsx");

        Console.WriteLine("Excel file has been updated successfully!");
    }
}
// Import the necessary libraries
using IronXL;
using System;

class ExcelEditor
{
    static void Main()
    {
        // Load the Excel workbook
        WorkBook workbook = WorkBook.Load("example.xlsx");

        // Access the first worksheet of the workbook
        WorkSheet sheet = workbook.WorkSheets.First();

        // Modify a specific cell value
        sheet["A1"].Value = "Updated Value";

        // Update an entire row with new values
        sheet.Rows[0].SetValues(new object[] { "Row1-Col1", "Row1-Col2", "Row1-Col3" });

        // Update an entire column with new values
        sheet.Columns[0].SetValues(new object[] { "Col1-Row1", "Col1-Row2", "Col1-Row3" });

        // Apply dynamic values to a series of cells
        for (int i = 0; i < 10; i++)
        {
            sheet[$"B{i + 1}"].Value = $"Dynamic{i}";
        }

        // Use the FindAndReplace feature for bulk updates
        sheet["B1:B10"].FindAndReplace("Dynamic", "Replaced");

        // Remove unwanted rows
        sheet.DeleteRow(5); // Deletes the 6th row (0-indexed)

        // Remove unwanted worksheets
        workbook.WorkSheets.Remove(sheet); // Removes the accessed sheet from the workbook

        // Save changes to the workbook
        workbook.SaveAs("updated_example.xlsx");

        Console.WriteLine("Excel file has been updated successfully!");
    }
}
' Import the necessary libraries
Imports IronXL
Imports System

Friend Class ExcelEditor
	Shared Sub Main()
		' Load the Excel workbook
		Dim workbook As WorkBook = WorkBook.Load("example.xlsx")

		' Access the first worksheet of the workbook
		Dim sheet As WorkSheet = workbook.WorkSheets.First()

		' Modify a specific cell value
		sheet("A1").Value = "Updated Value"

		' Update an entire row with new values
		sheet.Rows(0).SetValues(New Object() { "Row1-Col1", "Row1-Col2", "Row1-Col3" })

		' Update an entire column with new values
		sheet.Columns(0).SetValues(New Object() { "Col1-Row1", "Col1-Row2", "Col1-Row3" })

		' Apply dynamic values to a series of cells
		For i As Integer = 0 To 9
			sheet($"B{i + 1}").Value = $"Dynamic{i}"
		Next i

		' Use the FindAndReplace feature for bulk updates
		sheet("B1:B10").FindAndReplace("Dynamic", "Replaced")

		' Remove unwanted rows
		sheet.DeleteRow(5) ' Deletes the 6th row (0-indexed)

		' Remove unwanted worksheets
		workbook.WorkSheets.Remove(sheet) ' Removes the accessed sheet from the workbook

		' Save changes to the workbook
		workbook.SaveAs("updated_example.xlsx")

		Console.WriteLine("Excel file has been updated successfully!")
	End Sub
End Class
$vbLabelText   $csharpLabel
Regan Pun
Software Engineer
Regan graduated from the University of Reading, with a BA in Electronic Engineering. Before joining Iron Software, his previous job roles had him laser-focused on single tasks; and what he most enjoys at Iron Software is the spectrum of work he gets to undertake, whether it’s adding value to sales, technical support, product development or marketing. He enjoys understanding the way developers are using the Iron Software library, and using that knowledge to continually improve documentation and develop the products.
< PREVIOUS
How to Create and Edit Excel Charts in C#
NEXT >
How to Create New Spreadsheets in C#