国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > VBScript > 正文

Vbscript生成Excel報表的常用操作總結(jié)

2020-06-26 18:40:09
字體:
供稿:網(wǎng)友

這篇文章主要介紹了Vbscript生成Excel報表的常用操作總結(jié),涵蓋方方面面的Excel操作命令,不容錯過,需要的朋友可以參考下

使用QTP自動化測試結(jié)束后,經(jīng)常需要將測試結(jié)果寫入Excel中,這里就把一些常用對Excel操作的方法進行歸納、整理,方便使用時查閱。支持Office Excel 2003版本,不支持2007版本。

Vbscript代碼

 

 
  1. On Error Resume Next 
  2. Dim FileName, SheetName, Text, ExcelApp, ExcelBook, ExcelSheet 
  3. FileName = "D:/Book1.xls" 
  4. SheetName = "新建表" 
  5. Text = "Hello QTP ! 你好, QuickTestProfessional !" 
  6. Set ExcelApp = CreateObject("Excel.Application"
  7. Set ExcelBook= ExcelApp.Workbooks.Open(FileName) 
  8. Set ExcelSheet = ExcelBook.Sheets.Add '插入工作表 
  9. 'Set ExcelSheet = ExcelBook.Sheets.Item(SheetName) '獲得指定工作表 
  10. ' *************** 對數(shù)據(jù)表的操作 *************** 
  11. For i=1 To ExcelBook.Sheets.Count 
  12. If ExcelBook.Sheets(i).Name=SheetName Then 
  13. ExcelApp.DisplayAlerts=False 
  14. ExcelBook.Sheets(i).Delete '刪除工作表 
  15. ExcelApp.DisplayAlerts=True 
  16. Exit For 
  17. End If 
  18. Next 
  19. ExcelSheet.Name = SheetName '重命名工作表 
  20. ' *************** 對文字的操作 *************** 
  21. ExcelSheet.Cells(1,2) = Text 
  22. ExcelSheet.Range("B2","B20").Value = Text 
  23. ExcelSheet.Cells(1,2).Font.Name = "Verdana" '設(shè)置字體 
  24. ExcelSheet.Cells(1,2).Font.Size = 25 '設(shè)置字號 
  25. ExcelSheet.Cells(1,2).Font.Color = RGB(0, 0, 255) '設(shè)置字體顏色 
  26. ExcelSheet.Cells(2,2).Font.Bold = True '文字加粗 
  27. ExcelSheet.Cells(3,2).Font.Italic = True '文字傾斜 
  28. ExcelSheet.Cells(4,2).Font.Underline = True '文字加下劃線 
  29. ExcelSheet.Cells(5,2).Font.Strikethrough = True '文字加刪除線 
  30. ExcelSheet.Cells(6,2).Characters(2, 2).Font.Superscript = True '設(shè)定文字上標 
  31. ExcelSheet.Cells(7,2).Characters(2, 2).Font.Subscript = True '設(shè)定文字下標 
  32. ' *************** 對單元格的操作 *************** 
  33. ExcelSheet.Columns("B").ColumnWidth = 40 '設(shè)置列寬 
  34. 'ExcelSheet.Columns("B").AutoFit '自動調(diào)整列寬 
  35. ExcelSheet.Range("B11").RowHeight=40 '設(shè)置行高 
  36. 'ExcelSheet.Rows(11).Rows.AutoFit '自動調(diào)整行高 
  37. ExcelSheet.Range("B8","D8").Merge '合并單元格,水平方向 
  38. ExcelSheet.Range("B18","B19").Merge '合并單元格,垂直方向 
  39. ExcelSheet.Range("B8","D8").Borders.Color = RGB(0,255,0) '設(shè)定單元格邊框顏色 
  40. ExcelSheet.Range("B12").Interior.Color = RGB(255,0,0) '設(shè)置單元格背景色 
  41. ExcelSheet.Cells(9,2).WrapText = True '自動換行 
  42. ExcelSheet.Cells(10,2).HorizontalAlignment = 3 '設(shè)置水平對齊,1常規(guī),2靠左,3居中,4靠右 
  43. ' 5填充,6兩端對齊,7跨列居中,8分散對齊 
  44. ExcelSheet.Cells(11,2).VerticalAlignment = 1 '設(shè)置垂直對齊,1靠上,2居中,3靠下 
  45. ' 4兩端對齊,5分散對齊 
  46. ExcelSheet.Range("B14").Borders(1).LineStyle=1 '設(shè)置左邊框樣式 
  47. ExcelSheet.Range("B14").Borders(2).LineStyle=2 '設(shè)置右邊框樣式 
  48. ExcelSheet.Range("B14").Borders(3).LineStyle=3 '設(shè)置上邊框樣式 
  49. ExcelSheet.Range("B14").Borders(4).LineStyle=4 '設(shè)置下邊框樣式 
  50. ExcelSheet.Range("B15").ClearContents '清除單元格內(nèi)容 
  51. ExcelSheet.Range("B16").Formula="=1+10" '設(shè)置單元格公式 
  52. ExcelSheet.Range("B17").AddComment("Hello" & vbLf & "QTP") '插入批注 
  53. ExcelSheet.Range("B17").Comment.Visible=True '顯示批注 
  54. 'ExcelSheet.Range("B17").ClearComments '清除批注,與刪除批注效果相同 
  55. 'ExcelSheet.Range("B17").Comment.Delete '刪除批注,與清除批注效果相同 
  56. 'ExcelSheet.SaveAs("D:/Book2.xls") '另存為 
  57. ExcelBook.Save 
  58. ExcelBook.Close 
  59. ExcelApp.Quit 
  60. Set ExcelBook = Nothing 
  61. Set ExcelApp = Nothing 
  62. SystemUtil.CloseProcessByName "Excel.exe" '如果仍有Excel.exe進程,可使用這句關(guān)閉進程 
  63. If Err.number>0 Then 
  64. MsgBox Err.Description 
  65. End If 
  66. On Error GoTo 0 

補充:

 

 
  1. ExcelApp.DisplayAlerts = False ‘關(guān)閉兼容性檢查 
  2. ExcelBook = ExcelApp.Workbooks.Add ‘新建Excel 
  3. ExcelSheet = ExcelBook.ActiveSheet ‘激活第一個表 
  4. ExcelSheet.Columns(“A:E”).AutoFit() ‘設(shè)置A到E列自動調(diào)整列寬 
  5. ExcelBook.SaveAs(“D:/Book2.xls”,FileFormat:=Excel.XLFileFormat.xlAddIn) ‘文件另存為 

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 察隅县| 五原县| 武义县| 惠东县| 巴南区| 兰坪| 余干县| 威信县| 宾阳县| 赤峰市| 五莲县| 黎平县| 海口市| 岑溪市| 临武县| 西安市| 定兴县| 长沙县| 中宁县| 武定县| 盐亭县| 长丰县| 循化| 宜阳县| 延吉市| 阳西县| 霍林郭勒市| 德州市| 浪卡子县| 铜陵市| 江口县| 南漳县| 陆良县| 喀喇沁旗| 黄梅县| 即墨市| 邳州市| 延川县| 晋江市| 军事| 玉林市|