Get a cell value from excel using EPPlus and Linq

泄露秘密 提交于 2019-12-10 23:52:54

问题


Is it possible to get a cell value from excel sheet using EPPlus and Linq? Example:

I have an excel sheet with 3 columns

Column 1 = Userid
Column 2 = Email address
Column 3 = Full name

Now i would like to return the email address where userid = x

I hope it's more clear now.


回答1:


Suppose Column 1 is a, Column 2 is b:

var sheet = excelPackage.Workbook.Worksheets[sheetname_orSheetIndex];
var objs = from cell in sheet.Cells["a:a"]        // a:a is the column a, Userid
           where cell.Value.ToString().Equals(x)  // x is the input userid
           select sheet.Cells[cell.Start.Row, 2]; // 2 is column b, Email Address

It will work! Edited: It will return the collection of ExcelRange.




回答2:


Not being exactly sure what you are trying to do, here is an example of getting the value from a cell:

  excelPackage.Workbook.Worksheets
                .FirstOrDefault(w => w.Name == "Your Worksheet Name")
                .Cells.FirstOrDefault(c => c.Address == "Your Cell Address").Value;

Just make sure you are using the System.Linq namespace



来源:https://stackoverflow.com/questions/13857559/get-a-cell-value-from-excel-using-epplus-and-linq

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!