Not able to add text to an existing excel file

Ask general questions here.
mohan.pentyala
Posts: 31
Joined: Thu Sep 20, 2018 7:43 am

Not able to add text to an existing excel file

Post by mohan.pentyala » Wed Jan 08, 2020 10:48 am

Hi ,

I am trying to add data to an existing excel file. But method i executing but data is not adding to the sheet. Can you correct if I am missing anything

Method:


public void adddatatoexcel(string scenario,string timetaken)
{
Excel.Range range1 ;
xlApp4 = new Excel.Application();
worKbooK1 = xlApp4.Workbooks.Open("D:\\bmp\\PerformanceTesting1.xlsx", 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);

worksheet5 = (Excel.Worksheet)worKbooK1.Worksheets.get_Item(1);

string str1;
int rowCnt1;
int columnCnt1;
int rw1 = 1;
int cl1 = 1;
//string countryname;
//string scenario;

range1 = worksheet5.UsedRange;
rw1 = range1.Rows.Count;
cl1 = range1.Columns.Count;

for (rowCnt1 = 1; rowCnt1 <= rw1; rowCnt1++)
{
for (columnCnt1 = 1; columnCnt1 <= cl1; columnCnt1++)
{
str1=(string)(range1.Cells[rowCnt1, columnCnt1] as Excel.Range).Value2;
if(str1 == null)
{
Report.Info("In if block");

//worksheet5.Cells[rowCnt1 , columnCnt1] = scenario;

worksheet5.Cells[rowCnt1 , columnCnt1+1] = "timetaken";

worKbooK1.Save();
HtmlReports.SuccessReport("Writing in Excel Sheet Started", "Successfully Captured data in Excel");
break;
}

else
{
Report.Info("Sheet is not empty");

}

}

}
//return null;
}

User avatar
odklizec
Ranorex Guru
Ranorex Guru
Posts: 5459
Joined: Mon Aug 13, 2012 9:54 am
Location: Zilina, Slovakia

Re: Not able to add text to an existing excel file

Post by odklizec » Wed Jan 08, 2020 12:04 pm

Hi,

Is there an error message in report? Have you tried to debug the code (line by line)? Also, try to add worKbooK1.Close(); right after Save.
Pavel Kudrys
Ranorex explorer at Descartes Systems

Please add these details to your questions:
  • Ranorex Snapshot. Learn how to create one >here<
  • Ranorex xPath of problematic element(s)
  • Ranorex version
  • OS version
  • HW configuration

mohan.pentyala
Posts: 31
Joined: Thu Sep 20, 2018 7:43 am

Re: Not able to add text to an existing excel file

Post by mohan.pentyala » Wed Jan 08, 2020 1:53 pm

HI

yes I have added close now, But still same

I have gone through line by line in debug mode. it is going through the line for adding data but data is not adding to excel.

Same time no error message test case was passed