Back to Devexpress

Worksheet.Clear(CellRange) Method

officefileapi-devexpress-dot-spreadsheet-dot-worksheet-dot-clear-x28-devexpress-dot-spreadsheet-dot-cellrange-x29.md

latest8.1 KB
Original Source

Worksheet.Clear(CellRange) Method

Removes cell content, formatting, hyperlinks and comments.

Namespace : DevExpress.Spreadsheet

Assembly : DevExpress.Spreadsheet.v25.2.Core.dll

NuGet Package : DevExpress.Spreadsheet.Core

Declaration

csharp
void Clear(
    CellRange range
)
vb
Sub Clear(
    range As CellRange
)

Parameters

NameTypeDescription
rangeCellRange

A CellRange object specifying the cell range to clear.

|

Remarks

The Clear method empties cells and clears all formatting. You can separately remove cell content, formatting, hyperlinks or comments using the Worksheet.ClearContents, Worksheet.ClearFormats, Worksheet.ClearHyperlinks or Worksheet.ClearComments methods, respectively.

To clear cells, you can also use the range’s Clear* extension methods, defined by the RangeExtensions class (RangeExtensions.Clear, RangeExtensions.ClearContents, RangeExtensions.ClearFormats, RangeExtensions.ClearHyperlinks and RangeExtensions.ClearComments). These extension methods are accessible as methods of the CellRange object and called by using the instance method syntax.

To delete entire cells from the worksheet, use the Worksheet.DeleteCells method (see the How to: Delete a Cell or Range of Cells example).

Example

This example demonstrates how to clear worksheet cells.

  • Clear All

  • Clear Cell Content

  • Clear Cell Formatting

  • Clear Cell Hyperlinks

  • Clear Cell Comments

View Example

csharp
// Remove all cell information (content, formatting, hyperlinks and comments).
worksheet.Clear(worksheet["C2:D2"]);

// Remove cell content.
worksheet.ClearContents(worksheet["C3"]);
worksheet["D3"].Value = null;

// Remove cell formatting.
worksheet.ClearFormats(worksheet["C4"]);
worksheet["D4"].Style = workbook.Styles.DefaultStyle;

// Remove hyperlinks from cells.
worksheet.ClearHyperlinks(worksheet["C5"]);

Hyperlink hyperlinkD5 = worksheet.Hyperlinks.GetHyperlinks(worksheet["D5"])[0];
worksheet.Hyperlinks.Remove(hyperlinkD5);

// Remove comments from cells.
worksheet.ClearComments(worksheet["C6"]);
vb
' Remove all cell information (content, formatting, hyperlinks and comments).
worksheet.Clear(worksheet("C2:D2"))

' Remove cell content.
worksheet.ClearContents(worksheet("C3"))
worksheet("D3").Value = Nothing

' Remove cell formatting.
worksheet.ClearFormats(worksheet("C4"))
worksheet("D4").Style = workbook.Styles.DefaultStyle

' Remove hyperlinks from cells.
worksheet.ClearHyperlinks(worksheet("C5"))

Dim hyperlinkD5 As Hyperlink = worksheet.Hyperlinks.GetHyperlinks(worksheet("D5"))(0)
worksheet.Hyperlinks.Remove(hyperlinkD5)

' Remove comments from cells.
worksheet.ClearComments(worksheet("C6"))

The following code snippets (auto-collected from DevExpress Examples) contain references to the Clear(CellRange) method.

Note

The algorithm used to collect these code examples remains a work in progress. Accordingly, the links and snippets below may produce inaccurate results. If you encounter an issue with code examples below, please use the feedback form on this page to report the issue.

spreadsheet-document-api-import-data-from-data-sources/CS/DataImportExample/Form1.cs#L26

csharp
Worksheet worksheet = workbook.Worksheets[0];
worksheet.Clear(worksheet.GetUsedRange());
ImportDataTable(worksheet);

winforms-spreadsheet-import-data-from-different-data-sources/CS/DataImportExample/Form1.cs#L27

csharp
Worksheet worksheet = spreadsheetControl1.Document.Worksheets[0];
worksheet.Clear(worksheet.GetUsedRange());
ImportDataTable();

spreadsheet-document-api-import-data-from-data-sources/VB/DataImportExample/Form1.vb#L20

vb
Dim worksheet As Worksheet = workbook.Worksheets(0)
worksheet.Clear(worksheet.GetUsedRange())
ImportDataTable(worksheet)

winforms-spreadsheet-import-data-from-different-data-sources/VB/DataImportExample/Form1.vb#L24

vb
Dim worksheet As Worksheet = spreadsheetControl1.Document.Worksheets(0)
worksheet.Clear(worksheet.GetUsedRange())
ImportDataTable()

spreadsheet-document-api-examples-part1/VB/SpreadsheetExamples/SpreadsheetActions/CellActions.vb#L219

vb
' Remove all cell information (content, formatting, hyperlinks and comments).
worksheet.Clear(worksheet("C2:D2"))
' Remove cell content.

See Also

ClearContents(CellRange)

ClearFormats(CellRange)

ClearHyperlinks(CellRange)

ClearComments(CellRange)

DeleteCells

How to: Delete a Cell or Range of Cells

Worksheet Interface

Worksheet Members

DevExpress.Spreadsheet Namespace