4

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.

B. Clay Shannon-B. Crow Raven
  • 8,547
  • 144
  • 472
  • 862
Alk
  • 41
  • 1
  • 1
  • 2

3 Answers3

8

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;
nawfal
  • 70,104
  • 56
  • 326
  • 368
  • 3
    The Value2 property treats dates and currencies as numbers according to the documentation - so if you need a DateTime object then you have to use get_Value – Carl Pritchett Jan 22 '13 at 04:47
  • 1
    Note that X1 is the name of the alias to Microsoft.Office.Interop.Excel. – John Jan 16 '14 at 16:30
2

From this post on MSDN it looks like you would want something like:

var cellRangeValue = rng_ResolutionCell.get_Value(System.Type.Missing);
Chris Shaffer
  • 32,199
  • 5
  • 49
  • 61
-1

Also:

newWS = (Microsoft.Office.Interop.Excel.Worksheet)newWB.Worksheets[2];
newWS.Select();
string ExcelCellContent = (string)newWS.Cells[2, 1].Value2;

Usage:

if((string)(ws_Varsheet.Cells[x, y].Value2 != "")
{
   // process
}
Rupert
  • 169
  • 1
  • 2
  • 4