How to preserve the merged cells format in Excel using C# and VB.NET?
Syncfusion Excel (XlsIO) library is a .NET Excel library used to create, read, and edit Excel documents. Also, converts Excel documents to PDF files.
This article explains how to insert a new row with the previous row’s format in Excel when the row contains merged cells using
Include the following namespace in the Program.cs file.
C#
using Syncfusion.XlsIO;
VB.NET
Imports Syncfusion.XlsIO
Use the following code snippet to insert new row with previous row format in Excel while the row contains merged cells.
C#
using (ExcelEngine excelEngine = new ExcelEngine())
{
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Xlsx;
IWorkbook workbook = application.Workbooks.Open("../../../Data/Sample.xlsx");
IWorksheet worksheet = workbook.Worksheets[0];
//Insert rows with formatting preserved
InsertWithMerge(3, 2, worksheet);
//Save the result
workbook.SaveAs("../../../Output/Output.xlsx");
}
//<summary>
//Inserts rows at the specified position and preserves formatting.
//</summary>
//<param name="rowIndex">Starting row index for insertion (1-based).</param>
//<param name="rowCount">Number of rows to insert.</param>
//<param name="worksheet">Target worksheet.</param>
public static void InsertWithMerge(int rowIndex, int rowCount, IWorksheet worksheet)
{
while (rowCount > 0)
{
if (rowIndex > 1)
{
//Insert a row and copy the formatting from the row above
worksheet.InsertRow(rowIndex, 1, ExcelInsertOptions.FormatAsBefore);
IRange usedRange = worksheet.UsedRange;
//Copy row formatting down
worksheet[rowIndex - 1, usedRange.Column, rowIndex - 1, usedRange.LastColumn].CopyTo(worksheet["A" + rowIndex]);
//Clear the content (not the style) of the newly inserted row
worksheet[rowIndex, usedRange.Column, rowIndex, usedRange.LastColumn].Clear(ExcelClearOptions.ClearContent);
}
else
//If inserting at the first row, just insert with previous formatting
worksheet.InsertRow(rowIndex, 1, ExcelInsertOptions.FormatAsBefore);
rowCount--;
}
}
VB.NET
Using excelEngine As New ExcelEngine()
Dim application As IApplication = excelEngine.Excel
application.DefaultVersion = ExcelVersion.Xlsx
Dim workbook As IWorkbook = application.Workbooks.Open("../../Data/Sample.xlsx")
Dim worksheet As IWorksheet = workbook.Worksheets(0)
'Insert rows with formatting preserved
InsertWithMerge(3, 2, worksheet)
'Save the result
workbook.SaveAs("../../Output/Output.xlsx")
End Using
''' <summary>
''' Inserts rows at the specified position and preserves formatting.
''' </summary>
''' <param name="rowIndex">Starting row index for insertion (1-based).</param>
''' <param name="rowCount">Number of rows to insert.</param>
''' <param name="worksheet">Target worksheet.</param>
Public Sub InsertWithMerge(ByVal rowIndex As Integer,
ByVal rowCount As Integer,
ByVal worksheet As IWorksheet)
Do While rowCount > 0
If rowIndex > 1 Then
'Insert a row and copy the formatting from the row above
worksheet.InsertRow(rowIndex, 1, ExcelInsertOptions.FormatAsBefore)
Dim usedRange As IRange = worksheet.UsedRange
'Copy row formatting down
worksheet(rowIndex - 1, usedRange.Column,
rowIndex - 1, usedRange.LastColumn) _
.CopyTo(worksheet("A" & rowIndex.ToString()))
'Clear the content (not the style) of the newly inserted row
worksheet(rowIndex, usedRange.Column,
rowIndex, usedRange.LastColumn) _
.Clear(ExcelClearOptions.ClearContent)
Else
'If inserting at the first row, just insert with previous formatting
worksheet.InsertRow(rowIndex, 1, ExcelInsertOptions.FormatAsBefore)
End If
rowCount -= 1
Loop
End Sub
You can get the complete sample for inserting a new row with the previous row’s format in Excel while the row contains merged cells from
Take a moment to peruse the documentation where you can find basic Excel document processing options along with the features like import and export data, chart, formulas, conditional formatting, data validation, tables, pivot tables and, protect the Excel documents, and most importantly, the PDF, CSV and Image conversions with code examples.
Conclusion
I hope you enjoyed learning about how to insert a new row with the previous row’s format in Excel while the row contains merged cells using
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 forums, Support Tickets, or feedback portal. We are always happy to assist you!