How to Clear Cells
Clearing cell content can be used for various purposes, such as removing unwanted or outdated data, resetting cell values, cleaning up spreadsheet layouts, preparing templates, or fixing data entry errors.
IronXL simplifies the process of clearing cell content in C# without the need for Interop.
How to Clear Cell Content
- Download the C# library for clearing cell content
- Load the existing Excel spreadsheet
- Select the range, row, or column that you want to clear
- Invoke the
ClearContents
method on the selected range - Use the
Clear
method to delete all worksheets
Install with NuGet
Install-Package IronXL.Excel
Download DLL
Manually install into your project
Install with NuGet
Install-Package IronXL.Excel
Download DLL
Manually install into your project
Start using IronPDF in your project today with a free trial.
Check out IronXL on Nuget for quick installation and deployment. With over 8 million downloads, it's transforming Excel with C#.
Install-Package IronXL.Excel
Consider installing the IronXL DLL directly. Download and manually install it for your project or GAC form: IronXL.zip
Manually install into your project
Download DLLClear a Single Cell Example
To clear the content of a selected cell, you can use the ClearContents
method.
:path=/static-assets/excel/content-code-examples/how-to/clear-cells-clear-single-cell.cs
using IronXL;
WorkBook workBook = WorkBook.Load("sample.xlsx");
WorkSheet workSheet = workBook.GetWorkSheet("Data");
// Clear cell content
workSheet["A1"].ClearContents();
workBook.SaveAs("clearSingleCell.xlsx");
Imports IronXL
Private workBook As WorkBook = WorkBook.Load("sample.xlsx")
Private workSheet As WorkSheet = workBook.GetWorkSheet("Data")
' Clear cell content
workSheet("A1").ClearContents()
workBook.SaveAs("clearSingleCell.xlsx")
Clear Cell Range Example
This method is available in the Range class, allowing you to execute it on any range, regardless of its size. Here are some examples:
- Clear a single cell:
- workSheet ["A1"].ClearContents()
- Clear a column:
- workSheet.GetColumn("B").ClearContents()
- Clear a row:
- workSheet.GetRow(3).ClearContents()
- Clear a two-dimensional range (multiple rows and columns):
- workSheet ["D6:F9"].ClearContents()
:path=/static-assets/excel/content-code-examples/how-to/clear-cells-clear-cell-range.cs
using IronXL;
WorkBook workBook = WorkBook.Load("sample.xlsx");
WorkSheet workSheet = workBook.GetWorkSheet("Data");
// Clear a single cell(A1)
workSheet["A1"].ClearContents();
// Clear a column(B)
workSheet.GetColumn("B").ClearContents();
// Clear a row(4)
workSheet.GetRow(3).ClearContents();
// Clear a two-dimensional range(D6:F9)
workSheet["D6:F9"].ClearContents();
workBook.SaveAs("clearCellRange.xlsx");
Imports IronXL
Private workBook As WorkBook = WorkBook.Load("sample.xlsx")
Private workSheet As WorkSheet = workBook.GetWorkSheet("Data")
' Clear a single cell(A1)
workSheet("A1").ClearContents()
' Clear a column(B)
workSheet.GetColumn("B").ClearContents()
' Clear a row(4)
workSheet.GetRow(3).ClearContents()
' Clear a two-dimensional range(D6:F9)
workSheet("D6:F9").ClearContents()
workBook.SaveAs("clearCellRange.xlsx")
Output Spreadsheet
Before
After
Clear Worksheet Collection Example
In addition to clearing individual cells, you can also delete all worksheets in a workbook with ease. To achieve this, simply use the Clear
method on the worksheet collection. This method allows you to remove all worksheets in one go, providing a convenient way to reset the workbook to its initial state.
:path=/static-assets/excel/content-code-examples/how-to/clear-cells-clear.cs
using IronXL;
WorkBook workBook = WorkBook.Load("sample.xlsx");
// Delete all worksheets
workBook.WorkSheets.Clear();
workBook.SaveAs("useClear.xlsx");
Imports IronXL
Private workBook As WorkBook = WorkBook.Load("sample.xlsx")
' Delete all worksheets
workBook.WorkSheets.Clear()
workBook.SaveAs("useClear.xlsx")