Retrieving data from Excel cells

C

Chris Bellini

Greetings! I'm developing a C# application that needs to read some data
from a selected XLS file. I've used VB in the past to automate Excel
but this is the first time I've used C#. Back in VB, I was able to
retrieve a cell's contents via something like this:

Set objExcellApp = CreateObject("Excel.Application")
Set objWorkbook = objExcel.Workbooks.Open(strPathToXLS)

' retrive the contents of A1
strData = objExcel.Cells(1, 1).Value

However, I quickly found out that this was not the case with C#. Thus
far, I have:

objExcel = new Excel.Application();
objWorkbook = (Excel._Workbook)(objExcel.Workbooks._Open(strPath, true,
true, Missing.Value, Missing.Value, Missing.Value,
Missing.Value, Missing.Value, Missing.Value,
Missing.Value, Missing.Value, Missing.Value,
Missing.Value));

objSheet = (Excel._Worksheet)objWorkbook.ActiveSheet;

objSheet.Cells[1,1] = "Testing";

// I thought this would store "Testing" in strData
// but it sure doesn't ;)
strData = objSheet.Cells[1,1];

How can I retreive a cell's contents? It could probably be done via
Range() but I'd hate to resort to that, as I like the flexibility that
the ROW,COLUMN format offers. Thanks in advance.


Chris
 
G

Guest

This is how I do it (with Range)
Excel.Range oRng=(Excel.Range)oSheet.Cells[1,1];
string strData=oRng.Value.ToString();
Hope it helps
Karl Daggfeldt
 
C

Chris Bellini

Thanks, Karl. You pointed me in the right direction. For whatever
reason, the compiler hated the Value method. But I tried this:

Excel.Range objRange = (Excel.Range)objSheet.Cells[1,1];
strData = objRange.get_Value(Missing.Value).ToString();

and it worked like a charm :) Thanks.


Chris
This is how I do it (with Range)
Excel.Range oRng=(Excel.Range)oSheet.Cells[1,1];
string strData=oRng.Value.ToString();
Hope it helps
Karl Daggfeldt

:

Greetings! I'm developing a C# application that needs to read some data
from a selected XLS file. I've used VB in the past to automate Excel
but this is the first time I've used C#. Back in VB, I was able to
retrieve a cell's contents via something like this:

Set objExcellApp = CreateObject("Excel.Application")
Set objWorkbook = objExcel.Workbooks.Open(strPathToXLS)

' retrive the contents of A1
strData = objExcel.Cells(1, 1).Value

However, I quickly found out that this was not the case with C#. Thus
far, I have:

objExcel = new Excel.Application();
objWorkbook = (Excel._Workbook)(objExcel.Workbooks._Open(strPath, true,
true, Missing.Value, Missing.Value, Missing.Value,
Missing.Value, Missing.Value, Missing.Value,
Missing.Value, Missing.Value, Missing.Value,
Missing.Value));

objSheet = (Excel._Worksheet)objWorkbook.ActiveSheet;

objSheet.Cells[1,1] = "Testing";

// I thought this would store "Testing" in strData
// but it sure doesn't ;)
strData = objSheet.Cells[1,1];

How can I retreive a cell's contents? It could probably be done via
Range() but I'd hate to resort to that, as I like the flexibility that
the ROW,COLUMN format offers. Thanks in advance.


Chris
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top