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

2 posts, 0 answers
  1. David
    David avatar
    1 posts
    Member since:
    Aug 2013

    Posted 16 Oct 2013 Link to this post


    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? 
    test1.tstest writes to Sheet1
    test2.tstest writes to Sheet2

    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;

                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;

                workbook.Close(false, Type.Missing, Type.Missing);


    Reference site:
  2. Velin Koychev
    Velin Koychev avatar
    88 posts

    Posted 21 Oct 2013 Link to this post

    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. 

    Velin Koychev
    Quickly become an expert in Test Studio, check out our new training sessions!
    Test Studio Trainings
Back to Top