1

How do I extract data based on some criteria and create another array, this is similar to how excel filters data? Instead of filtering in excel I want to hold relevant data into array and do something with it.

Microsoft.Office.Interop.Excel.Application xlApp = new    Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel.Workbook xlWorkBookDB;
Microsoft.Office.Interop.Excel.Worksheet xlWorkSheetDB;
Microsoft.Office.Interop.Excel.Range xlRangeDB;

    int lRow = 0;

    //Open Excel Workbook to read data

     xlWorkBookDB = xlApp.Workbooks.Open(TestWorkbook1,
                        false, true, Type.Missing, Type.Missing,
                        Type.Missing, Type.Missing, Type.Missing, Type.Missing,
                        Type.Missing, Type.Missing, Type.Missing, Type.Missing,
                        Type.Missing, Type.Missing);

    //Name Excel Sheet from where data needs to be taken

    Worksheet xlWorkSheetDB = xlWorkBookDB.Worksheets["DB"];

    // Check if there is any data available in the worksheet
    int dataCount = (int)xlApp.WorksheetFunction.CountA(xlWorkSheetDB.Cells);

                        if (dataCount > 0)
                        {
                            lRow = xlWorkSheetDB.Cells.Find("*", xlWorkSheetDB.get_Range("A1"), Type.Missing, Type.Missing, XlSearchOrder.xlByRows, XlSearchDirection.xlPrevious).Row;
                        }

    //Name a range to get data from
    xlRangeDB = xlWorkSheetDB.get_Range("A2", "BA" + lRow.ToString());

    //read above range into an array
    object[,] valueArray = xlRangeDB.Value2;


    //How do I extract data based on some criteria and create another array, this is similar to how excel filters data?
    //instead of filtering in excel I want to hold relevant data into array and do something with it.
//Something like below I found on google.

string[,] resultData = new string[];

//List<string> resultData = valueArray.FindAll(productgroup => productgroup.Contains("tv"));


//If everything goes well close excel workbook
xlWorkBookDB.Close(false, TestWorkbook1, null);
SurvivalMachine
  • 7,946
  • 15
  • 57
  • 87
AAP
  • 169
  • 1
  • 2
  • 17
  • 2
    just convert the Excel to DataTable, then from there you can utilize the Filter Function from DataView [how to filter a DataView](http://stackoverflow.com/questions/10009675/how-to-filter-data-in-dataview) – MethodMan Jul 12 '16 at 15:59
  • look at some of the postings here also I have an answer on here as well where I convert a CSV file to Datatable. http://stackoverflow.com/questions/14261655/best-fastest-way-to-read-an-excel-sheet-into-a-datatable – MethodMan Jul 12 '16 at 16:07

0 Answers0