This is a migrated thread and some comments may be shown as answers.

How do I write test results to a specific worksheet within an Excel workbook?

1 Answer 91 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
David
Top achievements
Rank 1
David asked on 16 Oct 2013, 08:36 PM
Hello!

I found the code that allows me to write test result to an Excel file or txt file, and it all works great!  No issues at all.  But what I would like to do now is write the same results to a specific worksheet within the workbook.  I've tried several different methods that I found online, but keep getting errors in Visual Studio.  I've searched throughout these forums and haven't found exactly what I'm looking for.

Scenario I'm trying to achieve:
Run multiple tests and have them write to the same workbook, but each test assigned/writing to its own worksheet within that workbook.  Does that make sense? 
Example:
test1.tstest writes to Sheet1
test2.tstest writes to Sheet2
etc

Below is the code I am using that works successfully in writing to the default worksheet.  Any help is appreciated.  Thanks!



        public override void OnAfterTestCompleted(TestResult result)
        {
            string passed = Convert.ToString(result.TotalPassedSteps);
            string notRunSteps = Convert.ToString(result.TotalNumberOfNotRunSteps);
            string overall = Convert.ToString(result.Result);

            string resultMessage = Convert.ToString(result.Message);
            string[] split = resultMessage.Split(new Char[] { '\n' });

            Microsoft.Office.Interop.Excel.Application excelApp = new Microsoft.Office.Interop.Excel.Application();
            string myPath = "C:\\Results.xlsx";
            Microsoft.Office.Interop.Excel.Workbook workbook = excelApp.Workbooks.Open(myPath);

            int idx = 1;
            foreach (string s in split)
            {
                excelApp.Cells[idx, 1] = s;
                idx++;
            }

            excelApp.Cells[idx + 1, 1] = ("Number of Passed Steps: " + passed);
            excelApp.Cells[idx + 2, 1] = ("Number of Not Run Steps: " + notRunSteps);
            excelApp.Cells[idx + 3, 1] = ("Total Test Result: " + overall);
            excelApp.Visible = true;

            excelApp.ActiveWorkbook.Save();
            workbook.Close(false, Type.Missing, Type.Missing);
            excelApp.Workbooks.Close();
            System.Runtime.InteropServices.Marshal.ReleaseComObject(workbook);

            excelApp.Quit();
            GC.Collect();
            System.Runtime.InteropServices.Marshal.ReleaseComObject(excelApp);
        }

Reference site:
http://www.telerik.com/automated-testing-tools/support/documentation/user-guide/code-samples/general/external-log-file.aspx

1 Answer, 1 is accepted

Sort by
0
Velin Koychev
Telerik team
answered on 21 Oct 2013, 12:59 PM
Hi David,

You can see in this other forum thread another code sample that will show you how you can write in a specific worksheet. 

Let me know if you need any additional assistance. 

Regards,
Velin Koychev
Telerik
Quickly become an expert in Test Studio, check out our new training sessions!
Test Studio Trainings
Tags
General Discussions
Asked by
David
Top achievements
Rank 1
Answers by
Velin Koychev
Telerik team
Share this question
or