开发者

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

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 开发者_StackOverflowknow 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.


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;


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

var cellRangeValue = rng_ResolutionCell.get_Value(System.Type.Missing);


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
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