Write Excel .NET Functions in C# with IronXL
Write Excel data in C# using IronXL by accessing worksheets, setting cell values with simple bracket notation, and saving changes. The library supports writing to specific cells, ranges, and replacing values programmatically without complex code.
C# applications often need to update files and write new data in Excel spreadsheets programmatically. Excel .NET capabilities can sometimes be complicated, but using the IronXL library, this task is straightforward and allows working with Excel spreadsheets in any format. Access specific cells directly and assign custom values with minimal code. Whether you're creating new spreadsheets or editing existing Excel files, IronXL provides intuitive methods for all your Excel automation needs.
Quickstart: Write a Value to a Specific Cell
This example shows how to write a value into a single cell using IronXL and save the Excel file in just a few lines.
Get started making PDFs with NuGet now:
Install IronXL with NuGet Package Manager
Copy and run this code snippet.
worksheet["A1"].Value = "Hello, IronXL!"; workbook.SaveAs("output.xlsx");Deploy to test on your live environment
Write Excel .NET Instructions
- Download the Library for Excel .NET
- Write values in specific cells
- Write static values in multiple cells
- Write dynamic values in cell range
- Replace cell values in specific row, column, range, and more
How Do I Access Excel Files?
Start by accessing the Excel file where you want to write data. Open the Excel file in your project, then open its specific worksheet using the following code.
:path=/static-assets/excel/content-code-examples/how-to/write-excel-net-load-file.cs// Load Excel file in the project
WorkBook workBook = WorkBook.Load("path");' Load Excel file in the project
Dim workBook As WorkBook = WorkBook.Load("path")The above opens the specified Excel file. Next, access the worksheet.
:path=/static-assets/excel/content-code-examples/how-to/write-excel-net-get-sheet.cs// Open Excel WorkSheet
WorkSheet workSheet = workBook.GetWorkSheet("Sheet1");' Open Excel WorkSheet
Dim workSheet As WorkSheet = workBook.GetWorkSheet("Sheet1")The Excel worksheet opens in workSheet and you can use it to write any type of data in the Excel file. Learn more about how to load Excel files and access worksheets in different ways through the examples in the link. For working with multiple worksheets, check out our guide on managing worksheets which covers adding, renaming, and deleting sheets programmatically.
Note: Don't forget to add the reference to IronXL in your project and import the library by using using IronXL.
How Do I Write a Value in a Specific Cell?
You can write in an Excel file using many different methods, but the basic approach uses ExcelCell. For this purpose, any cell of the opened Excel worksheet can be accessed and a value written in it as follows:
:path=/static-assets/excel/content-code-examples/how-to/write-excel-net-assign-cell.csworkSheet["Cell Address"].Value="Assign the Value";workSheet("Cell Address").Value="Assign the Value"Here's an example of how to use the above function to write in an Excel cell in your C# project.
:path=/static-assets/excel/content-code-examples/how-to/write-excel-net-assign-cell-full.csusing IronXL;
// Load Excel file
WorkBook workBook = WorkBook.Load("sample.xlsx");
// Open WorkSheet of sample.xlsx
WorkSheet workSheet = workBook.GetWorkSheet("Sheet1");
// Access A1 cell and write the value
workSheet["A1"].Value = "new value";
// Save changes
workBook.SaveAs("sample.xlsx");Imports IronXL
' Load Excel file
Private workBook As WorkBook = WorkBook.Load("sample.xlsx")
' Open WorkSheet of sample.xlsx
Private workSheet As WorkSheet = workBook.GetWorkSheet("Sheet1")
' Access A1 cell and write the value
Private workSheet("A1").Value = "new value"
' Save changes
workBook.SaveAs("sample.xlsx")This code writes new value in cell A1 of the worksheet Sheet1 in the Excel file sample.xlsx. In the same way, you can insert values in any cell address of an Excel file. For more advanced formatting options, such as setting cell fonts and sizes or applying background colors and patterns, IronXL provides comprehensive styling capabilities.
Note: Don't forget to save the Excel file after writing new values in the worksheet, as shown in the example above.
Why Would I Force Assign Exact Values?
When setting the Value property, IronXL tries to convert it to its corresponding value type. Sometimes, this evaluation is undesirable since you want to force assign the exact value to the cell without conversion. To do this, assign the value as a string. In IronXL, simply use StringValue instead of Value to achieve the same effect.
:path=/static-assets/excel/content-code-examples/how-to/write-excel-net-assign-stringvalue.cs// Assign value as string
workSheet["A1"].StringValue = "4402-12";' Assign value as string
workSheet("A1").StringValue = "4402-12"How Do I Write Static Values in a Range?
You can write new values in multiple cells, called a range, as follows:
// Assign a static value to a range of cells
worksheet["B2:C5"].Value = "static value";// Assign a static value to a range of cells
worksheet["B2:C5"].Value = "static value";' Assign a static value to a range of cells
worksheet("B2:C5").Value = "static value"This way, you specify the range of cells From to To where the data will be written. The new value will be written in all cells within this range. To understand more about C# Excel Range, check out the examples here.
See how to write a range in action using the example below.
:path=/static-assets/excel/content-code-examples/how-to/write-excel-net-assign-cell-range-full.csusing IronXL;
// Load Excel file
WorkBook workBook = WorkBook.Load("sample.xlsx");
// Open WorkSheet of sample.xlsx
WorkSheet workSheet = workBook.GetWorkSheet("Sheet1");
// Specify range row wise and write new value
workSheet["B2:B9"].Value = "new value";
// Specify range column wise and write new value
workSheet["C3:C7"].Value = "new value";
// Save changes
workBook.SaveAs("sample.xlsx");Imports IronXL
' Load Excel file
Private workBook As WorkBook = WorkBook.Load("sample.xlsx")
' Open WorkSheet of sample.xlsx
Private workSheet As WorkSheet = workBook.GetWorkSheet("Sheet1")
' Specify range row wise and write new value
Private workSheet("B2:B9").Value = "new value"
' Specify range column wise and write new value
Private workSheet("C3:C7").Value = "new value"
' Save changes
workBook.SaveAs("sample.xlsx")This code writes new value from B2 to C5 in the worksheet Sheet1 of the Excel file sample.xlsx. It uses static values for the Excel cells.
How Do I Write Dynamic Values in a Range?
You can also add dynamic values to a range, as seen below. This is particularly useful when you need to populate data from a database or when generating reports. You might also want to explore importing and exporting as DataSet for more complex data operations.
:path=/static-assets/excel/content-code-examples/how-to/write-excel-net-assign-dynamic-value.csusing IronXL;
// Load Excel file
WorkBook workBook = WorkBook.Load("sample.xlsx");
// Open WorkSheet of sample.xlsx
WorkSheet workSheet = workBook.GetWorkSheet("Sheet1");
// Specify range in which we want to write the values
for (int i = 2; i <= 7; i++)
{
// Write the Dynamic value in one row
workSheet["B" + i].Value = "Value" + i;
// Write the Dynamic value in another row
workSheet["D" + i].Value = "Value" + i;
}
// Save changes
workBook.SaveAs("sample.xlsx");Imports IronXL
' Load Excel file
Private workBook As WorkBook = WorkBook.Load("sample.xlsx")
' Open WorkSheet of sample.xlsx
Private workSheet As WorkSheet = workBook.GetWorkSheet("Sheet1")
' Specify range in which we want to write the values
For i As Integer = 2 To 7
' Write the Dynamic value in one row
workSheet("B" & i).Value = "Value" & i
' Write the Dynamic value in another row
workSheet("D" & i).Value = "Value" & i
Next i
' Save changes
workBook.SaveAs("sample.xlsx")The above code writes dynamic values in columns B from 2 to 7 in the Excel file sample.xlsx. You can see the result of the code on sample.xlsx.
How Do I Replace Excel Cell Values?
Using IronXL, you can easily write a new value to replace the old value, using the Replace() function as follows:
:path=/static-assets/excel/content-code-examples/how-to/write-excel-net-replace.csworkSheet.Replace("old value", "new value");workSheet.Replace("old value", "new value")The above function writes new value overwriting the old value in the complete Excel worksheet.
How Do I Replace Values in a Specific Row?
If you want to write a new value only in one specific row, do as follows:
:path=/static-assets/excel/content-code-examples/how-to/write-excel-net-replace-row.csworkSheet.Rows[RowIndex].Replace("old value", "new value");workSheet.Rows(RowIndex).Replace("old value", "new value")This writes new value over the old value in only the specified row index.
How Do I Replace Values in a Specific Column?
Similarly, if you want to write new value over the old value within a specific column, do as follows:
:path=/static-assets/excel/content-code-examples/how-to/write-excel-net-replace-column.csworkSheet.Columns[ColumnIndex].Replace("old value", "new Value");workSheet.Columns(ColumnIndex).Replace("old value", "new Value")The above code writes new value to replace the old value, but only in the specified column index. The rest of the worksheet remains the same.
How Do I Replace Values in a Specific Range?
IronXL also provides a way to write a new value replacing the old value, only in a specified range. This is helpful when working with specific data regions or when you need to update values in a particular section of your spreadsheet. For more advanced range operations, see our guide on sorting cell ranges.
:path=/static-assets/excel/content-code-examples/how-to/write-excel-net-replace-range.csworkSheet["From Cell Address : To Cell Address"].Replace("old value", "new value");workSheet("From Cell Address : To Cell Address").Replace("old value", "new value")This writes new value over old value, just in the cells within the specified range.
See the example of how to use all of the above functions to write new values to replace old values in an Excel worksheet.
What Does a Complete Replace Example Look Like?
:path=/static-assets/excel/content-code-examples/how-to/write-excel-net-replace-full.csusing IronXL;
WorkBook workBook = WorkBook.Load("sample.xlsx");
WorkSheet workSheet = workBook.GetWorkSheet("Sheet1");
// Write new above old in complete WorkSheet
workSheet.Replace("old", "new");
// Write new above old just in row no 6 of WorkSheet
workSheet.Rows[5].Replace("old", "new");
// Write new above old just in column no 5 of WorkSheet
workSheet.Columns[4].Replace("old", "new");
// Write new above old just from A5 to H5 of WorkSheet
workSheet["A5:H5"].Replace("old", "new");
workBook.SaveAs("sample.xlsx");Imports IronXL
Private workBook As WorkBook = WorkBook.Load("sample.xlsx")
Private workSheet As WorkSheet = workBook.GetWorkSheet("Sheet1")
' Write new above old in complete WorkSheet
workSheet.Replace("old", "new")
' Write new above old just in row no 6 of WorkSheet
workSheet.Rows(5).Replace("old", "new")
' Write new above old just in column no 5 of WorkSheet
workSheet.Columns(4).Replace("old", "new")
' Write new above old just from A5 to H5 of WorkSheet
workSheet("A5:H5").Replace("old", "new")
workBook.SaveAs("sample.xlsx")For more information about how to write Excel .NET applications and more, check out our full tutorial on how to Open and Write Excel Files C#. If you're working with different file formats, you might also find our guide on converting spreadsheet file types helpful.
Tutorial Quick Access
Read API Reference
Read the IronXL documentation including lists of all functions, features, namespaces, classes, and enums available to you in the library.
Read API ReferenceFrequently Asked Questions
How do I write a value to a specific cell in an Excel file using C#?
With IronXL, you can write to a specific cell by simply accessing it with bracket notation and setting its Value property. For example: worksheet["A1"].Value = "Hello, IronXL!"; then save the workbook with workbook.SaveAs("output.xlsx"). This straightforward approach eliminates the need for complex Excel interop code.
What's the first step to write data to an Excel file programmatically?
The first step is to load your Excel file using IronXL's WorkBook.Load() method, then access the specific worksheet where you want to write data. IronXL makes this simple with intuitive methods that allow you to open any Excel format and immediately start working with cells and ranges.
Can I write data to multiple cells at once in Excel?
Yes, IronXL supports writing to multiple cells efficiently. You can write static values to multiple specific cells individually, or use cell ranges to write dynamic values across multiple cells programmatically. The library provides methods for both single-cell operations and bulk data writing.
How do I replace existing cell values in an Excel spreadsheet?
IronXL allows you to replace cell values by simply accessing the cell and assigning a new value to it. You can replace values in specific rows, columns, or ranges. The library automatically handles the overwriting of existing data when you assign new values to cells.
What Excel file formats are supported for writing data?
IronXL supports writing to all major Excel file formats including .xlsx, .xls, .xlsm, and CSV files. The library handles format-specific requirements automatically, allowing you to work with Excel spreadsheets in any format without changing your code approach.
Do I need Microsoft Excel installed to write Excel files in C#?
No, IronXL operates independently without requiring Microsoft Excel or Office Interop. It's a standalone .NET library that can read, write, and create Excel files programmatically on any system, making it perfect for server applications and environments where Excel isn't installed.
How do I save changes after writing data to an Excel file?
After writing data to cells, save your changes using IronXL's SaveAs() method to create a new file, or Save() to overwrite the existing file. The library ensures all your cell updates, formatting, and formulas are properly saved in the Excel file format you specify.







