Insert Excel rows and columns in C#, 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. Using this library, you can insert Excel rows and columns in C#, VB.NET.
‘InsertRow’ and ‘InsertColumn’ methods are designed to work if the row index or column index falls within the used range of the worksheet. Also, these methods take input in one based index, and hence to insert a column at ‘A’, you must use worksheet.InsertColumn(1);
As per Microsoft Excel behavior,
- the merged formats of the previous or next row/column to the inserted one, won’t be copied.
- the styles and borders applied to the previous row/column will be updated.
- formulas won’t be updated.
Steps to insert Excel rows and columns, programmatically:
Step 1: Create a new C# console application project.
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
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 rows and columns 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("InsertRowsandColumnsTemplate.xlsx", ExcelOpenType.Automatic); //The first worksheet object in the worksheets collection is accessed IWorksheet worksheet = workbook.Worksheets[0]; //Inserting Rows worksheet.InsertRow(3); worksheet.InsertRow(6); worksheet.InsertRow(8); //Inserting Columns worksheet.InsertColumn(2); worksheet.InsertColumn(3); worksheet.InsertColumn(4); //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("InsertRowsandColumnsTemplate.xlsx", ExcelOpenType.Automatic) 'The first worksheet object in the worksheets collection is accessed Dim worksheet As IWorksheet = workbook.Worksheets(0) 'Inserting Rows worksheet.InsertRow(3) worksheet.InsertRow(6) worksheet.InsertRow(8) 'Inserting Columns worksheet.InsertColumn(2) worksheet.InsertColumn(3) worksheet.InsertColumn(4) 'Save the workbook workbook.SaveAs("Output.xlsx") End Using
A complete Windows Forms working example of how to insert Excel rows and columns in C# and VB can be downloaded from Insert Excel Rows And Columns.zip.
By executing the program, you will get the output Excel file as shown below.
Output Excel document
Refer here to explore the rich set of Syncfusion Excel (XlsIO) library features.
See Also:
How do I insert a row after last row?
Why formulas are not updated when inserting rows?
How do I delete a bunch of rows or columns?
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?
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.