How to Write to Excel Files in .NET with C#

In this tutorial, we delve into the process of writing and modifying Excel files using C# with the help of the IronXL library. The session starts by installing IronXL via the NuGet package manager and opening the Program.cs file. We begin by loading an Excel workbook, 'sample.xlsx', and creating a worksheet object. The tutorial demonstrates how to write new values to cell A1, save the workbook as 'sample1.xlsx', and write values to specified ranges such as B2 to B9 and from C3 to B7, saving the result as 'sample2.xlsx'. It further explores dynamic value insertion using a for loop across a specified range of rows, saving the file as 'sample3.xlsx'. Text replacement is also covered, showcasing how to replace text throughout the worksheet and in specific areas, resulting in 'sample4.xlsx'. The tutorial concludes with a demonstration of the output files, encouraging users to visit the IronXL website for advanced features and to explore IronPDF for more functionality. Viewers are invited to subscribe for more tutorials and to download the software to experience its capabilities.

Code Example

Below are the code examples demonstrating the discussed functionalities.

// Ensure you have installed IronXL via NuGet package manager before running this code.
using IronXL;

namespace ExcelTutorial
{
    class Program
    {
        static void Main(string[] args)
        {
            // Step 1: Load the Excel workbook
            WorkBook workbook = WorkBook.Load("sample.xlsx");
            WorkSheet sheet = workbook.DefaultWorkSheet;

            // Step 2: Write a value to cell A1
            sheet["A1"].Value = "Hello, Excel!";
            workbook.SaveAs("sample1.xlsx");

            // Step 3: Write values to a specified range B2 to B9
            for (int i = 2; i <= 9; i++)
            {
                sheet[$"B{i}"].Value = $"Value {i}";
            }
            workbook.SaveAs("sample2.xlsx");

            // Step 4: Dynamic value insertion using a for loop across a specified range C3 to C7
            for (int i = 3; i <= 7; i++)
            {
                sheet[$"C{i}"].Value = i * 10; // Example: set cell C3 to C7 with multiples of 10
            }
            workbook.SaveAs("sample3.xlsx");

            // Step 5: Replace all occurrences of a specific text throughout the worksheet
            sheet.ReplaceText("Hello", "Hi");
            workbook.SaveAs("sample4.xlsx");

            // Additional dynamic text replacement in specific range, if needed
            // (Assume we want to replace 'Value' to 'Data' from cells B2 to B9)
            for (int i = 2; i <= 9; i++)
            {
                if (sheet[$"B{i}"].Value.ToString().Contains("Value"))
                {
                    sheet[$"B{i}"].Value = sheet[$"B{i}"].Value.ToString().Replace("Value", "Data");
                }
            }
            // Save again if desired after this operation
            // workbook.SaveAs("sample5.xlsx");
        }
    }
}
// Ensure you have installed IronXL via NuGet package manager before running this code.
using IronXL;

namespace ExcelTutorial
{
    class Program
    {
        static void Main(string[] args)
        {
            // Step 1: Load the Excel workbook
            WorkBook workbook = WorkBook.Load("sample.xlsx");
            WorkSheet sheet = workbook.DefaultWorkSheet;

            // Step 2: Write a value to cell A1
            sheet["A1"].Value = "Hello, Excel!";
            workbook.SaveAs("sample1.xlsx");

            // Step 3: Write values to a specified range B2 to B9
            for (int i = 2; i <= 9; i++)
            {
                sheet[$"B{i}"].Value = $"Value {i}";
            }
            workbook.SaveAs("sample2.xlsx");

            // Step 4: Dynamic value insertion using a for loop across a specified range C3 to C7
            for (int i = 3; i <= 7; i++)
            {
                sheet[$"C{i}"].Value = i * 10; // Example: set cell C3 to C7 with multiples of 10
            }
            workbook.SaveAs("sample3.xlsx");

            // Step 5: Replace all occurrences of a specific text throughout the worksheet
            sheet.ReplaceText("Hello", "Hi");
            workbook.SaveAs("sample4.xlsx");

            // Additional dynamic text replacement in specific range, if needed
            // (Assume we want to replace 'Value' to 'Data' from cells B2 to B9)
            for (int i = 2; i <= 9; i++)
            {
                if (sheet[$"B{i}"].Value.ToString().Contains("Value"))
                {
                    sheet[$"B{i}"].Value = sheet[$"B{i}"].Value.ToString().Replace("Value", "Data");
                }
            }
            // Save again if desired after this operation
            // workbook.SaveAs("sample5.xlsx");
        }
    }
}
' Ensure you have installed IronXL via NuGet package manager before running this code.
Imports IronXL

Namespace ExcelTutorial
	Friend Class Program
		Shared Sub Main(ByVal args() As String)
			' Step 1: Load the Excel workbook
			Dim workbook As WorkBook = WorkBook.Load("sample.xlsx")
			Dim sheet As WorkSheet = workbook.DefaultWorkSheet

			' Step 2: Write a value to cell A1
			sheet("A1").Value = "Hello, Excel!"
			workbook.SaveAs("sample1.xlsx")

			' Step 3: Write values to a specified range B2 to B9
			For i As Integer = 2 To 9
				sheet($"B{i}").Value = $"Value {i}"
			Next i
			workbook.SaveAs("sample2.xlsx")

			' Step 4: Dynamic value insertion using a for loop across a specified range C3 to C7
			For i As Integer = 3 To 7
				sheet($"C{i}").Value = i * 10 ' Example: set cell C3 to C7 with multiples of 10
			Next i
			workbook.SaveAs("sample3.xlsx")

			' Step 5: Replace all occurrences of a specific text throughout the worksheet
			sheet.ReplaceText("Hello", "Hi")
			workbook.SaveAs("sample4.xlsx")

			' Additional dynamic text replacement in specific range, if needed
			' (Assume we want to replace 'Value' to 'Data' from cells B2 to B9)
			For i As Integer = 2 To 9
				If sheet($"B{i}").Value.ToString().Contains("Value") Then
					sheet($"B{i}").Value = sheet($"B{i}").Value.ToString().Replace("Value", "Data")
				End If
			Next i
			' Save again if desired after this operation
			' workbook.SaveAs("sample5.xlsx");
		End Sub
	End Class
End Namespace
$vbLabelText   $csharpLabel

Further Reading: Write Excel .NET Functions

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 an Excel File in C# using IronXL
NEXT >
How to Create and Read Excel Files in VB .NET

Report an Issue