如何清除细胞
This article was translated from English: Does it need improvement?
TranslatedView the article in English
清除单元格内容可用于多种目的,例如删除不需要或过时的数据、重置单元格值、清理电子表格布局、准备模板或修复数据输入错误。
IronXL简化了在C#中清除单元格内容的过程,无需Interop。
如何清除单元格内容
- 下载用于清除单元格内容的 C# 库
- 加载现有的 Excel 电子表格
- 选择要清除的范围、行或列
- 调用
清除内容
方法对所选范围 - 使用
清除
方法删除所有工作表
开始使用IronXL
立即在您的项目中开始使用IronXL,并享受免费试用。
清除单个单元格示例
要清除选定单元格的内容,您可以使用 ClearContents
方法。
: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#
清除单元格范围示例
此方法可在 Range 类中使用,您可以在任何范围内执行此方法,无论其大小如何。以下是一些示例:
- 清除单个单元格:
- 工作表["A1"].ClearContents()**
- 清除一列:
- workSheet.GetColumn 返回指定列的所有单元格。("B").ClearContents()
- 清除一行:
- workSheet.GetRow(3).ClearContents()**
清除二维范围(多行多列):
- 工作表["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#
输出电子表格
之前
之后
清除工作表集合示例
除了清除单个单元格,您还可以轻松删除工作簿中的所有工作表。 要实现这一点,只需在工作表集合上使用 Clear
方法。 此方法允许您一次性删除所有工作表,提供了一种方便的方式来将工作簿重置为初始状态。
: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#