1

我正在使用 EPPLUS 在 C# .NET 4.0 中编写一个应用程序并尝试加载一个巨大的 20mb excel 文件。我想使用 Find 或 Findall 方法在工作表中查找特定字符串。任何人都可以分享如何执行此操作的片段,因为互联网上没有关于此的信息。

我想避免阅读所有单元格,因为这需要很多时间。我想找到那个特定的字符串,然后复制那个特定的行而不阅读整个工作表。

谢谢你。

4

1 回答 1

7

ExcelDataValidationCollection 上的 Find 和 FindAll 方法不适用于在工作簿/工作表中搜索字符串,而是让您使用 lambda 表达式在工作表中搜索特定的数据验证。

如果您想查询工作表以获取匹配值,Linq 可能是最好的方法。您可以在查询中指定一个范围,这样您就不必遍历整个工作表。此示例来自可在 codeplex 上下载的 EPPlus Samples 中的 Sample 8。

//Here we use more than one column in the where clause. We start by searching column D, then use the Offset method to check the value of column C.
            var query3 = (from cell in sheet.Cells["d:d"]
                          where cell.Value is double && 
                                (double)cell.Value >= 9500 && (double)cell.Value <= 10000 && 
                                cell.Offset(0, -1).GetValue<DateTime>().Year == DateTime.Today.Year+1 
                          select cell);

            Console.WriteLine();
            Console.WriteLine("Print all cells with a value between 9500 and 10000 in column D and the year of Column C is {0} ...", DateTime.Today.Year + 1);
            Console.WriteLine();    

            count = 0;
            foreach (var cell in query3)    //The cells returned here will all be in column D, since that is the address in the indexer. Use the Offset method to print any other cells from the same row.
            {
                Console.WriteLine("Cell {0} has value {1:N0} Date is {2:d}", cell.Address, cell.Value, cell.Offset(0, -1).GetValue<DateTime>());
                count++;
            }
于 2012-07-01T07:22:22.103 回答