Deletes a column from a worksheet.
The Delete method removes the current Column from the Worksheet.Columns collection. Other columns in the worksheet are shifted to the left. To delete more than one column, use the ColumnCollection.Remove method.
The number of columns in a worksheet is unchanged - 16,384. When you delete columns, the equivalent number of new columns is automatically added to the end of the worksheet’s column collection.
If you need to delete only the cell content or formatting from columns without removing entire columns from the worksheet, use the Clear* methods of the Worksheet object (see the How to: Clear Cells of Content, Formatting, Hyperlinks and Comments example).
This example demonstrates how to remove columns from a worksheet.
- Call the Column.Delete method to delete the current column.
- Call the ColumnCollection.Remove method of the Worksheet.Columns collection to remove a column at the specified position or delete multiple columns at once.
- To delete a column containing the specified cell or multiple columns containing the specified cell range, use the Worksheet.DeleteCells method with the DeleteMode.EntireColumn enumeration member passed as a parameter.
When you delete columns from a worksheet, other columns are automatically shifted to the left.
// Delete the 2nd column from the worksheet. worksheet.Columns.Delete(); // Delete the 3rd column from the worksheet. worksheet.Columns.Remove(2); // Delete three columns from the worksheet starting from the 10th column. worksheet.Columns.Remove(9, 3); // Delete a column that contains the "B2"cell. worksheet.DeleteCells(worksheet.Cells["B2"], DeleteMode.EntireColumn);