【问题标题】:EPPLUS how to find data using the find commandEPPLUS 如何使用 find 命令查找数据
【发布时间】:2012-07-05 20:20:58
【问题描述】:

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

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

谢谢。

【问题讨论】:

  • 你试过什么? Find/FindAll 方法在哪里?我看不到他们..
  • 它在数据验证类中。我使用了一个简单的 for 循环来完成我的工作

标签: c# excel epplus


【解决方案1】:

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

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

//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++;
            }

【讨论】:

  • 我在执行以下 foreach 时遇到问题(ExcelCellDeleteList 中的字符串 cellString){ var query3 = (from cell in ws.Cells["b:b"] where cell.Value is string select cell ); } 我想搜索“cellString”字符串。
猜你喜欢
  • 1970-01-01
  • 2019-09-02
  • 1970-01-01
  • 2012-02-11
  • 1970-01-01
  • 2015-09-07
  • 2016-01-06
  • 2019-12-24
  • 2021-11-22
相关资源
最近更新 更多