Service Test 11.20 : How to Write Web Service Response to Excel 2010 DataTable

by Joe Colantonio on December 15, 2011

Post image for Service Test 11.20 : How to Write Web Service Response to Excel 2010 DataTable

The other day I received an e-mail from my blog-reading friend Samar. He asked:

"I've created a local Excel table with one column (e.g Column1). How can I set a column programmatically?"

Rather than sending him a long e-mail reply, I've decided to answer in the form of a blog post, which follows below.

How to Programmatically Write to Excel 2010

1. First, start the HP Flight sample application and enter the end point http://localhost:24240/HPFlights_SOAP?WSDL to import the flight web services into Service Test

(For step-by-step instructions, be sure to check out my post, HP Service Test 11 – How to Test a Web Services Video).

2. Drag the CreateFlightOrder operation onto the main canvas area.

3. Drag a Custom Code activity onto the main canvas area.

4. Under the Custom Code's "Property Sheet," create an Input value named joePrice.

5. Click on the "Event" icon and create a default handler for the ExecuteEvent.

6. Enter the following code under your onExecuteEvent under the TestUserCode.cs section:

string price;
price = this.CodeActivity12.Input.joePrice.ToString();
System.Data.OleDb.OleDbConnection MyConnection;
System.Data.OleDb.OleDbCommand myCommand = new System.Data.OleDb.OleDbCommand();
string sql = null;
MyConnection = new System.Data.OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source='D:\\STDEMO.xlsx';Extended Properties=Excel 12.0;Persist Security Info=False");
MyConnection.Open();
myCommand.Connection = MyConnection;
sql = "INSERT into [Sheet1$] (COLUMN1) values('" + price + "')";
myCommand.CommandText = sql;
myCommand.ExecuteNonQuery();
MyConnection.Close();

7. Run the script. You should now have a price value in your Excel file:

How to Use Service Test's Built-in Database Commands to Write to an Excel File

1. Follow steps 1 through 3 (above).

2. Drag a Concatenate String operation onto the main canvas area.

3. Under the 'Concatenate Strings' "Property Sheet," enter the following under the Prefix "Property":

