在C#中将数据过滤到数组中

时间:2016-07-12 15:55:14

标签: c#

如何根据某些条件提取数据并创建另一个数组,这类似于excel过滤数据的方式? 我没有在Excel中进行过滤,而是希望将相关数据保存到数组中并使用它做一些事情。

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);

0 个答案:

没有答案