如何从.net应用程序中搜索Excel中的值?

时间:2009-03-10 10:49:14

标签: .net excel

我必须从excel中搜索超过5000条记录的值。

在Windows .net应用程序中实现此目的的最佳方法是什么?

3 个答案:

答案 0 :(得分:1)

由于您正在尝试搜索'5000条记录,我认为您必须尝试搜索thro'值。 您可以使用interop API中的“Range.Find”来执行此操作。

private void OpenExcelFile()
{
     Excel.Application exlApp = new Microsoft.Office.Interop.Excel.Application();

    if (exlApp == null)
    {
        MessageBox.Show("Excel app object could not be created");
    }
    else
    {

        exlFileSelector.FileName = @"*.xls";

        if (exlFileSelector.ShowDialog() == DialogResult.OK)
        {
            Excel.Workbook wrkBook = exlApp.Workbooks.Open(exlFileSelector.FileName, 0, true, 5, "", "", true, Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, true, true);
            Excel.Sheets sheetList = wrkBook.Sheets;

            Excel.Range search = exlApp.get_Range("A1", "C5");
            search.Find("FindMe", null, Excel.XlFindLookIn.xlValues, Excel.XlLookAt.xlWhole, Excel.XlSearchOrder.xlByRows, Excel.XlSearchDirection.xlNext, false, null, null); 
        }
    }
}

进一步阅读: How to: Search for Text in Worksheet Ranges

答案 1 :(得分:1)

SpreadsheetGear for .NET是.NET的Excel兼容电子表格组件,它支持类似于Excel的Find API的Find API。您可以搜索值或公式,或计算公式,然后根据需要搜索结果。这是一个简短的示例,它加载工作簿并找到以“John”开头的第一个单元格:

// Open an xlsx workbook (can also open xls).
IWorkbook workbook = Factory.GetWorkbook(@"C:\tmp\Data.xlsx");

// Find the first cell which starts with "John".
IRange foundCell = workbook.Worksheets["Sheet1"].Cells.Find("John*", null,
    FindLookIn.Values, LookAt.Whole, SearchOrder.ByColumns, SearchDirection.Next, true);
if (foundCell != null)
    Console.WriteLine("found {0} in cell {1}", foundCell.Text, foundCell.Address);

您可以下载免费评估here

免责声明:我拥有SpreadsheetGear LLC

答案 2 :(得分:0)

您的值是由电子表格公式之一产生的计算值,还是实际的预定值?

如果是晚些时候,您可以将电子表格保存为XML并适当地解析它们 或
根据您可用的索引技术,您可以将它们编入索引,然后搜索索引并找到适当的值 (这是我首选的方法,现在我已经考虑过了。)

如果是前者,我能想到的唯一选择是使用InterOp库打开每一个并使用API​​。