INSERT into [Sheet1$] (COLUMN1) values ('

For the Suffix, click on the "link to data source," and point the TotalPrice value from the CreateFlightOrder operation:

4. Drag another Concatenate String operation onto the main canvas area.

For the Prefix, click on the "link to data source," and point to the previous Concatenate String's Result output.

For the Suffix, enter ').

5. Drag an Open Connection operation onto the main canvas area.

6. Under the Open Connection "Property Sheet," enter the following under the Connection String:

Provider=Microsoft.ACE.OLEDB.12.0;Data Source='D:\\STDEMO.xlsx';Extended Properties=Excel 12.0;Persist Security Info=False

(FYI – IF you do not have the ACE ODBC driver you should be able to get it by installing http://www.microsoft.com/download/en/details.aspx?id=13255

7. Drag an Execute Command operation onto the main canvas area.

For the Connection property, point to the Open Connection's "Connection Output" property.

For the Command Property, point to the previous Concatenation String's "Results" property.

8. Run the script. You should now have a price value in your Excel file.

Hope this post helps you to excel with Excel and HP Services Test 11.20

{ 18 comments… read them below or add one }

Thulasi January 17, 2012 at 6:40 am

How to write the output of a database querry to an XML File?

Reply

Joe Colantonio January 18, 2012 at 1:54 pm

Thulasi » Hi Thulasi- have you tried using ST 11.20′s db activity with the XML\String to XML activity to get the functionality you are looking for? If you use the Database\Select Data activity and point the String To XML’s Source String parameter to Select Data’s results does it work for you? Are you having issues with this approach? Let me know. Cheers~Joe

Reply

STUSER January 18, 2012 at 11:27 pm

Hi Joe,
I tried above steps. Test has passed successfully but nothing updated in excel sheet. Please help what else should i check.

Here is my code:

string price;
price = this.CodeActivity5.Input.joePrice.ToString();
System.Data.OleDb.OleDbConnection MyConnection;
System.Data.OleDb.OleDbCommand myCommand = new System.Data.OleDb.OleDbCommand();
string sql = null;
MyConnection = new System.Data.OleDb.OleDbConnection(“Provider=Microsoft.ACE.OLEDB.12.0;Data Source=’C:\\STDEMO.xlsx’;Extended Properties=Excel 12.0;Persist Security Info=False”);
MyConnection.Open();
myCommand.Connection = MyConnection;
sql = “INSERT into [Sheet1$] (COLUMN1) values(‘” + price + “‘)”;
myCommand.CommandText = sql;
myCommand.ExecuteNonQuery();
MyConnection.Close();

Reply

Joe Colantonio January 19, 2012 at 3:26 pm

STUSER » Hi – what version of Excel are you using? I took the code you sent and was able to run it in ST. It did write the price value to the STDEMO spreadsheet. What happens if you create a new script with just a custom code activity and hard code a value for joePrice does it work? Let me know. Cheers~Joe

Reply

STUSER January 23, 2012 at 8:36 pm

@Joe, No need of your time now. I was able to send output to spreadsheet . Thanks for the code

Reply

abc July 30, 2012 at 7:35 pm

what was the issue?

Reply

abc July 26, 2012 at 4:50 pm

Joe,

I am facing issues with this.I teried the same code.I donot get any error but, I dinot see EXCEL updated.Please help

Reply

Joe Colantonio July 27, 2012 at 3:28 pm

abc » What version of Excel are you using?

Reply

abc July 30, 2012 at 2:38 pm

Excel 2003.I tried to save the file in Excel 2007 also.

Reply

Joe Colantonio July 30, 2012 at 3:08 pm

abc » I will update this post but this code is specifically for Excel 2010. If you are using 2007 you cant try to change the Extended Properties=Excel 12.0 to Extended Properties=Excel 8.0. For 2003 you would have to change the Provider to = Provider=Microsoft.Jet.OLEDB.4.0. Sorry for the confusion. Hope this helps~Cheers Joe

Reply

abc July 30, 2012 at 3:51 pm

How do I create default handler in step 6.Please explain with example.

Reply

abc July 31, 2012 at 2:59 pm

Joe,

I followed all the steps mentioned for this, however, I do not see excel updated.Could you please guibe me on what might be the issue.

Reply

Kavitha August 2, 2012 at 10:55 am

Hi, we have encountered an issue with respect to Data Source using ‘Excel’.
Can you please let us know how to iterate a single test flow for multiple Rows in an Excel through Service Test. Let us know the configuration to be made in order to achieve this task.

Reply

Joe Colantonio August 2, 2012 at 1:07 pm

Kavitha » Hi Kavitha – you should be able to do this by specifying a For loop at the Test Flow Level. For example in your ST test click on the ‘Test Flow’. The Test Flow should appear in your Property Sheet. Click on the Input button (Step Icon). Under the Input section click on the ‘For’ Loop radio button and specify the number of iterations that you want to run. Hope I’m understanding your issue correctly. Please let me know if this works for you. Cheers~Joe

Reply

Kavitha August 3, 2012 at 6:40 am

Hi Joe,

Thanks for your quick reply, your information helped me to resolve the issue.

Reply

Madan August 20, 2012 at 2:49 pm

Hi Joe,

I may need your help again. I have similar issue, but I would like import results into local table with in Service test.

For example, I have created some test to add two numbers from local table. I have added three columns (column a, b and c) in local table and I have filled column ‘a’ and column ‘b’ with values and left column ‘c’ empty. My test will add values from columns ‘a’ and ‘b’ with in a loop for all rows in local table.
I would like to get the result into column ‘c’ ..

is there any way to do that please?

Many Thanks…

Reply

Rakhi February 17, 2013 at 8:08 pm

Hi Joe/All,

We are planning to use UFT 11.5 for web service automation, is there any framework or approach similar to qtp automation ? Is there a way to implement data driven framework for hp st ? Please share the details.

Thanks in advance.

- Rakhi

Reply

Noel.R.Sinnou April 12, 2013 at 9:29 am

Hello Joel ,

We are planning to implement Web Service Automation Framework using Functional approach .

Can you share across a few sample frame works please ?

Reply

Leave a Comment

Previous post:

Next post: