We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. (Last updated on: November 16, 2018).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

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

Platform: WinForms |
Control: XlsIO |
Published Date: April 30, 2011 |
Last Revised Date: June 13, 2019

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.

 

2X faster development

The ultimate WinForms UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon