C# Excel Add-in (Code Example Tutorial)
When developing applications, you need to be able to manage data without Excel spreadsheets but still communicate with the files. For example, you may need to insert new rows or columns in an existing Excel Spreadsheet programmatically. Using the functions for the umbrella concept "Excel: Add" in C#, we can add all these and more. See the examples below.
How to USE Excel Add in C#
- Download and install Excel Add C# Library
- Add new rows and columns to existing Excel spreadsheet in C#
- Insert rows to the Last and First position
- Add columns utilizing C# functions
- Export complete Excel file in various types to desired location
Step 1
1. Download the IronXL Excel Library
To access the functions to add rows and columns in Excel, first, we need to download the IronXL Excel Library. This is free for development in your project. Download the DLL directly or you can use the NuGet install method.
Install-Package IronXL.Excel
How to Tutorial
2. Excel Add Row in C#
Now that we have installed IronXL, it will be easy to insert new rows and columns in existing Excel Spreadsheets using C# programming.
First, access your Excel SpreadSheet and specify the WorkSheet where new rows or columns need to be added.
2.1. Add Row in Last Position
In our first example, we'll show how to add a new row in the last position.
Suppose that our Excel file is named sample.xlsx
, and it has 5
columns from A
to E
. Using the following method, we can add a new row:
// Add a New Row in Last Position
using IronXL;
class Program
{
static void Main(string[] args)
{
// Load the existing Excel workbook
WorkBook wb = WorkBook.Load("sample.xlsx");
// Get the specific worksheet
WorkSheet ws = wb.GetWorkSheet("Sheet1");
// Calculate the position for the new row
int i = ws.Rows.Count + 1;
// Set values for each cell in the new row
ws["A" + i].Value = "New Row";
ws["B" + i].Value = "New Row";
ws["C" + i].Value = "New Row";
ws["D" + i].Value = "New Row";
ws["E" + i].Value = "New Row";
// Save changes
wb.SaveAs("sample.xlsx");
}
}
// Add a New Row in Last Position
using IronXL;
class Program
{
static void Main(string[] args)
{
// Load the existing Excel workbook
WorkBook wb = WorkBook.Load("sample.xlsx");
// Get the specific worksheet
WorkSheet ws = wb.GetWorkSheet("Sheet1");
// Calculate the position for the new row
int i = ws.Rows.Count + 1;
// Set values for each cell in the new row
ws["A" + i].Value = "New Row";
ws["B" + i].Value = "New Row";
ws["C" + i].Value = "New Row";
ws["D" + i].Value = "New Row";
ws["E" + i].Value = "New Row";
// Save changes
wb.SaveAs("sample.xlsx");
}
}
' Add a New Row in Last Position
Imports IronXL
Friend Class Program
Shared Sub Main(ByVal args() As String)
' Load the existing Excel workbook
Dim wb As WorkBook = WorkBook.Load("sample.xlsx")
' Get the specific worksheet
Dim ws As WorkSheet = wb.GetWorkSheet("Sheet1")
' Calculate the position for the new row
Dim i As Integer = ws.Rows.Count + 1
' Set values for each cell in the new row
ws("A" & i).Value = "New Row"
ws("B" & i).Value = "New Row"
ws("C" & i).Value = "New Row"
ws("D" & i).Value = "New Row"
ws("E" & i).Value = "New Row"
' Save changes
wb.SaveAs("sample.xlsx")
End Sub
End Class
This will create a new row in the last position with the value New Row
in the Excel Spreadsheet sample.xlsx
.
2.2. Add Row in First Position
It is also very simple to add a new row in the first position of the Excel Spreadsheet.
For this purpose, firstly we need to move all existing rows down to make room at the top for the new row.
Here's the example on how to add a new row in the first position:
// Add a New Row in First Position
using IronXL;
class Program
{
static void Main(string[] args)
{
// Load the existing Excel workbook
WorkBook wb = WorkBook.Load("sample.xlsx");
// Get the specific worksheet
WorkSheet ws = wb.GetWorkSheet("Sheet1");
// Loop through the rows to shift them down
for (int j = ws.Rows.Count; j >= 1; j--)
{
// Move each row down by one position
ws["A" + (j + 1)].Value = ws["A" + j].Value;
ws["B" + (j + 1)].Value = ws["B" + j].Value;
ws["C" + (j + 1)].Value = ws["C" + j].Value;
ws["D" + (j + 1)].Value = ws["D" + j].Value;
ws["E" + (j + 1)].Value = ws["E" + j].Value;
}
// Add a new row at the first position
ws["A1"].Value = "New First Row";
ws["B1"].Value = "New First Row";
ws["C1"].Value = "New First Row";
ws["D1"].Value = "New First Row";
ws["E1"].Value = "New First Row";
// Save changes
wb.SaveAs("sample.xlsx");
}
}
// Add a New Row in First Position
using IronXL;
class Program
{
static void Main(string[] args)
{
// Load the existing Excel workbook
WorkBook wb = WorkBook.Load("sample.xlsx");
// Get the specific worksheet
WorkSheet ws = wb.GetWorkSheet("Sheet1");
// Loop through the rows to shift them down
for (int j = ws.Rows.Count; j >= 1; j--)
{
// Move each row down by one position
ws["A" + (j + 1)].Value = ws["A" + j].Value;
ws["B" + (j + 1)].Value = ws["B" + j].Value;
ws["C" + (j + 1)].Value = ws["C" + j].Value;
ws["D" + (j + 1)].Value = ws["D" + j].Value;
ws["E" + (j + 1)].Value = ws["E" + j].Value;
}
// Add a new row at the first position
ws["A1"].Value = "New First Row";
ws["B1"].Value = "New First Row";
ws["C1"].Value = "New First Row";
ws["D1"].Value = "New First Row";
ws["E1"].Value = "New First Row";
// Save changes
wb.SaveAs("sample.xlsx");
}
}
' Add a New Row in First Position
Imports IronXL
Friend Class Program
Shared Sub Main(ByVal args() As String)
' Load the existing Excel workbook
Dim wb As WorkBook = WorkBook.Load("sample.xlsx")
' Get the specific worksheet
Dim ws As WorkSheet = wb.GetWorkSheet("Sheet1")
' Loop through the rows to shift them down
For j As Integer = ws.Rows.Count To 1 Step -1
' Move each row down by one position
ws("A" & (j + 1)).Value = ws("A" & j).Value
ws("B" & (j + 1)).Value = ws("B" & j).Value
ws("C" & (j + 1)).Value = ws("C" & j).Value
ws("D" & (j + 1)).Value = ws("D" & j).Value
ws("E" & (j + 1)).Value = ws("E" & j).Value
Next j
' Add a new row at the first position
ws("A1").Value = "New First Row"
ws("B1").Value = "New First Row"
ws("C1").Value = "New First Row"
ws("D1").Value = "New First Row"
ws("E1").Value = "New First Row"
' Save changes
wb.SaveAs("sample.xlsx")
End Sub
End Class
Let's compare the changes done by the code in our sample.xlsx
document.
Before | After |
---|---|
![]() | ![]() |
Here we can see that before, the sample.xlsx
file had 10
rows. With the new row added in the first position, it now has 11
rows.
2.3. Add Row in First with Column Header
In cases where the first row includes column headers, you can start adding data from the second row and retain headers on row 0
.
3. Excel Add Column in C#
It is also possible to add a new column to an existing Excel Spreadsheet in our C# project.
Suppose that we have 5
columns in our Excel Spreadsheet called sample.xlsx
, ranging from A to E, and we want to add a new column in the first position, A. See the code sample below:
// Add a New Column
using IronXL;
class Program
{
static void Main(string[] args)
{
// Load the existing Excel workbook
WorkBook wb = WorkBook.Load("sample.xlsx");
// Get the specific worksheet
WorkSheet ws = wb.GetWorkSheet("Sheet1");
// Loop through each row to move existing columns to the right
for (int i = 1; i <= ws.Rows.Count; i++)
{
ws["F" + i].Value = ws["E" + i].Value;
ws["E" + i].Value = ws["D" + i].Value;
ws["D" + i].Value = ws["C" + i].Value;
ws["C" + i].Value = ws["B" + i].Value;
ws["B" + i].Value = ws["A" + i].Value;
ws["A" + i].Value = "New Column Added";
}
// Save changes
wb.SaveAs("sample.xlsx");
}
}
// Add a New Column
using IronXL;
class Program
{
static void Main(string[] args)
{
// Load the existing Excel workbook
WorkBook wb = WorkBook.Load("sample.xlsx");
// Get the specific worksheet
WorkSheet ws = wb.GetWorkSheet("Sheet1");
// Loop through each row to move existing columns to the right
for (int i = 1; i <= ws.Rows.Count; i++)
{
ws["F" + i].Value = ws["E" + i].Value;
ws["E" + i].Value = ws["D" + i].Value;
ws["D" + i].Value = ws["C" + i].Value;
ws["C" + i].Value = ws["B" + i].Value;
ws["B" + i].Value = ws["A" + i].Value;
ws["A" + i].Value = "New Column Added";
}
// Save changes
wb.SaveAs("sample.xlsx");
}
}
' Add a New Column
Imports IronXL
Friend Class Program
Shared Sub Main(ByVal args() As String)
' Load the existing Excel workbook
Dim wb As WorkBook = WorkBook.Load("sample.xlsx")
' Get the specific worksheet
Dim ws As WorkSheet = wb.GetWorkSheet("Sheet1")
' Loop through each row to move existing columns to the right
For i As Integer = 1 To ws.Rows.Count
ws("F" & i).Value = ws("E" & i).Value
ws("E" & i).Value = ws("D" & i).Value
ws("D" & i).Value = ws("C" & i).Value
ws("C" & i).Value = ws("B" & i).Value
ws("B" & i).Value = ws("A" & i).Value
ws("A" & i).Value = "New Column Added"
Next i
' Save changes
wb.SaveAs("sample.xlsx")
End Sub
End Class
Let's view the changes done by the code in sample.xlsx
:
Before | After |
---|---|
![]() | ![]() |
We can see that a new column was added in first position A, and the rest were pushed back, resulting in a total of 6 columns from A to F.
Library Quick Access
Read the IronXL Documentation
Read the documentation for IronXL for more functions and information on adding rows, columns, and other Excel C# functionality.
Read the IronXL DocumentationFrequently Asked Questions
What is the purpose of the C# Excel Add-in tutorial?
The tutorial aims to guide developers on how to programmatically manage and manipulate Excel spreadsheets using C#, specifically focusing on adding rows and columns without using Interop.
How can I install the IronXL Excel Library in my C# project?
You can install the IronXL Excel Library by downloading the DLL directly from the provided link or using the NuGet install method with the command 'Install-Package IronXL.Excel'.
How do I add a new row to the last position in an Excel spreadsheet using C#?
Load the existing Excel workbook using IronXL, access the specific worksheet, calculate the position for the new row, set values for each cell in the new row, and save the changes to the workbook.
Can I add a new row to the first position of an Excel spreadsheet in C#?
Yes, you can add a new row to the first position by moving all existing rows down by one position and then inserting the new row at the top.
How can I add a new column to an Excel spreadsheet using C#?
To add a new column, load the existing workbook, access the worksheet, and loop through each row to move existing columns to the right. Then, set values for the new column and save the workbook.
What is the IronXL library used for?
IronXL is a C# library used for reading, creating, and manipulating Excel files without the need for Excel Interop, providing a simplified way to work with Excel spreadsheets in C#.
Is IronXL free for development use?
Yes, IronXL is free for development purposes, allowing developers to integrate Excel functionalities into their C# projects without cost during development.
Where can I find more documentation on IronXL?
You can read more about IronXL and its functionalities by accessing the IronXL documentation available on their website, which provides detailed information and examples.
Does IronXL support exporting Excel files in different formats?
Yes, IronXL allows exporting complete Excel files in various formats to a desired location, enhancing flexibility in data management.
Can IronXL handle Excel files with column headers?
IronXL can manage Excel files with column headers, allowing you to add data from the second row onwards while retaining headers on the first row.