How to Clear Cells

by Chaknith Bin

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.


C# NuGet Library for Excel

Install with NuGet

Install-Package IronXL.Excel
or
C# Excel DLL

Download DLL

Download DLL

Manually install into your project

Clear 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")
VB   C#

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")
VB   C#

Output Spreadsheet

Sample
Clear Cell Range

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")
VB   C#

Chaknith Bin

Software Engineer

Chaknith is the Sherlock Holmes of developers. It first occurred to him he might have a future in software engineering, when he was doing code challenges for fun. His focus is on IronXL and IronBarcode, but he takes pride in helping customers with every product. Chaknith leverages his knowledge from talking directly with customers, to help further improve the products themselves. His anecdotal feedback goes beyond Jira tickets and supports product development, documentation and marketing, to improve customer’s overall experience.When he isn’t in the office, he can be found learning about machine learning, coding and hiking.