Articles in this section
Category / Section

Insert new row with previous row format in Excel using C#

2 mins read

Syncfusion Excel (XlsIO) library is a .NET Excel library used to create, read, and edit Excel documents. Also, converts Excel documents to PDF files. Using this library, you can insert Excel row with previous row format in C#, VB.NET.

‘InsertRow’ method has three overloads as below.

  1. InsertRow(int index)
  2. InsertRow(int rowIndex, int rowCount)
  3. InsertRow(int rowIndex, int rowCount, ExcelInsertOptions insertOptions)

These methods take input in one based index. In order to insert Excel row with previous row format, use FormatAsBefore option in ExcelInsertOptions as below.

InsertRow(4, 2, ExcelInsertOptions.FormatAsBefore);

 

The above statement will insert two Excel rows after the 4th row with the same formatting as in 4th row. Similarly, if you want to inset Excel row with next row format, FormatAsAfter option can be used.

Steps to insert Excel row with previous row format, programmatically:

Step 1: Create a new C# console application project.

Create C# console application

Create a new C# console application

Step 2: Install the Syncfusion.XlsIO.WinForms NuGet package as reference to your .NET Framework application from NuGet.org.

Install NuGet package

Install NuGet package

Step 3: Include the following namespace in the Program.cs file.

C#

using Syncfusion.XlsIO;

 

VB.NET

Imports Syncfusion.XlsIO

 

Step 4: Use the following code snippet to insert Excel row with previous row format in C#, VB.NET.

C#

using (ExcelEngine excelEngine = new ExcelEngine())
{
    //Instantiate the application object
    IApplication application = excelEngine.Excel;
 
    //Add a workbook
    IWorkbook workbook = application.Workbooks.Open("InputTemplate.xlsx", ExcelOpenType.Automatic);
 
    //The first worksheet object in the worksheets collection is accessed
    IWorksheet worksheet = workbook.Worksheets[0];
 
    //Insert Excel row with previous row format
    worksheet.InsertRow(11, 3, ExcelInsertOptions.FormatAsBefore);
                                
    //Save the workbook
    workbook.SaveAs("Output.xlsx");
}

 

VB.NET

Using excelEngine As ExcelEngine = New ExcelEngine()
    'Instantiate the application object
    Dim application As IApplication = excelEngine.Excel
 
    'Add a workbook
    Dim workbook As IWorkbook = application.Workbooks.Open("InputTemplate.xlsx", ExcelOpenType.Automatic)
 
    'The first worksheet object in the worksheets collection is accessed
    Dim worksheet As IWorksheet = workbook.Worksheets(0)
 
    'Insert Excel row with previous row format
    worksheet.InsertRow(11, 3, ExcelInsertOptions.FormatAsBefore)
 
    'Save the workbook
    workbook.SaveAs("Output.xlsx")
End Using

 

A complete Windows Forms working example of how to insert Excel row with previous row format in C#, VB.NET can be downloaded from Insert Excel Row With Previous Row Format.zip.

By executing the program, you will get the output Excel document as shown below.

Output Excel document

Output Excel document

Refer here to explore the rich set of Syncfusion Excel (XlsIO) library features.

See Also:

Insert Excel rows and columns in C#, VB.NET

Why formulas are not updated when inserting rows?

How can we format a cell in Accounting format type of MS Excel?

How to copy and insert a chart in the same worksheet using C#, VB.NET?

How do I insert watermark in MS Excel?

How to insert an Hyperlink in a cell?

Note:

Starting with v16.2.0.x, if you reference Syncfusion assemblies from trial setup or from the NuGet feed, include a license key in your projects. Refer the link to learn about generating and registering Syncfusion license key in your application to use the components without trail message.

 

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied