How to read data of a cell in excel

Ranorex Studio, Spy, Recorder, and Driver.
bheemuabhigna
Posts: 49
Joined: Thu Feb 03, 2011 3:15 pm

How to read data of a cell in excel

Post by bheemuabhigna » Thu Feb 10, 2011 1:31 pm

Hi,

How to retrieve data of a particular cell from excel sheet

Thanks
Abhigna

User avatar
Support Team
Site Admin
Site Admin
Posts: 12145
Joined: Fri Jul 07, 2006 4:30 pm
Location: Houston, Texas, USA
Contact:

Re: How to read data of a cell in excel

Post by Support Team » Thu Feb 10, 2011 1:38 pm

Hi,

have a look at following blog entry which will guide you thru Excel Test Automation with Ranorex:
Excel Test Automation Plug-In for Ranorex

Kind regards,
Tobias
Support Team

bheemuabhigna
Posts: 49
Joined: Thu Feb 03, 2011 3:15 pm

Re: How to read data of a cell in excel

Post by bheemuabhigna » Thu Feb 10, 2011 2:07 pm

Hi,

I am using below code

Excel.Application xlApp ;
Excel.Workbook xlWorkBook ;
Excel.Worksheet xlWorkSheet ;
Excel.Range range ;

string str;
int rCnt = 0;
int cCnt = 0;

xlApp = new Excel.ApplicationClass();
xlWorkBook = xlApp.Workbooks.Open("C:/TestData.xls", 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);

range = xlWorkSheet.UsedRange;

for (rCnt = 1; rCnt <= range.Rows.Count; rCnt++)
{
for (cCnt = 1; cCnt <= range.Columns.Count; cCnt++)
{
str = (string)(range.Cells[rCnt, cCnt] as Excel.Range).Value2 ;

}
}

xlWorkBook.Close(true, null, null);
xlApp.Quit();


here str gives complete data of one rows with column values seperated by commas
how can I get a value of single cell.

Thanks
Abhigna

User avatar
Support Team
Site Admin
Site Admin
Posts: 12145
Joined: Fri Jul 07, 2006 4:30 pm
Location: Houston, Texas, USA
Contact:

Re: How to read data of a cell in excel

Post by Support Team » Thu Feb 10, 2011 2:15 pm

Hi,

with altering the code above you get the value of cell(5,5):
...
  rCnt = 5;
  cCnt = 5;
  str = (string)(range.Cells[rCnt, cCnt] as Excel.Range).Value2;
...
instead of
...
for (rCnt = 1; rCnt <= range.Rows.Count; rCnt++)
{
  for (cCnt = 1; cCnt <= range.Columns.Count; cCnt++)
  {
    str = (string)(range.Cells[rCnt, cCnt] as Excel.Range).Value2;
  }
}
...
Regards,
Tobias
Support Team

bheemuabhigna
Posts: 49
Joined: Thu Feb 03, 2011 3:15 pm

Re: How to read data of a cell in excel

Post by bheemuabhigna » Thu Feb 10, 2011 2:30 pm

Thanks.
I solved the issue You can clos the ticket