使用TestComplete从数据库表导出数据到Excel
2011-01-25 21:30
417 查看
参考:
Exporting Database Tables to Excel Sheets Using TestComplete
http://www.automatedqa.com/blogs/post/11-01-25/exporting-database-tables-to-excel-sheets-using-testcomplete/
First of all, we connect to the desired database using the ADO functionality:
// Creates a new connection
var ConnDB = ADO.CreateConnection();
ConnDB.ConnectionString = "DRIVER=SQL Server;SERVER=H_kptest;UID=aaa;PWD=aaa";
ConnDB.Open();
We create an ADO Connection object by calling TestComplete’s
The Connection object which we obtained is an ADO data access object registered in the operating system. We use its Execute method to retrieve data from the database:
TableName = "ListOfOrders";
// Opens a recordset
var Tbl = ConnDB.Execute("SELECT * FROM " + TableName);
After we get data from the database, we connect to Excel via COM. For this purpose we will use TestComplete’s property -
var ExcelFilePath = "C:\\Book1.xls";
var SheetName = "Sheet1";
// Opens an Excel file
var MsExcel = Sys.OleObject("Excel.Application");
MsExcel.Workbooks.Open(ExcelFilePath);
var Sheet = MsExcel.Sheets(SheetName);
MsExcel.Visible = true;
Then, we use both ADO and Excel COM objects to copy desired column names and data from the database table to the work sheet:
// Copies field names
for (var i = 0; i < Tbl.Fields.Count; i++)
Sheet.Cells(1, i + 1).Value = Tbl.Fields.Item(i).Name;
// Scans all records returned by the query
Tbl.MoveFirst();
var RowIndex = 2;
while (! Tbl.EOF)
{
for (i = 0; i < Tbl.Fields.Count; i++)
Sheet.Cells(RowIndex, i + 1).Value = Tbl.Fields.Item(i).Value;
Tbl.MoveNext();
RowIndex++;
}
At the end, we save the changes to the Excel worksheet and close connection to the database:
// Closes the Excel file
MsExcel.Save();
MsExcel.Workbooks.Close();
// Closes the recordset and the connection
Tbl.Close();
ConnDB.Close();
For more information on working with databases and Excel files from your tests, see TestComplete on-line help: Working with Databases and Working With Microsoft Excel Files
Exporting Database Tables to Excel Sheets Using TestComplete
http://www.automatedqa.com/blogs/post/11-01-25/exporting-database-tables-to-excel-sheets-using-testcomplete/
First of all, we connect to the desired database using the ADO functionality:
// Creates a new connection
var ConnDB = ADO.CreateConnection();
ConnDB.ConnectionString = "DRIVER=SQL Server;SERVER=H_kptest;UID=aaa;PWD=aaa";
ConnDB.Open();
We create an ADO Connection object by calling TestComplete’s
ADO.CreateConnectionmethod, then form a string with connection attributes and open the connection. If you have problems with specifying a connection string for your database, please visit www.connectionstrings.com.
The Connection object which we obtained is an ADO data access object registered in the operating system. We use its Execute method to retrieve data from the database:
TableName = "ListOfOrders";
// Opens a recordset
var Tbl = ConnDB.Execute("SELECT * FROM " + TableName);
After we get data from the database, we connect to Excel via COM. For this purpose we will use TestComplete’s property -
Sys.OleObject. This property is parameterized with the program id of the desired COM server. After we connect to the Excel COM engine, we use Excel COM objects and methods to open the desired worksheet:
var ExcelFilePath = "C:\\Book1.xls";
var SheetName = "Sheet1";
// Opens an Excel file
var MsExcel = Sys.OleObject("Excel.Application");
MsExcel.Workbooks.Open(ExcelFilePath);
var Sheet = MsExcel.Sheets(SheetName);
MsExcel.Visible = true;
Then, we use both ADO and Excel COM objects to copy desired column names and data from the database table to the work sheet:
// Copies field names
for (var i = 0; i < Tbl.Fields.Count; i++)
Sheet.Cells(1, i + 1).Value = Tbl.Fields.Item(i).Name;
// Scans all records returned by the query
Tbl.MoveFirst();
var RowIndex = 2;
while (! Tbl.EOF)
{
for (i = 0; i < Tbl.Fields.Count; i++)
Sheet.Cells(RowIndex, i + 1).Value = Tbl.Fields.Item(i).Value;
Tbl.MoveNext();
RowIndex++;
}
At the end, we save the changes to the Excel worksheet and close connection to the database:
// Closes the Excel file
MsExcel.Save();
MsExcel.Workbooks.Close();
// Closes the recordset and the connection
Tbl.Close();
ConnDB.Close();
For more information on working with databases and Excel files from your tests, see TestComplete on-line help: Working with Databases and Working With Microsoft Excel Files
相关文章推荐
- 使用opi将数据库数据导出到Excel
- java、Extjs导出数据库里的数据至Excel并下载至本地进行数据备份方法之二——使用Action
- 使用pentaho工具将数据库数据导入导出为Excel
- java、Extjs导出数据库里的数据至Excel并下载至本地进行数据备份方法之一——使用servlet
- 使用PL/SQL导出数据库表中的数据为excel格式
- 从数据库导出数据到word、excel、.txt
- Java操作Excel(二)将数据库中的数据导出到Excel中
- 使用sql*plus导出数据表为excel或者html
- 使用SSIS将数据从Cube中导出到关系型数据库
- PHP_ThinkPHP使用phpExcel实现Excel数据的导出
- 数据库中数据导出成excel
- 将数据库中的数据导出到Excel和Doc中
- C#中对Excel的操作 使用Excel公式,导入导出数据库等
- Oracle 使用TOAD实现导入导出Excel数据
- 使用sqlplus/mysql 导出数据为excel或html
- 使用NPOI将Excel文件的数据导入数据库
- NPOI 操作数据库中数据的导入导出(Excel.xls文件) 和null数据的处理。
- C#数据库数据导入导出系列之三 数据库导出到Excel下
- C#导出EXCEL超大数据源码,解决内存溢出问题,使用OLEDB方式(NPOI做不到)
- 从数据库导出数据到word、excel、.txt(二)