51Testing软件测试论坛

标题: 【seagull1985-QTP】EXCEL测试报告生成一 [打印本页]

作者: seagull1985    时间: 2016-8-5 09:40
标题: 【seagull1985-QTP】EXCEL测试报告生成一
代码的来源已经不记得哪里COPY的了,我自己用了觉得非常好,优化了里面部分的代码。大家有兴趣的可以试试。。先上2张效果图
[attach]102080[/attach]

[attach]102081[/attach]


代码部分因为超过贴子长度要求,只能分开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

复制代码


作者: 绿色的枫叶    时间: 2016-9-26 15:07
我的运行到179行的时候会提示报错这是什么原因
作者: seagull1985    时间: 2016-9-26 16:22
绿色的枫叶 发表于 2016-9-26 15:07
我的运行到179行的时候会提示报错这是什么原因

图中提示的错误,有没有排除下?
作者: 绿色的枫叶    时间: 2016-9-28 09:00
seagull1985 发表于 2016-9-26 16:22
图中提示的错误,有没有排除下?

排除过,文件下都已创建表格了,但是创建的表格里只有表头和格式 其他内容都没有
作者: wangjun7    时间: 2016-11-2 08:40
多谢




欢迎光临 51Testing软件测试论坛 (http://bbs.51testing.com/) Powered by Discuz! X3.2