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 }
How to write the output of a database querry to an XML File?
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
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();
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
@Joe, No need of your time now. I was able to send output to spreadsheet . Thanks for the code
what was the issue?
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
abc » What version of Excel are you using?
Excel 2003.I tried to save the file in Excel 2007 also.
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
How do I create default handler in step 6.Please explain with example.
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.
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.
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
Hi Joe,
Thanks for your quick reply, your information helped me to resolve the issue.
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…
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
Hello Joel ,
We are planning to implement Web Service Automation Framework using Functional approach .
Can you share across a few sample frame works please ?