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. It also converts Excel documents to PDF files. Using this library, you can insert Excel rows with the previous row format in C# and VB.NET.

‘InsertRow’ method has three overloads as shown below:

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

These methods take input in a one-based index. In order to insert an Excel row with the previous row format, use the FormatAsBefore option in ExcelInsertOptions as shown 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 a reference to your .NET Framework application from NuGet.org.

Install NuGet package

Install the 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 rows with the 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 rows with the 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 rows with the previous row format in C# and 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 a trial setup or from the NuGet feed, include a license key in your projects. Refer to the link to learn about generating and registering the Syncfusion® license key in your application to use the components without a trial message.

 

Conclusion

I hope you enjoyed learning about Insert new row with previous row format in Excel using C#.

You can refer to our XIsIO’s feature tour page to learn about its other groundbreaking features. Explore our UG documentation and online demos to understand how to manipulate data in Excel documents.

If you are an existing user, you can access our latest components from the License and Downloads page. For new users, you can try our 30-day free trial to check out XlsIO and other Syncfusion® components.

If you have any queries or require clarification, please let us know in the comments below or contact us through our support forumsSupport Tickets, or feedback portal. We are always happy to assist you!

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