zoukankan      html  css  js  c++  java
  • 使用TestComplete从数据库表导出数据到Excel

    参考:

    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.CreateConnection method, 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

  • 相关阅读:
    Log4Net 配置详解
    .Net Core 获取应用物理路径的常见问题
    Js/Jquery获取iframe中的元素
    Ztree树使用详解
    【解决】nginx + socket.io ,能连接但不响应事件
    基础文档官方链接
    位运算
    Java基础之集合框架--Collections.reverse()方法
    Android动画攻略—帧动画、补间动画、属性动画
    [转]京东mPaaS移动日志建设与应用
  • 原文地址:https://www.cnblogs.com/testware/p/1944933.html
Copyright © 2011-2022 走看看