对于QTP操作excel的大前提是,保证组建服务里的DCOM配置里存在 microsoft excel application ,具体的查看方式是,在运行框中输入dcomcnfg,然后会打开组件服务的窗口,选择组件服务,计算机,我的电脑,DCOM配置,可以在里面查找存不存在microsoft excel application,存在的话,恭喜你,不出意外是可以对excel文档进行操作了。
下面为具体的操作代码。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36 |
option explicit Dim excel Set excel = createObject( "excel.application" ) excel.Visible = true Dim excelBook ‘打开工作簿 Set excelBook = excel.Workbooks.Open( "C:\Documents and Settings\Administrator\桌面\test.xls" ) ‘指定工作的表格 Dim excelSheet Set excelSheet = excelBook.Sheets( "Sheet1" ) ‘取出表格里有效的行数 Dim rowCount rowCount= excelSheet.usedRange.rows.count ‘取出表格里有效的列数 Dim
columnCount columnCount=excelSheet.usedRange.columns.count ‘取出每个单元格的值 Dim cellValue,i,j cellValue = "" For
i = 1 to rowCount For
j =1 to columnCount cellValue = excelSheet.cells(i,j) & cellValue Next Next ‘msgbox(cellValue) ‘向excel中写入数据 Dim content content = "this is a new cell" excelSheet.cells(rowCount+1,1) = content ‘保存写入的数据 excelBook.Save excelBook.Close excel.Quit Set excelSheet = nothing Set excelBook = nothing Set excel = nothing |
原文:http://www.cnblogs.com/huang1990/p/3682725.html