Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding a value with Epplus

Tags:

c#

linq

epplus

I'm trying to use the code to find the address of a certain value in a Worksheet using Epplus and Linq. The value is in column D (4), but could be in any cell However, the following error is displayed

Linq Code

var query3 = (from cell in sheet.Cells["d:d"]
    where cell.Value.ToString().Equals("CRÉDITOS")
    select cell);

Error in Results View:

   at ExcelTests.Form1.<>c.<button1_Click>b__1_0(ExcelRangeBase cell)
   at System.Linq.Enumerable.WhereEnumerableIterator`1.MoveNext()
   at System.Linq.SystemCore_EnumerableDebugView`1.get_Items()

enter image description here

like image 564
Gabriel Bernardone Avatar asked Mar 01 '17 18:03

Gabriel Bernardone


People also ask

Does EPPlus require Excel?

No, it does not require Excel to be installed on the server, as you can read in the docs: EPPlus is a . NET library that reads and writes Excel files using the Office Open XML format (xlsx).

What is EPPlus C#?

EPPlus is a very helpful open-source 3rd party DLL for writing data to excel. EPPlus supports multiple properties of spreadsheets like cell ranges, cell styling, charts, pictures, shapes, comments, tables, protection, encryption, pivot tables, data validation, conditional formatting, formula calculation, etc.


1 Answers

As @krillgar suggested, you should rewrite the linq statement to include the possibility of Value returning null.

var query3 = 
    from cell in sheet.Cells["d:d"]
    where cell.Value?.ToString() == "CRÉDITOS"
    select cell;
like image 174
dbraillon Avatar answered Oct 01 '22 00:10

dbraillon