有哪些Vbscript生成Excel报表的常用操作

发布时间:2021-09-30 11:35:33 作者:iii
来源:亿速云 阅读:99

这篇文章主要介绍“有哪些Vbscript生成Excel报表的常用操作”,在日常操作中,相信很多人在有哪些Vbscript生成Excel报表的常用操作问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”有哪些Vbscript生成Excel报表的常用操作”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

使用QTP自动化测试结束后,经常需要将测试结果写入Excel中,这里就把一些常用对Excel操作的方法进行归纳、整理,方便使用时查阅。支持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) '获得指定工作表
' *************** 对数据表的操作 ***************
 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.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

补充:

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) ‘文件另存为

到此,关于“有哪些Vbscript生成Excel报表的常用操作”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!

推荐阅读:
  1. 把sar的数据生成报表
  2. ReportViewer生成报表的相关代码

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

vbscript excel

上一篇:9个好用的JavaScript小技巧是怎样的

下一篇:如何实现干掉进程并删除源文件的批处理bat代码

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》