Deleting all the Rows in a Worksheet

Hi I want to delete all the rows in all the worksheets of a workbook (blanks and data) and leave it empty , and the only way I found is for each worksheet

first delete the blank rows and then delete rows using row.count . I was wondering if there is a more elegant way to Delete All Rows including blank.

Below is my current code.

Workbook wb = new Workbook(fileName);

foreach (Worksheet ws in wb.Worksheets)

{ ws.Cells.DeleteBlankRows(); ws.Cells.DeleteRows(0, ws.Cells.Rows.Count); }

Hi,

Thanks for your posting and using Aspose.Cells for .NET.

Please download and use the latest version:
Aspose.Cells for .NET (Latest Version)


There is another method to delete all the rows inside a workbook and this is elegant method because it uses range to delete only those cells which has data.

Please see the code below and I have attached the source and output xlsx file.

Please also see the screenshot for your reference.

C#


string filePath = @“F:\source.xlsx”;


Workbook workbook = new Workbook(filePath);


foreach (Worksheet worksheet in workbook.Worksheets)

{

Range maxRange = worksheet.Cells.MaxDisplayRange;


worksheet.Cells.DeleteRange(maxRange.FirstRow, maxRange.FirstColumn, maxRange.RowCount, maxRange.ColumnCount, ShiftType.Up);

}


workbook.Save(filePath + “.out.xlsx”);



Screenshot: