How to get correct Row count of filtered excel in c# interoperobility

RAMAN RAGAVAN 51 Reputation points
2021-12-11T11:30:12.813+00:00

Issue is my code always return one row instead of 13. I need to create a text file based on the filtered rows.But i am not getting the correct count and filtered rows.I will add and remove other clms .but row has issue.I need to get rows having colum value 01

xlRange.AutoFilter(17, "01", Microsoft.Office.Interop.Excel.XlAutoFilterOperator.xlFilterValues, Type.Missing, true);

This line filtering but not giving correct counts of filtered row

what i have tried:

public static void ExportToTextFile(Excel._Worksheet xlWorksheet, Excel.Workbook xlWorkbook, Excel.Application xlApp, string wbname)

        {

            //creating .txt file with new coloumns



            Excel._Worksheet xlWorksheetNew = (Microsoft.Office.Interop.Excel.Worksheet)xlWorkbook.Sheets[1]; // excel sheet

            xlWorksheet.Copy(xlWorksheetNew); //copying from another xl

            Excel.Range excelRange = xlWorksheetNew.UsedRange;



            xlWorksheetNew.Cells[1, 18] = "FLAG";





            Microsoft.Office.Interop.Excel.Range xlRange = xlWorksheetNew.UsedRange;



          xlRange.AutoFilter(17,"01",Microsoft.Office.Interop.Excel.XlAutoFilterOperator.xlFilterValues, Type.Missing, true);


            int Columns = xlWorksheetNew.UsedRange.Columns.Count;

            int Rows = filteredRange.Rows.Count; //NOT GIVING CORRECT COUNT


            ((Excel.Range)xlWorksheetNew.Columns["P:Q"]).Delete();

            ((Excel.Range)xlWorksheetNew.Columns["C"]).Delete();

            ((Excel.Range)xlWorksheetNew.Columns["A:B"]).Delete();




            int AfterdeletColumns = xlWorksheetNew.UsedRange.Columns.Count;

            int afterdeletRows = filteredRange.Rows.Count;



             xlWorksheetNew.Visible = Excel.XlSheetVisibility.xlSheetHidden;


            ExportToTextFile(Rows, Columns, xlWorksheetNew);

        }
.NET CLI
.NET CLI
A cross-platform toolchain for developing, building, running, and publishing .NET applications.
323 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,280 questions
{count} votes

1 answer

Sort by: Most helpful
  1. RLWA32 40,656 Reputation points
    2021-12-11T13:02:53.5+00:00