Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C#: Getting a cell's value with Excel.interop

Tags:

c#

excel

I want to get the value of a cell from an excel file with a software in C#; NOT THE TEXT, because the text depend of the size of the column, and I must not change it myself, the software should not be disturbed by that. So my choice was to get the value.

I get my cell as a range of 1:1, so I have a Range object, but Value field and Value2 field are not recognized by the language. I tried with get_Value() but it needs a parameter and I don't know what to put in it, i didn't find documentations about it. Here's my code (extracted from a loop):

  if((string)(ws_Varsheet.get_Range("L" + iIndex, "L" + iIndex).Text) != "")
  {

    rng_ResolutionCell = ws_Varsheet.get_Range("L" + iIndex, "L" + iIndex);   
    float iResolution;
    rng_ResolutionCell.Cells.get_Value(&iResolution); //what to do here?
    str_Resolution = ((string)iResolution.toString()).Replace(",",".");
    str_Resolution = str_Resolution.Replace(" ","");
     mObj.Str_Resolution="1";
  }

Can you help me with that? Thanks by advance.

like image 319
Alk Avatar asked Jan 20 '23 23:01

Alk


1 Answers

I have often found codes a little more complex than needed all over. Basically reading a cell is as simple as:

Xl.Range rng = sheet.Cells[row, column] as Xl.Range;
return rng.Value2;
like image 92
nawfal Avatar answered Jan 29 '23 03:01

nawfal