Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Insert and Delete Rows or Columns in Excel using C#
// Instantiating a Workbook object
// Opening the Excel file through the file stream
Workbook workbook = new Workbook(@"C:\Files\Book1.xlsx");
// Accessing the first worksheet in the Excel file
Worksheet worksheet = workbook.Worksheets[0];
// Deleting a 3rd column from the worksheet
worksheet.Cells.DeleteColumn(2);
// Saving the modified Excel file
workbook.Save(@"C:\Files\output.xlsx");
// Instantiating a Workbook object
// Opening the Excel file through the file stream
Workbook workbook = new Workbook(@"C:\Files\Book1.xlsx");
// Accessing the first worksheet in the Excel file
Worksheet worksheet = workbook.Worksheets[0];
// Deleting 5 columns from the worksheet starting from 3rd column
worksheet.Cells.DeleteColumns(2, 5, false);
// Saving the modified Excel file
workbook.Save(@"C:\Files\output.xlsx");
// Instantiating a Workbook object
// Opening the Excel file through the file stream
Workbook workbook = new Workbook(@"C:\Files\Book1.xlsx");
// Accessing the first worksheet in the Excel file
Worksheet worksheet = workbook.Worksheets[0];
// Deleting 10 rows into the worksheet starting from 3rd row
worksheet.Cells.DeleteRows(2, 10);
// Saving the modified Excel file
workbook.Save(@"C:\Files\output.xlsx");
// Instantiating a Workbook object
// Opening the Excel file through the file stream
Workbook workbook = new Workbook(@"C:\Files\Book1.xlsx");
// Accessing the first worksheet in the Excel file
Worksheet worksheet = workbook.Worksheets[0];
// Inserting a column into the worksheet at 2nd position
worksheet.Cells.InsertColumn(1);
// Saving the modified Excel file
workbook.Save(@"C:\Files\output.xlsx");
// Instantiating a Workbook object
// Opening the Excel file through the file stream
Workbook workbook = new Workbook(@"C:\Files\Book1.xlsx");
// Accessing the first worksheet in the Excel file
Worksheet worksheet = workbook.Worksheets[0];
// Inserting 5 column into the worksheet starting at 2nd position
worksheet.Cells.InsertColumns(2, 5);
// Saving the modified Excel file
workbook.Save(@"C:\Files\output.xlsx");
// Instantiating a Workbook object
// Opening the Excel file through the file stream
Workbook workbook = new Workbook(@"C:\Files\Book1.xlsx");
// Accessing the first worksheet in the Excel file
Worksheet worksheet = workbook.Worksheets[0];
// Inserting a row into the worksheet at 3rd position
worksheet.Cells.InsertRow(2);
// Saving the modified Excel file
workbook.Save(@"C:\Files\output.xlsx");
// Instantiating a Workbook object
// Opening the Excel file through the file stream
Workbook workbook = new Workbook(@"C:\Files\Book1.xlsx");
// Accessing the first worksheet in the Excel file
Worksheet worksheet = workbook.Worksheets[0];
// Inserting 10 rows into the worksheet starting from 3rd row
worksheet.Cells.InsertRows(2, 10);
// Saving the modified Excel file
workbook.Save(@"C:\Files\output.xlsx");
// Instantiating a Workbook object
// Opening the Excel file through the file stream
Workbook workbook = new Workbook(@"C:\Files\Book1.xlsx");
// Accessing the first worksheet in the Excel file
Worksheet worksheet = workbook.Worksheets[0];
// Setting Formatting options
InsertOptions insertOptions = new InsertOptions();
insertOptions.CopyFormatType = CopyFormatType.SameAsAbove;
// Inserting a row into the worksheet at 3rd position
worksheet.Cells.InsertRows(2, 1, insertOptions);
// Saving the modified Excel file
workbook.Save(@"C:\Files\output.xlsx");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment