Vbscript生成Excel報(bào)表的常用操作總結(jié)
使用QTP自動(dòng)化測(cè)試結(jié)束后,經(jīng)常需要將測(cè)試結(jié)果寫入Excel中,這里就把一些常用對(duì)Excel操作的方法進(jìn)行歸納、整理,方便使用時(shí)查閱。支持Office Excel 2003版本,不支持2007版本。
Vbscript代碼
On Error Resume Next
Dim FileName, SheetName, Text, ExcelApp, ExcelBook, ExcelSheet
FileName = "D:/Book1.xls"
SheetName = "新建表"
Text = "Hello QTP ! 你好, QuickTestProfessional !"
Set ExcelApp = CreateObject("Excel.Application")
Set ExcelBook= ExcelApp.Workbooks.Open(FileName)
Set ExcelSheet = ExcelBook.Sheets.Add '插入工作表
'Set ExcelSheet = ExcelBook.Sheets.Item(SheetName) '獲得指定工作表
' *************** 對(duì)數(shù)據(jù)表的操作 ***************
For i=1 To ExcelBook.Sheets.Count
If ExcelBook.Sheets(i).Name=SheetName Then
ExcelApp.DisplayAlerts=False
ExcelBook.Sheets(i).Delete '刪除工作表
ExcelApp.DisplayAlerts=True
Exit For
End If
Next
ExcelSheet.Name = SheetName '重命名工作表
' *************** 對(duì)文字的操作 ***************
ExcelSheet.Cells(1,2) = Text
ExcelSheet.Range("B2","B20").Value = Text
ExcelSheet.Cells(1,2).Font.Name = "Verdana" '設(shè)置字體
ExcelSheet.Cells(1,2).Font.Size = 25 '設(shè)置字號(hào)
ExcelSheet.Cells(1,2).Font.Color = RGB(0, 0, 255) '設(shè)置字體顏色
ExcelSheet.Cells(2,2).Font.Bold = True '文字加粗
ExcelSheet.Cells(3,2).Font.Italic = True '文字傾斜
ExcelSheet.Cells(4,2).Font.Underline = True '文字加下劃線
ExcelSheet.Cells(5,2).Font.Strikethrough = True '文字加刪除線
ExcelSheet.Cells(6,2).Characters(2, 2).Font.Superscript = True '設(shè)定文字上標(biāo)
ExcelSheet.Cells(7,2).Characters(2, 2).Font.Subscript = True '設(shè)定文字下標(biāo)
' *************** 對(duì)單元格的操作 ***************
ExcelSheet.Columns("B").ColumnWidth = 40 '設(shè)置列寬
'ExcelSheet.Columns("B").AutoFit '自動(dòng)調(diào)整列寬
ExcelSheet.Range("B11").RowHeight=40 '設(shè)置行高
'ExcelSheet.Rows(11).Rows.AutoFit '自動(dòng)調(diào)整行高
ExcelSheet.Range("B8","D8").Merge '合并單元格,水平方向
ExcelSheet.Range("B18","B19").Merge '合并單元格,垂直方向
ExcelSheet.Range("B8","D8").Borders.Color = RGB(0,255,0) '設(shè)定單元格邊框顏色
ExcelSheet.Range("B12").Interior.Color = RGB(255,0,0) '設(shè)置單元格背景色
ExcelSheet.Cells(9,2).WrapText = True '自動(dòng)換行
ExcelSheet.Cells(10,2).HorizontalAlignment = 3 '設(shè)置水平對(duì)齊,1常規(guī),2靠左,3居中,4靠右
' 5填充,6兩端對(duì)齊,7跨列居中,8分散對(duì)齊
ExcelSheet.Cells(11,2).VerticalAlignment = 1 '設(shè)置垂直對(duì)齊,1靠上,2居中,3靠下
' 4兩端對(duì)齊,5分散對(duì)齊
ExcelSheet.Range("B14").Borders(1).LineStyle=1 '設(shè)置左邊框樣式
ExcelSheet.Range("B14").Borders(2).LineStyle=2 '設(shè)置右邊框樣式
ExcelSheet.Range("B14").Borders(3).LineStyle=3 '設(shè)置上邊框樣式
ExcelSheet.Range("B14").Borders(4).LineStyle=4 '設(shè)置下邊框樣式
ExcelSheet.Range("B15").ClearContents '清除單元格內(nèi)容
ExcelSheet.Range("B16").Formula="=1+10" '設(shè)置單元格公式
ExcelSheet.Range("B17").AddComment("Hello" & vbLf & "QTP") '插入批注
ExcelSheet.Range("B17").Comment.Visible=True '顯示批注
'ExcelSheet.Range("B17").ClearComments '清除批注,與刪除批注效果相同
'ExcelSheet.Range("B17").Comment.Delete '刪除批注,與清除批注效果相同
'ExcelSheet.SaveAs("D:\Book2.xls") '另存為
ExcelBook.Save
ExcelBook.Close
ExcelApp.Quit
Set ExcelBook = Nothing
Set ExcelApp = Nothing
SystemUtil.CloseProcessByName "Excel.exe" '如果仍有Excel.exe進(jìn)程,可使用這句關(guān)閉進(jìn)程
If Err.number>0 Then
MsgBox Err.Description
End If
On Error GoTo 0
補(bǔ)充:
ExcelApp.DisplayAlerts = False ‘關(guān)閉兼容性檢查 ExcelBook = ExcelApp.Workbooks.Add ‘新建Excel ExcelSheet = ExcelBook.ActiveSheet ‘激活第一個(gè)表 ExcelSheet.Columns(“A:E”).AutoFit() ‘設(shè)置A到E列自動(dòng)調(diào)整列寬 ExcelBook.SaveAs(“D:\Book2.xls”,FileFormat:=Excel.XLFileFormat.xlAddIn) ‘文件另存為
相關(guān)文章
初窺WMI_Vbs腳本編程簡(jiǎn)明教程補(bǔ)充讀物
看了沼澤介紹的wmi腳本相關(guān)知識(shí),對(duì)新手是個(gè)不錯(cuò)的教材,如果想和系統(tǒng)結(jié)合的更好,想實(shí)現(xiàn)更大功能的代碼,那么學(xué)習(xí)vbs就不得不用wmi腳本2008-06-06
VBS中通過(guò)調(diào)用CAPICOM對(duì)象實(shí)現(xiàn)SHA1&MD5加密
MD5算法的VBS代碼在網(wǎng)上可以找到很多,這些代碼都是按照“算法”寫出來(lái)的,所以都很長(zhǎng),更重要的是,找不到SHA1算法的VBS實(shí)現(xiàn)。2011-01-01
vbs-toolkit VBSEdit 提供 免費(fèi)的COM組件
VBSCRIPT 語(yǔ)法簡(jiǎn)單 強(qiáng)大 但是功能上明顯不足 需要第三方的控制 e.g. COM 組件來(lái)擴(kuò)展其功能. VBSEDIT 安裝完之后就可以在安裝目錄下發(fā)現(xiàn) 免費(fèi)提供的 COM 組件 vbs toolkit2018-06-06
使用 iisftpdr.vbs 創(chuàng)建 FTP 虛擬目錄的方法
這篇文章主要介紹了使用 iisftpdr.vbs 創(chuàng)建 FTP 虛擬目錄的方法,需要的朋友可以參考下2014-07-07
VBS調(diào)用WMI遍歷搜索硬盤文件并計(jì)數(shù)的方法
這篇文章主要介紹了VBS調(diào)用WMI遍歷搜索硬盤文件并計(jì)數(shù)的方法,需要的朋友可以參考下2016-10-10
用vbs讀取index.dat內(nèi)容的實(shí)現(xiàn)代碼
用vbs讀取index.dat內(nèi)容的實(shí)現(xiàn)代碼,需要的朋友可以參考下。2010-11-11
vbs打開ie兩種方法 在VBScript中啟動(dòng)IE瀏覽器的實(shí)現(xiàn)代碼
用VBS打開網(wǎng)頁(yè),減少IE廣告, 不錯(cuò)的東東,是在網(wǎng)上收集的,你也可以實(shí)現(xiàn)網(wǎng)頁(yè)的自動(dòng)登錄等2013-01-01

