51Testing软件测试论坛

 找回密码
 (注-册)加入51Testing

QQ登录

只需一步,快速开始

微信登录,快人一步

查看: 2283|回复: 4
打印 上一主题 下一主题

[资料] 【seagull1985-QTP】EXCEL测试报告生成一

[复制链接]
  • TA的每日心情
    奋斗
    2018-8-27 15:56
  • 签到天数: 322 天

    连续签到: 1 天

    [LV.8]测试军长

    跳转到指定楼层
    1#
    发表于 2016-8-5 09:40:41 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    代码的来源已经不记得哪里COPY的了,我自己用了觉得非常好,优化了里面部分的代码。大家有兴趣的可以试试。。先上2张效果图





    代码部分因为超过贴子长度要求,只能分开2个贴子放出来了,使用的时候,直接把2份代码接起来就可以了

    代码PART1
    1. Class excelreporterengine

    2.                         Private oEngine
    3.                        
    4.                         Private objSheet
    5.                        
    6.                     Private objWorkBook

    7.                         Public str_ScriptPath
    8.                        
    9.                          Private Sub Class_Initialize   
    10.                                   str_ScriptPath = str_Test_Path&"Report\"
    11.                                   

    12.                                   me.StartReportEngine GetTestName,"Result.xls"
    13.                                   me.OpenExcelFile  me.str_ScriptPath&GetTestName&"\"&"Result.xls"
    14.                                   
    15.                            End Sub
    16.                        
    17.                         Private Sub Class_Terminate      
    18.                         me.CloseExcelFile
    19.                         Set objWorkBook = Nothing
    20.                         Set objSheet = nothing
    21.                     Set  oEngine =nothing
    22.                         End Sub
    23.                        
    24.                        
    25.                        
    26.                         'create a folder
    27.                         Function Createreporterfolder(str_folderpath)
    28.                         Dim fso, f
    29.                         Set fso = CreateObject("Scripting.FileSystemObject")
    30.                        
    31.                            If not fso.FolderExists(str_folderpath)  Then
    32.                           
    33.                            Set f = fso.CreateFolder(str_folderpath)
    34.                            Set f = Nothing
    35.                            Set fso = Nothing
    36.                            Else
    37.                            Set fso = Nothing
    38.                            End If
    39.                        
    40.                         End Function
    41.                        
    42.                         'create a excelfile  and initialize
    43.                    Function CreateResultFile(FilePath)
    44.                                 Dim fso
    45.                                 Set fso = CreateObject("Scripting.FileSystemObject")
    46.                           If not  fso.FileExists(FilePath)Then
    47.                           
    48.                                
    49.                                         Set oEngine = CreateObject("excel.Application")
    50.                   
    51.                                         'Disable alerts
    52.                                         oEngine.DisplayAlerts = False
    53.                                        
    54.                                         'Add a workbook to the Excel App
    55.                                         oEngine.Workbooks.Add
    56.                                        
    57.                                         'Get the object of the first sheet in the workbook
    58.                                         Set objSheet = oEngine.Sheets.Item(1)
    59.                                         oEngine.Sheets.Item(1).Select
    60.                                         With objSheet
    61.                                                 'Rename the first sheet to "Test_Summery"
    62.                                                 .Name = "Test_Summary"
    63.                                                 'Set the Heading
    64.                                                 .Range("B1").Value = "Test Results"
    65.                                                 .Range("B1:C1").Merge               
    66.                                                 'Set color and Fonts for the Header
    67.                                                 .Range("B1:C1").Interior.ColorIndex = 23
    68.                                                 .Range("B1:C1").Font.ColorIndex = 2
    69.                                                 .Range("B1:C1").Font.Bold = True
    70.                                
    71.                                                 'Set the Date and time of Execution
    72.                                                 .Range("B3").Value = "Test Date: "
    73.                                                 .Range("B4").Value = "Test Start Time: "
    74.                                                 .Range("B5").Value = "Test End Time: "
    75.                                                 .Range("B6").Value = "Test Duration: "   
    76.                                                 .Range("C3").Value = Date
    77.                                                 .Range("C4").Value = Time
    78.                                                 .Range("C5").Value = Time
    79.                                                 .Range("C6").Value = "=R[-1]C-R[-2]C"
    80.                                                 .Range("C6").NumberFormat = "[h]:mm:ss;@"
    81.                                                
    82.                                                 'Set the Borders for the Date & Time Cells
    83.                                                 .Range("B3:C8").Borders(1).LineStyle = 1
    84.                                                 .Range("B3:C8").Borders(2).LineStyle = 1
    85.                                                 .Range("B3:C8").Borders(3).LineStyle = 1
    86.                                                 .Range("B3:C8").Borders(4).LineStyle = 1
    87.                                                
    88.                                                 'Format the Date and Time Cells
    89.                                                 .Range("B3:C8").Interior.ColorIndex = 37
    90.                                                 .Range("B3:C8").Font.ColorIndex = 1
    91.                                                 .Range("B3:A8").Font.Bold = True
    92.                                
    93.                                                 'Track the Row Count and insrtuct the viewer not to disturb this
    94.                                                 .Range("C7").AddComment
    95.                                                 .Range("C7").Comment.Visible = False
    96.                                                 .Range("C7").Comment.Text "This is a very Important field for the script." & vbCrLf & "Please Do not Edit or Delete."
    97.                                                 .Range("C7").Value = "0"
    98.                                                 .Range("B7").Value = "Number Of Testcases:"
    99.                                
    100.                                                 'Track the Testcase Count Count and insrtuct the viewer not to disturb this
    101.                                                 .Range("C8").AddComment
    102.                                                 .Range("C8").Comment.Visible = False
    103.                                                 .Range("C8").Comment.Text "This is a very Important field for the script." & vbCrLf & "Please Do not Edit or Delete."
    104.                                                 .Range("C8").Value = "0"
    105.                                                 .Range("B8").Value = "Total Number Of Test Steps:"
    106.                                                
    107.                                                 .Range("B10").Value = "TestCase Name"
    108.                                                 .Range("C10").Value = "Status"
    109.                                                 .Range("D10").Value = "Number Of Steps"                                               
    110.                                                 .Range("E10").Value = "Pass"
    111.                                                 .Range("F10").Value = "Fail"
    112.                                                 .Range("G10").Value = "Warning"
    113.                                                 .Range("H10").Value = "*Click the TestCase Name to see detail result."
    114.                                                
    115.                                                
    116.                                                
    117.                                                 'Format the Heading for the Result Summery
    118.                                                 .Range("B10:G10").Interior.ColorIndex = 23
    119.                                                 .Range("B10:G10").Font.ColorIndex = 2
    120.                                                 .Range("B10:G10").Font.Bold = True
    121.                                
    122.                                                 'Set the Borders for the Result Summery
    123.                                                 .Range("B10:G10").Borders(1).LineStyle = 1
    124.                                                 .Range("B10:G10").Borders(2).LineStyle = 1
    125.                                                 .Range("B10:G10").Borders(3).LineStyle = 1
    126.                                                 .Range("B10:G10").Borders(4).LineStyle = 1
    127.                                
    128.                                                 'Set Column width
    129.                                                 .Columns("B:G").Select
    130.                                                 .Columns("B:G").Autofit
    131.                                
    132.                                                 .Range("B11").Select
    133.                                         End With
    134.                                         'Freez pane
    135.                                         oEngine.ActiveWindow.FreezePanes = True
    136.                                
    137.                                         'Get the object of the first sheet in the workbook
    138.                                         Set objSheet = oEngine.Sheets.Item(2)
    139.                                         oEngine.Sheets.Item(1).Select
    140.                                         With objSheet
    141.                                                
    142.                                                 'Rename the first sheet to "Test_Result"
    143.                                                 .Name = "Test_Result"
    144.                                                
    145.                                                 'Set the Column widths
    146.                                                 .Columns("A:A").ColumnWidth = 30
    147.                                                 .Columns("B:B").ColumnWidth = 15
    148.                                                 .Columns("C:D").ColumnWidth = 35
    149.                                                 .Columns("E:E").ColumnWidth = 35
    150.                                                 .Columns("A:E").HorizontalAlignment = -4131
    151.                                                 .Columns("A:E").WrapText = True
    152.                                                                
    153.                                                 'Set the Heading for the Result Columns
    154.                                                 .Range("A1").Value = "STEP NAME"
    155.                                                 .Range("B1").Value = "STATUS"
    156.                                                 .Range("C1").Value = "EXPECTED RESULT"
    157.                                                 .Range("D1").Value = "ACTUAL RESULT"
    158.                                                 .Range("E1").Value = "ERROR MESSAGE"
    159.                                                
    160.                                                 'Format the Heading for the Result Columns
    161.                                                 .Range("A1:E1").Interior.ColorIndex = 23
    162.                                                 .Range("A1:E1").Font.ColorIndex = 2
    163.                                                 .Range("A1:E1").Font.Bold = True
    164.                                
    165.                                                 'Set the Borders for the Result Header
    166.                                                 .Range("A1:E1").Borders(1).LineStyle = 1
    167.                                                 .Range("A1:E1").Borders(2).LineStyle = 1
    168.                                                 .Range("A1:E1").Borders(3).LineStyle = 1
    169.                                                 .Range("A1:E1").Borders(4).LineStyle = 1
    170.                         '                                        .Range("A2").Select
    171.                                         End With
    172.                                         'Freez pane
    173.                                         oEngine.ActiveWindow.FreezePanes = True
    174.                                
    175.                                         'Save the Workbook at the specified Path with the Specified Name
    176.                                         oEngine.ActiveWorkbook.saveas FilePath
    177.                                         oEngine.Workbooks.close
    178.                                         oEngine.quit
    179.                                        
    180.                                         'Relese the Object
    181.                                         Set objSheet = Nothing
    182.                         End If
    183.                         Set fso = Nothing
    184.                         End Function
    185.                        
    186.                         'openExcelfile
    187.                         Function OpenExcelFile(Excelpath)
    188.                                         Set oEngine = CreateObject("Excel.Application")
    189.                                         Set objWorkBook =  oEngine.Workbooks.Open(Excelpath)
    190.                         end Function
    191.                        
    192.                    Function CloseExcelFile()
    193.                                 oEngine.ActiveWorkbook.save
    194.                                         oEngine.Workbooks.close
    195.                                         oEngine.quit
    196.                    End Function
    197.                        
    198.                         Function StartReportEngine(str_foldername,str_filename)
    199.                          Createreporterfolder(str_ScriptPath&str_foldername)
    200.                          CreateResultFile(str_ScriptPath&str_foldername&"\"&str_filename)  
    201.                         End Function

    复制代码

    本帖子中包含更多资源

    您需要 登录 才可以下载或查看,没有帐号?(注-册)加入51Testing

    x
    分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
    收藏收藏1
    回复

    使用道具 举报

    该用户从未签到

    2#
    发表于 2016-9-26 15:07:14 | 只看该作者
    我的运行到179行的时候会提示报错这是什么原因

    本帖子中包含更多资源

    您需要 登录 才可以下载或查看,没有帐号?(注-册)加入51Testing

    x
    回复 支持 反对

    使用道具 举报

  • TA的每日心情
    奋斗
    2018-8-27 15:56
  • 签到天数: 322 天

    连续签到: 1 天

    [LV.8]测试军长

    3#
     楼主| 发表于 2016-9-26 16:22:36 | 只看该作者
    绿色的枫叶 发表于 2016-9-26 15:07
    我的运行到179行的时候会提示报错这是什么原因

    图中提示的错误,有没有排除下?

    评分

    参与人数 1测试积点 +10 收起 理由
    lsekfe + 10 积极回复获得测试积点10

    查看全部评分

    回复 支持 反对

    使用道具 举报

    该用户从未签到

    4#
    发表于 2016-9-28 09:00:02 | 只看该作者
    seagull1985 发表于 2016-9-26 16:22
    图中提示的错误,有没有排除下?

    排除过,文件下都已创建表格了,但是创建的表格里只有表头和格式 其他内容都没有
    回复 支持 反对

    使用道具 举报

    本版积分规则

    关闭

    站长推荐上一条 /1 下一条

    小黑屋|手机版|Archiver|51Testing软件测试网 ( 沪ICP备05003035号 关于我们

    GMT+8, 2024-4-28 18:12 , Processed in 0.070193 second(s), 26 queries .

    Powered by Discuz! X3.2

    © 2001-2024 Comsenz Inc.

    快速回复 返回顶部 返回列表