Search code examples
c#.netexcel-interopexcel-automation

How to delete the visible rows after Auto Filter , excluding the heading row?


In Excel Interop, after setting a Range.Autofilter(), then I need to delete all visible rows except the first row. Since the first row is the heading row of the sheet. Thanks .


Solution

  • Excel.Application objexcel = new Microsoft.Office.Interop.Excel.Application();
    Excel.Workbook xlBook = objexcel.Workbooks.Open(filename);
    Excel.Worksheet xlSheet = (Microsoft.Office.Interop.Excel.Worksheet)xlBook.Worksheets.get_Item(1);
    range = xlSheet.UsedRange;
    Excel.Range last =xlSheet.Cells.SpecialCells(Excel.XlCellType.xlCellTypeLastCell, Type.Missing);
    range.get_Range("A2", last).Delete(XlDeleteShiftDirection.xlShiftUp);
    

    This worked.