c# - EPPLUS如何使用find命令查找数据

标签 c# excel epplus

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

我想避免读取所有单元格,因为这会花费很多时间。我想找到那个特定的字符串并只复制那个特定的行而不阅读整个工作表。

谢谢。

最佳答案

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

如果您想在工作表中查询匹配值,Linq 可能是最好的方法。您可以在查询中指定一个范围,这样您就不必遍历整个工作表。此示例来自可在 codeplex 上下载的 EPPlus 示例中的示例 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++;
            }

关于c# - EPPLUS如何使用find命令查找数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11156018/

相关文章:

c# - 未调用 Windows 窗体文本框函数

EPPlus: "Column out of range"错误与 LoadFromCollection

asp.net - 在 EPPlus 中格式化列以使用电话号码格式

c# - Visual C# SQL 语句不会插入到数据库中

c# - 在嵌入式资源上强制 Visual Studio 重建已更改

c# - 用户控件、自定义控件和组件之间有什么区别?

vba - 将 Variant 数组传递给 sub, "Expected: ="

vba - 打开通过 URL 下载的文件

java - 如何修复 "We found a problem with some content in ' abc.xlsm'。您希望我们尽力恢复吗?

EPPlus如何使用ExcelChartTrendline添加趋势线?