473,394 Members | 1,879 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Retrieving data from Excel cells

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
2 46454
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: aniket_sp | last post by:
i am using a data adapter and a dataset for filling and retrieving data into .mdb database. following is the code..... for the form load event Dim dc(0) As DataColumn Try If...
0
by: maccer | last post by:
i need to create some form of macro that will read the information from excel cells and then put them all together in a text box. it is very important that the formating remains consistant between...
3
by: Jakob Petersen | last post by:
Hi, I need to increase the speed when retrieving data from a hosted SQL Server into VBA. I'm using simple SELECT statements. How important is the speed of my Internet connection? (I have...
0
by: DC01 | last post by:
I have added a new measure successfully into the normal cube. I then add it to the virtual cube and reprocess all cubes. I can browse the normal cube successfully. Then when I try and browse the...
1
by: hanusoft | last post by:
This is an example of Inserting and Retrieving data from xml file. private void Page_Load(object sender, System.EventArgs e) { // Put user code to initialize the page here ...
1
by: hanusoft | last post by:
This is an example of Inserting and Retrieving data from xml file. private void Page_Load(object sender, System.EventArgs e) { // Put user code to initialize the page here ...
4
by: smartin | last post by:
Hi, I'm having problem retrieving data from an SQL stored procedure. I tried debugging but it wont give a the reason for the error. it just throws an exception after executing cmd.ExecuteNonQuery...
3
ADezii
by: ADezii | last post by:
Last Tip, we demonstrated the technique for retrieving data from a DAO Recordset, and placing it into a 2-dimensional Array using the GetRows() Method. This week, we will cover the same exact Method...
10
vikas1111
by: vikas1111 | last post by:
Hi All Can anyone give me an idea to solve the problem.. My Problem is ,, I want to Retrieving data from database and displaying in textbox... If anybody have link of some good tutorial on...
7
by: splendid9 | last post by:
Problem in retrieving data from a XML file.......this is my code- protected void Page_Load(object sender, EventArgs e) { DataSet ds = new DataSet(); ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.