Back to Devexpress

How to: Clear Cell Formatting

wpf-16387-controls-and-libraries-spreadsheet-examples-formatting-how-to-clear-cell-formatting.md

latest2.2 KB
Original Source

How to: Clear Cell Formatting

  • Jun 07, 2019
  • 2 minutes to read

This example demonstrates how to remove all formatting from a cell or range of cells. You can do this in one of the following ways.

  • Call the Worksheet.ClearFormats method with the passed object specifying the cell or cell range to be cleared.
  • Apply the Normal style to a cell or range of cells via the CellRange.Style property.

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"]);

Comment commentD6 = worksheet.Comments.GetComments(worksheet["D6"])[0];
worksheet.Comments.Remove(commentD6);
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"))

Dim commentD6 As Comment = worksheet.Comments.GetComments(worksheet("D6"))(0)
worksheet.Comments.Remove(commentD6)