By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,659 Members | 940 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,659 IT Pros & Developers. It's quick & easy.

Retrieving data from Excel cells

P: n/a
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
Nov 17 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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

"Chris Bellini" wrote:
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

Nov 17 '05 #2

P: n/a
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

KalleD wrote:
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

"Chris Bellini" wrote:

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

Nov 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.