Skip to content

Instantly share code, notes, and snippets.

@conholdate-gists
Last active November 15, 2021 07:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save conholdate-gists/1edeb7f514e5f836a74b1c7f17c925b6 to your computer and use it in GitHub Desktop.
Save conholdate-gists/1edeb7f514e5f836a74b1c7f17c925b6 to your computer and use it in GitHub Desktop.
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