473,785 Members | 2,768 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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("E xcel.Applicatio n")
Set objWorkbook = objExcel.Workbo oks.Open(strPat hToXLS)

' 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.Applicati on();
objWorkbook = (Excel._Workboo k)(objExcel.Wor kbooks._Open(st rPath, true,
true, Missing.Value, Missing.Value, Missing.Value,
Missing.Value, Missing.Value, Missing.Value,
Missing.Value, Missing.Value, Missing.Value,
Missing.Value)) ;

objSheet = (Excel._Workshe et)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 46478
This is how I do it (with Range)
Excel.Range oRng=(Excel.Ran ge)oSheet.Cells[1,1];
string strData=oRng.Va lue.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("E xcel.Applicatio n")
Set objWorkbook = objExcel.Workbo oks.Open(strPat hToXLS)

' 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.Applicati on();
objWorkbook = (Excel._Workboo k)(objExcel.Wor kbooks._Open(st rPath, true,
true, Missing.Value, Missing.Value, Missing.Value,
Missing.Value, Missing.Value, Missing.Value,
Missing.Value, Missing.Value, Missing.Value,
Missing.Value)) ;

objSheet = (Excel._Workshe et)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)ob jSheet.Cells[1,1];
strData = objRange.get_Va lue(Missing.Val ue).ToString();

and it worked like a charm :) Thanks.
Chris

KalleD wrote:
This is how I do it (with Range)
Excel.Range oRng=(Excel.Ran ge)oSheet.Cells[1,1];
string strData=oRng.Va lue.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("E xcel.Applicatio n")
Set objWorkbook = objExcel.Workbo oks.Open(strPat hToXLS)

' 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.Applicati on();
objWorkbook = (Excel._Workboo k)(objExcel.Wor kbooks._Open(st rPath, true,
true, Missing.Value, Missing.Value, Missing.Value,
Missing.Value, Missing.Value, Missing.Value,
Missing.Value, Missing.Value, Missing.Value,
Missing.Value)) ;

objSheet = (Excel._Workshe et)objWorkbook. ActiveSheet;

objSheet.Cell s[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
2236
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 OleDbConnection1.State = ConnectionState.Closed Then OleDbConnection1.Open() Else
0
1475
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 the excel cells and text box. any help would be appreciate as i am going nowhere fast, ta
3
3655
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 4mbits) Should I index my tables or use Stored Procedures? Or is there a kind of "flush" function or readonly function or... Or is it simply a question of the amount of data transmitted over the Internet?
0
1530
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 virtual cube it says 'Retrieving Data' but then thats it, it just hangs like that. I am using AS2000. The other problem I have is that the data is doubling up when its in the cube. The SQL which sets up the fact tables are all fine and are...
1
1623
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 if(!IsPostBack) { BindGrid();
1
1672
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 if(!IsPostBack) { BindGrid();
4
18819
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 without any details. Can anyone please help me.. Im stuck on it since 2 days Thanks Stored Procedure
3
43535
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 (GetRows()), but only as it applies to an ADO Recordset. Although there are similarities in the 2 methodologies, the ADO Method offers 2 more Optional Arguments, is a little more complex, and of course, the syntax is different in creating the...
10
34970
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 this pls let me know..Or if anybody can explain I would be very thankful...
7
2740
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(); ds.ReadXml(MapPath("prod.xml")); DataGrid grid1 = new DataGrid(); grid1.DataSource = ds; grid1.DataBind();
0
9480
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10325
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10148
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10091
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7499
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6740
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5381
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4053
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.