VBS生成Excel報告的常用操作

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) '獲得指定工作表

' *************** 對數據表的操作 ***************
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       '重命名工作表


' *************** 對文字的操作 ***************
ExcelSheet.Cells(1,2) = Text
ExcelSheet.Range("B2","B20").Value = Text
ExcelSheet.Cells(1,2).Font.Name = "Verdana"    '設置字體
ExcelSheet.Cells(1,2).Font.Size = 25     '設置字號
ExcelSheet.Cells(1,2).Font.Color = RGB(0, 0, 255)  '設置字體顏色
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 '設定文字上標
ExcelSheet.Cells(7,2).Characters(2, 2).Font.Subscript = True '設定文字下標


' *************** 對單元格的操作 ***************
ExcelSheet.Columns("B").ColumnWidth = 40   '設置列寬
'ExcelSheet.Columns("B").AutoFit     '自動調整列寬
ExcelSheet.Range("B11").RowHeight=40    '設置行高
'ExcelSheet.Rows(11).Rows.AutoFit     '自動調整行高
ExcelSheet.Range("B8","D8").Merge     '合併單元格,水平方向
ExcelSheet.Range("B18","B19").Merge     '合併單元格,垂直方向
ExcelSheet.Range("B8","D8").Borders.Color = RGB(0,255,0) '設定單元格邊框顏色
ExcelSheet.Range("B12").Interior.Color = RGB(255,0,0)  '設置單元格背景色
ExcelSheet.Cells(9,2).WrapText = True    '自動換行
ExcelSheet.Cells(10,2).HorizontalAlignment = 3  '設置水平對齊,1常規,2靠左,3居中,4靠右
             ' 5填充,6兩端對齊,7跨列居中,8分散對齊
ExcelSheet.Cells(11,2).VerticalAlignment = 1  '設置垂直對齊,1靠上,2居中,3靠下
             ' 4兩端對齊,5分散對齊
ExcelSheet.Range("B14").Borders(1).LineStyle=1  '設置左邊框樣式
ExcelSheet.Range("B14").Borders(2).LineStyle=2  '設置右邊框樣式
ExcelSheet.Range("B14").Borders(3).LineStyle=3  '設置上邊框樣式
ExcelSheet.Range("B14").Borders(4).LineStyle=4  '設置下邊框樣式
ExcelSheet.Range("B15").ClearContents    '清除單元格內容
ExcelSheet.Range("B16").Formula="=1+10"    '設置單元格公式
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.DisplayAlerts = False '關閉兼容性檢查

ExcelBook = ExcelApp.Workbooks.Add '新建Excel

ExcelSheet = ExcelBook.ActiveSheet '激活第一個表

ExcelSheet.Columns("A:E").AutoFit() '設置A到E列自動調整列寬

ExcelBook.SaveAs("D:\Book2.xls",FileFormat:=Excel.XLFileFormat.xlAddIn) '文件另存爲

ExcelApp.Quit
Set ExcelBook = Nothing
Set ExcelApp = Nothing 
SystemUtil.CloseProcessByName "Excel.exe"  '如果仍有Excel.exe進程,可使用這句關閉進程
If Err.number>0 Then
 MsgBox Err.Description
End If
On Error GoTo 0

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章